/code-function-watcher

๐Ÿ•ต๏ธโ€โ™‚๏ธ code-function-watcher is a CLI tool built in Go that helps development teams prevent redundant function creation by scanning, comparing, and identifying unused or duplicate functions in a project. Perfect for collaborative teams and codebase cleanup.

Primary LanguageGoOtherNOASSERTION

๐Ÿง  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