From 246f818052a02ac8a585b22480f8792ba47c6fb2 Mon Sep 17 00:00:00 2001 From: Gaballa Date: Fri, 22 May 2026 04:52:32 -0700 Subject: [PATCH] basic custom linter --- .github/workflows/ci.yaml | 4 +++ Makefile | 5 +++- cmd/linter/main.go | 62 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 cmd/linter/main.go diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index ac43748f..6810ffb7 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -33,6 +33,10 @@ jobs: if: steps.filter.outputs.source_code == 'true' run: nix develop .#ci -c bash -c "make fmt && git diff --exit-code" + - name: Linter check + if: steps.filter.outputs.source_code == 'true' + run: nix develop .#ci -c make linter + - name: Check if: steps.filter.outputs.source_code == 'true' run: nix develop .#ci -c make check diff --git a/Makefile b/Makefile index e8fcedae..5762081b 100644 --- a/Makefile +++ b/Makefile @@ -55,6 +55,9 @@ check-sql: ## Lint all sql files fix-sql: ## Fix all sql files sqlfluff fix --dialect sqlite +linter: + go run cmd/linter/main.go + release: ## Create a new release tag @echo "Current version: $(VERSION)" @read -p "Enter new version (e.g., v0.2.0): " version; \ @@ -69,4 +72,4 @@ clean: ## Clean up binaries and build artifacts help: ## Display this help screen @grep -hE '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' -.PHONY: help fmt run build all api cli check test check-sql fix-sql clean release generate +.PHONY: help fmt run build all api cli check test check-sql fix-sql clean release generate linter diff --git a/cmd/linter/main.go b/cmd/linter/main.go new file mode 100644 index 00000000..102312cd --- /dev/null +++ b/cmd/linter/main.go @@ -0,0 +1,62 @@ +package main + +import ( + "errors" + "go/ast" + "go/parser" + "go/token" + "io/fs" + "log" + "os" + "path/filepath" + "slices" + "strconv" +) + +var filesToSkip []string = []string{".", "swagger.go"} + +func main() { + + // Go to api/handlers + // This should be run in the project root + handlersPath := "internal/api/handlers/" + os.Chdir(handlersPath) + + err := filepath.Walk(".", func(path string, info fs.FileInfo, err error) error { + if slices.Contains(filesToSkip, path) { + return nil + } + + // Creating an AST tree by parsing + fset := token.NewFileSet() + file, err := parser.ParseFile(fset, path, nil, parser.ParseComments) + if err != nil { + log.Println(err) + os.Exit(1) + } + + // Now we can search for specific things like functions and imports + ast.Inspect(file, func(n ast.Node) bool { + // looking for an import, we shouldn't have dbmodels in handler + switch x := n.(type) { + case *ast.ImportSpec: + + importedModule, _ := strconv.Unquote(x.Path.Value) + + // yah messy, but for the sake of proposal + check := (importedModule == "github.com/acmcsufoss/api.acmcsuf.com/internal/api/store/dbmodels") + if check { + // This error is going to make me throw up lol + log.Println(errors.New("Bad import found: " + importedModule + " in " + handlersPath + path)) + os.Exit(1) + } + } + return true + }) + return nil + }) + if err != nil { + log.Println(err) + os.Exit(1) + } +}