Skip to content

Latest commit

Β 

History

History
182 lines (128 loc) Β· 3.52 KB

File metadata and controls

182 lines (128 loc) Β· 3.52 KB

🧠 code-function-watcher

A powerful CLI tool for Go developers to scan, detect, and prevent duplicated or unused functions in your codebase.
Designed for code quality automation, especially useful in team collaboration, code review, and CI/CD pipelines.


✨ Features

  • βœ… Detect all exported functions in a directory
  • πŸ” Compare old and new snapshots to find duplicated or similar functions
  • 🧹 Detect unused functions that are defined but never called
  • 🚫 Ignore list support to skip known functions or third-party calls
  • πŸ€– GitHub CI/CD & PR auto-comment support

πŸ“¦ Installation

1. Clone the Repo

git clone https://github.com/ak4bento/code-function-watcher.git
cd code-function-watcher

2. Install Dependencies

go mod tidy

πŸš€ Usage

πŸ” Scan Functions

go run main.go scan <path> [-o output.json]

Example:

go run main.go scan ./ -o data/functions.json

This will scan all Go files recursively and extract exported function names + locations.


πŸ” Compare Function Snapshots

go run main.go compare <old.json> <new.json>

Example:

go run main.go compare data/functions-old.json data/functions-new.json

It will print potentially duplicated or very similar functions with similarity percentage (e.g., 94.5%).


🧹 Detect Unused Functions

go run main.go unused <path> --defined <functions.json> [--ignore ignore.txt]

Example:

go run main.go unused ./ --defined data/functions.json --ignore ignore.txt

Lists exported functions that were never called anywhere in your project.


🚫 Ignore List

Use a plain text file (e.g. ignore.txt) to exclude functions from being detected as "unused":

log.Println
fmt.Errorf
main

πŸ§ͺ GitHub Actions (CI/CD)

Create a file .github/workflows/scan.yml:

name: Code Function Watcher

on:
  pull_request:
    paths:
      - '**/*.go'

jobs:
  scan:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Code
        uses: actions/checkout@v3

      - name: Setup Go
        uses: actions/setup-go@v4
        with:
          go-version: 1.21

      - name: Install Dependencies
        run: go mod tidy

      - name: Run Function Comparison
        run: |
          go run main.go scan ./ -o data/functions-new.json
          go run main.go compare data/functions-old.json data/functions-new.json > result.txt

      - name: Upload Results
        uses: actions/upload-artifact@v3
        with:
          name: compare-result
          path: result.txt

πŸ’¬ Auto-Comment to Pull Request (Bonus)

Install peter-evans/create-or-update-comment

Extend your workflow:

      - name: Post PR Comment
        uses: peter-evans/create-or-update-comment@v3
        with:
          issue-number: ${{ github.event.pull_request.number }}
          body: |
            πŸ” **Function Comparison Report**
            ```
            $(cat result.txt)
            ```

πŸ“ Project Structure

.
β”œβ”€β”€ cmd/
β”‚   β”œβ”€β”€ compare.go
β”‚   β”œβ”€β”€ scan.go
β”‚   └── unused.go
β”œβ”€β”€ pkg/
β”‚   β”œβ”€β”€ compare/
β”‚   β”œβ”€β”€ exporter/
β”‚   β”œβ”€β”€ scanner/
β”‚   └── unused/
β”œβ”€β”€ data/
β”‚   └── functions-old.json
β”‚   └── functions-new.json
β”œβ”€β”€ ignore.txt
└── main.go

πŸ§‘β€πŸ’» Author

Made with ❀️ by @ak4bento