Skip to content

Commit a701af7

Browse files
authored
Adds Hook for Revive Linter (#10)
1 parent d09b682 commit a701af7

6 files changed

+242
-0
lines changed

.pre-commit-hooks.yaml

+49
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,55 @@
140140
description: "Run 'goreturns -l -d [$ARGS] $FILE' for each staged .go file"
141141
pass_filenames: true
142142

143+
# ==============================================================================
144+
# go-revive
145+
# * File-based
146+
# * Executes if any .go files modified
147+
# ==============================================================================
148+
- id: go-revive
149+
name: 'go-revive'
150+
entry: go-revive.sh
151+
types: [go]
152+
exclude: '(^|/)vendor/'
153+
language: 'script'
154+
description: "Run 'revive [$ARGS] $FILE' for each staged .go file"
155+
pass_filenames: true
156+
157+
# ==============================================================================
158+
# go-revive-mod
159+
# * Folder-Based
160+
# * Recursive
161+
# * Targets first parent folder with a go.mod file
162+
# * Executes if any .go files modified
163+
# * Executes if go.mod modified
164+
# ==============================================================================
165+
- id: go-revive-mod
166+
name: 'go-revive-mod'
167+
entry: go-revive-mod.sh
168+
files: '(\.go$)|(\bgo\.mod$)'
169+
exclude: '(^|/)vendor/'
170+
language: 'script'
171+
description: "Run 'cd $(mod_root $FILE); revive [$ARGS] ./...' for each staged .go file"
172+
pass_filenames: true
173+
require_serial: true
174+
175+
# ==============================================================================
176+
# go-revive-repo-mod
177+
# * Repo-Based
178+
# * Recursive
179+
# * Targets ALL folders with a go.mod file
180+
# * Executes if any .go files modified
181+
# * Executes if go.mod modified
182+
# ==============================================================================
183+
- id: go-revive-repo-mod
184+
name: 'go-revive-repo-mod'
185+
entry: go-revive-repo-mod.sh
186+
files: '(\.go$)|(\bgo\.mod$)'
187+
exclude: '(^|/)vendor/'
188+
language: 'script'
189+
description: "Run 'cd $(mod_root); revive [$ARGS] ./...' for each module in the repo"
190+
pass_filenames: false
191+
143192
# ==============================================================================
144193
# go-sec-mod
145194
# * Folder-Based

README.md

+44
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ You can copy/paste the following snippet into your `.pre-commit-config.yaml` fil
5454
- id: go-vet-repo-mod
5555
- id: go-vet-repo-pkg
5656
#
57+
# Revive
58+
#
59+
- id: go-revive
60+
- id: go-revive-mod
61+
- id: go-revive-repo-mod
62+
#
5763
# GoSec
5864
#
5965
- id: go-sec-mod
@@ -137,6 +143,7 @@ For file and repo-based hooks, this isn't an issue, but for module and package-b
137143
args: [arg1, arg2, ..., '--'] # Pass options ('--' is optional)
138144
always_run: true # Run even if no matching files staged
139145
alias: hook-alias # Create an alias
146+
verbose: true # Display output, even if no errors
140147
```
141148
142149
#### Passing Options To Hooks
@@ -160,6 +167,11 @@ When configured to `"always_run"`, a hook is executed as if EVERY matching file
160167
#### Aliases
161168
Consider adding aliases to longer-named hooks for easier CLI usage.
162169
170+
#### Verbose Hook Output
171+
When the `"verbose"` flag is enabled, all output generated by the hook will be displayed, even if there were no errors.
172+
173+
This can be useful, for example, for hooks that display warnings, but don't generate error codes for them.
174+
163175
--------
164176
## Hooks
165177
@@ -175,6 +187,7 @@ Consider adding aliases to longer-named hooks for easier CLI usage.
175187
- Style Checkers
176188
- [go-lint](#go-lint)
177189
- [go-critic](#go-critic)
190+
- [go-revive](#go-revive)
178191
- GolangCI-Lint
179192
- [golangci-lint](#golangci-lint)
180193
@@ -361,6 +374,37 @@ go get -u golang.org/x/lint/golint
361374
- https://golang.org/doc/effective_go.html
362375
- https://golang.org/wiki/CodeReviewComments
363376
377+
-------------
378+
### go-revive
379+
~6x faster, stricter, configurable, extensible, and beautiful drop-in replacement for golint.
380+
381+
| Hook ID | Description
382+
|-----------|------------
383+
| `go-revive` | Run `'revive [$ARGS] $FILE'` for each staged .go file
384+
| `go-revive-mod` | Run `'cd $(mod_root $FILE); revive [$ARGS] ./...'` for each staged .go file
385+
| `go-revive-repo-mod` | Run `'cd $(mod_root); revive [$ARGS] ./...'` for each module in the repo
386+
387+
##### Install
388+
```
389+
go get -u github.com/mgechev/revive
390+
```
391+
392+
##### Useful Args
393+
```
394+
-config [PATH] : Path to config file (TOML)
395+
-exclude [PATTERN] : Pattern for files/directories/packages to be excluded from linting
396+
-formatter [NAME] : formatter to be used for the output
397+
```
398+
399+
##### Displaying Warnings
400+
By default, `revive` doesn't generate errors on warnings, so warning messages may not be displayed if there are no accompanying error messages.
401+
402+
You can use the `"verbose: true"` hook configuration to always show hook output.
403+
404+
##### Help
405+
- https://github.com/mgechev/revive#usage
406+
- `revive -h`
407+
364408
-------------
365409
### go-critic
366410
The most opinionated Go source code linter for code audit.

go-revive-mod.sh

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#!/usr/bin/env bash
2+
3+
cmd=(revive)
4+
5+
export GO111MODULE=on
6+
7+
# Walks up the file path looking for go.mod
8+
#
9+
function find_module_roots() {
10+
for arg in "$@" ; do
11+
local path="${arg}"
12+
if [ "${path}" == "" ]; then
13+
path="."
14+
elif [ -f "${path}" ]; then
15+
path=$(dirname "${path}")
16+
fi
17+
while [ "${path}" != "." ] && [ ! -f "${path}/go.mod" ]; do
18+
path=$(dirname "${path}")
19+
done
20+
if [ -f "${path}/go.mod" ]; then
21+
echo "${path}"
22+
fi
23+
done
24+
}
25+
26+
OPTIONS=()
27+
# If arg doesn't pass [ -f ] check, then it is assumed to be an option
28+
#
29+
while [ $# -gt 0 ] && [ "$1" != "-" ] && [ "$1" != "--" ] && [ ! -f "$1" ]; do
30+
OPTIONS+=("$1")
31+
shift
32+
done
33+
34+
FILES=()
35+
# Assume start of file list (may still be options)
36+
#
37+
while [ $# -gt 0 ] && [ "$1" != "-" ] && [ "$1" != "--" ]; do
38+
FILES+=("$1")
39+
shift
40+
done
41+
42+
# If '--' next, then files = options
43+
#
44+
if [ $# -gt 0 ]; then
45+
if [ "$1" == "-" ] || [ "$1" == "--" ]; then
46+
shift
47+
# Append to previous options
48+
#
49+
OPTIONS=("${OPTIONS[@]}" "${FILES[@]}")
50+
FILES=()
51+
fi
52+
fi
53+
54+
# Any remaining arguments are assumed to be files
55+
#
56+
while [ $# -gt 0 ]; do
57+
FILES+=("$1")
58+
shift
59+
done
60+
61+
errCode=0
62+
for sub in $(find_module_roots "${FILES[@]}" | sort -u) ; do
63+
pushd "${sub}" >/dev/null
64+
"${cmd[@]}" "${OPTIONS[@]}" ./...
65+
if [ $? -ne 0 ]; then
66+
errCode=1
67+
fi
68+
popd >/dev/null
69+
done
70+
exit $errCode

go-revive-repo-mod.sh

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env bash
2+
3+
cmd=(revive)
4+
5+
export GO111MODULE=on
6+
7+
OPTIONS=()
8+
# Build options list, ignoring '-', '--', and anything after
9+
#
10+
while [ $# -gt 0 ] && [ "$1" != "-" ] && [ "$1" != "--" ]; do
11+
OPTIONS+=("$1")
12+
shift
13+
done
14+
15+
errCode=0
16+
# Assume parent folder of go.mod is module root folder
17+
#
18+
for sub in $(find . -name go.mod -not -path '*/vendor/*' | xargs -n1 dirname | sort -u) ; do
19+
pushd "${sub}" >/dev/null
20+
"${cmd[@]}" "${OPTIONS[@]}" ./...
21+
if [ $? -ne 0 ]; then
22+
errCode=1
23+
fi
24+
popd >/dev/null
25+
done
26+
exit $errCode

go-revive.sh

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/usr/bin/env bash
2+
3+
cmd=(revive)
4+
5+
OPTIONS=()
6+
# If arg doesn't pass [ -f ] check, then it is assumed to be an option
7+
#
8+
while [ $# -gt 0 ] && [ "$1" != "-" ] && [ "$1" != "--" ] && [ ! -f "$1" ]; do
9+
OPTIONS+=("$1")
10+
shift
11+
done
12+
13+
FILES=()
14+
# Assume start of file list (may still be options)
15+
#
16+
while [ $# -gt 0 ] && [ "$1" != "-" ] && [ "$1" != "--" ]; do
17+
FILES+=("$1")
18+
shift
19+
done
20+
21+
# If '--' next, then files = options
22+
#
23+
if [ $# -gt 0 ]; then
24+
if [ "$1" == "-" ] || [ "$1" == "--" ]; then
25+
shift
26+
# Append to previous options
27+
#
28+
OPTIONS=("${OPTIONS[@]}" "${FILES[@]}")
29+
FILES=()
30+
fi
31+
fi
32+
33+
# Any remaining arguments are assumed to be files
34+
#
35+
while [ $# -gt 0 ]; do
36+
FILES+=("$1")
37+
shift
38+
done
39+
40+
errCode=0
41+
for file in "${FILES[@]}"; do
42+
"${cmd[@]}" "${OPTIONS[@]}" "${file}"
43+
if [ $? -ne 0 ]; then
44+
errCode=1
45+
fi
46+
done
47+
exit $errCode

sample-config.yaml

+6
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,12 @@ repos:
9797
- id: go-vet-repo-mod
9898
- id: go-vet-repo-pkg
9999
#
100+
# Revive
101+
#
102+
- id: go-revive
103+
- id: go-revive-mod
104+
- id: go-revive-repo-mod
105+
#
100106
# GoSec
101107
#
102108
- id: go-sec-mod

0 commit comments

Comments
 (0)