diff --git a/.pre-commit-hooks.yaml b/.pre-commit-hooks.yaml index 3dfe64d..9dbe1a0 100644 --- a/.pre-commit-hooks.yaml +++ b/.pre-commit-hooks.yaml @@ -469,6 +469,72 @@ description: "Run 'gosec [$ARGS] ./...' in repo root folder" pass_filenames: false +# ============================================================================== +# go-staticcheck-mod +# * Folder-Based +# * Recursive +# * Targets first parent folder with a go.mod file +# * Executes if any .go files modified +# * Executes if go.mod modified +# ============================================================================== +- id: go-staticcheck-mod + name: 'go-staticcheck-mod' + entry: go-staticcheck-mod.sh + files: '(\.go$)|(\bgo\.mod$)' + exclude: '(^|/)vendor/' + language: 'script' + description: "Run 'cd $(mod_root $FILE); staticcheck [$ARGS] ./...' for each staged .go file" + pass_filenames: true + require_serial: true + +# ============================================================================== +# go-staticcheck-pkg +# * Folder-Based +# * Targets folder containing staged file +# * Executes if any .go files modified +# ============================================================================== +- id: go-staticcheck-pkg + name: 'go-staticcheck-pkg' + entry: go-staticcheck-pkg.sh + types: [go] + exclude: '(^|/)vendor/' + language: 'script' + description: "Run 'staticcheck [$ARGS] ./$(dirname $FILE)' for each staged .go file" + pass_filenames: true + require_serial: true + +# ============================================================================== +# go-staticcheck-repo-mod +# * Repo-Based +# * Recursive +# * Targets ALL folders with a go.mod file +# * Executes if any .go files modified +# * Executes if go.mod modified +# ============================================================================== +- id: go-staticcheck-repo-mod + name: 'go-staticcheck-repo-mod' + entry: go-staticcheck-repo-mod.sh + files: '(\.go$)|(\bgo\.mod$)' + exclude: '(^|/)vendor/' + language: 'script' + description: "Run 'cd $(mod_root); staticcheck [$ARGS] ./...' for each module in the repo" + pass_filenames: false + +# ============================================================================== +# go-staticcheck-repo-pkg +# * Repo-Based +# * Recursive +# * Executes if any .go files modified +# ============================================================================== +- id: go-staticcheck-repo-pkg + name: 'go-staticcheck-repo-pkg' + entry: go-staticcheck-repo-pkg.sh + types: [go] + exclude: '(^|/)vendor/' + language: 'script' + description: "Run 'staticcheck [$ARGS] ./...' in repo root folder" + pass_filenames: false + # ============================================================================== # go-test-mod # * Folder-Based diff --git a/README.md b/README.md index d51efd0..8d92ec6 100644 --- a/README.md +++ b/README.md @@ -72,6 +72,13 @@ You can copy/paste the following snippet into your `.pre-commit-config.yaml` fil - id: go-sec-repo-mod - id: go-sec-repo-pkg # + # StaticCheck + # + - id: go-staticcheck-mod + - id: go-staticcheck-pkg + - id: go-staticcheck-repo-mod + - id: go-staticcheck-repo-pkg + # # Formatters # - id: go-fmt @@ -257,6 +264,7 @@ This can be useful, for example, for hooks that display warnings, but don't gene - [go-test](#go-test) - [go-vet](#go-vet) - [go-sec](#go-sec) + - [go-staticcheck](#go-staticcheck) - Formatters - [go-fmt](#go-fmt) - [go-fumpt](#go-fumpt) @@ -323,7 +331,7 @@ Comes with Golang ( [golang.org](https://golang.org/) ) - https://golang.org/cmd/go/#hdr-Test_packages - `go help test` ------------ +---------- ### go-sec Inspects source code for security problems by scanning the Go AST. @@ -343,6 +351,26 @@ bingo install github.com/securego/gosec/v2/cmd/gosec - https://github.com/securego/gosec#usage - `gosec (no args)` +------------------ +### go-staticcheck +A state of the art linter for the Go programming language. Using static analysis, it finds bugs and performance issues, offers simplifications, and enforces style rules. + +| Hook ID | Description +|---------------------------|------------ +| `go-staticcheck-mod` | Run `'cd $(mod_root $FILE); staticcheck [$ARGS] ./...'` for each staged .go file +| `go-staticcheck-pkg` | Run `'staticcheck [$ARGS] ./$(dirname $FILE)'` for each staged .go file +| `go-staticcheck-repo-mod` | Run `'cd $(mod_root); staticcheck [$ARGS] ./...'` for each module in the repo +| `go-staticcheck-repo-pkg` | Run `'staticcheck [$ARGS] ./...'` in repo root folder + +##### Install (via [bingo](https://github.com/TekWizely/bingo)) +``` +bingo install honnef.co/go/tools/cmd/staticcheck +``` + +##### Help + - https://staticcheck.io/ + - `staticcheck -h` + ---------- ### go-vet Examines Go source code and reports suspicious constructs, such as diff --git a/go-staticcheck-mod.sh b/go-staticcheck-mod.sh new file mode 100755 index 0000000..39e9bef --- /dev/null +++ b/go-staticcheck-mod.sh @@ -0,0 +1,3 @@ +#!/usr/bin/env bash +cmd=(staticcheck) +. "$(dirname "${0}")/lib/cmd-mod.bash" diff --git a/go-staticcheck-pkg.sh b/go-staticcheck-pkg.sh new file mode 100755 index 0000000..886ebbf --- /dev/null +++ b/go-staticcheck-pkg.sh @@ -0,0 +1,3 @@ +#!/usr/bin/env bash +cmd=(staticcheck) +. "$(dirname "${0}")/lib/cmd-pkg.bash" diff --git a/go-staticcheck-repo-mod.sh b/go-staticcheck-repo-mod.sh new file mode 100755 index 0000000..19b756b --- /dev/null +++ b/go-staticcheck-repo-mod.sh @@ -0,0 +1,3 @@ +#!/usr/bin/env bash +cmd=(staticcheck) +. "$(dirname "${0}")/lib/cmd-repo-mod.bash" diff --git a/go-staticcheck-repo-pkg.sh b/go-staticcheck-repo-pkg.sh new file mode 100755 index 0000000..5359c76 --- /dev/null +++ b/go-staticcheck-repo-pkg.sh @@ -0,0 +1,3 @@ +#!/usr/bin/env bash +cmd=(staticcheck) +. "$(dirname "${0}")/lib/cmd-repo-pkg.bash" diff --git a/sample-config.yaml b/sample-config.yaml index e320a10..48b1c19 100644 --- a/sample-config.yaml +++ b/sample-config.yaml @@ -120,6 +120,13 @@ repos: - id: go-sec-repo-mod - id: go-sec-repo-pkg # + # StaticCheck + # + - id: go-staticcheck-mod + - id: go-staticcheck-pkg + - id: go-staticcheck-repo-mod + - id: go-staticcheck-repo-pkg + # # Formatters # - id: go-fmt