Skip to content

Commit

Permalink
feat: Adds support for staticcheck (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
TekWizely committed Aug 10, 2021
1 parent 1fde58e commit 4d78ac2
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 1 deletion.
66 changes: 66 additions & 0 deletions .pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 29 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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.

Expand All @@ -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
Expand Down
3 changes: 3 additions & 0 deletions go-staticcheck-mod.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash
cmd=(staticcheck)
. "$(dirname "${0}")/lib/cmd-mod.bash"
3 changes: 3 additions & 0 deletions go-staticcheck-pkg.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash
cmd=(staticcheck)
. "$(dirname "${0}")/lib/cmd-pkg.bash"
3 changes: 3 additions & 0 deletions go-staticcheck-repo-mod.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash
cmd=(staticcheck)
. "$(dirname "${0}")/lib/cmd-repo-mod.bash"
3 changes: 3 additions & 0 deletions go-staticcheck-repo-pkg.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash
cmd=(staticcheck)
. "$(dirname "${0}")/lib/cmd-repo-pkg.bash"
7 changes: 7 additions & 0 deletions sample-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4d78ac2

Please sign in to comment.