Skip to content

Commit

Permalink
feat(revive): Adds built-in support for repo-level revive.toml (#20)
Browse files Browse the repository at this point in the history
Modifies revive hooks:

* Auto-adds '-config=revive.toml' argument if file is present in repository root
* Triggers hooks when repo-level revive.toml is modified
* Updates README

NOTE: Although the file-based go-revive hook is configured to trigger when revive.toml is modified, it will essentially do nothing if there are no .go files staged. We might consider adding some logic to run against ALL .go files in the repo, but I'm really not sure thats a good idea, and I probably didn't need to add a trigger to the file-level hook at all.
  • Loading branch information
TekWizely committed Oct 16, 2021
1 parent 4d78ac2 commit bd69b81
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 10 deletions.
13 changes: 10 additions & 3 deletions .pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -358,11 +358,14 @@
# go-revive
# * File-based
# * Executes if any .go files modified
# * Executes if revive.toml modified
# * Adds arg '-config=revive.toml' if present
# NOTE: Does nothing if ONLY revive.toml is modified (ie no .go files modified)
# ==============================================================================
- id: go-revive
name: 'go-revive'
entry: go-revive.sh
types: [go]
files: '(\.go$)|(\brevive\.toml$)'
exclude: '(^|/)vendor/'
language: 'script'
description: "Run 'revive [$ARGS] $FILE' for each staged .go file"
Expand All @@ -375,11 +378,13 @@
# * Targets first parent folder with a go.mod file
# * Executes if any .go files modified
# * Executes if go.mod modified
# * Executes if revive.toml modified
# * Adds arg '-config=revive.toml' if present
# ==============================================================================
- id: go-revive-mod
name: 'go-revive-mod'
entry: go-revive-mod.sh
files: '(\.go$)|(\bgo\.mod$)'
files: '(\.go$)|(\bgo\.mod$)|(\brevive\.toml$)'
exclude: '(^|/)vendor/'
language: 'script'
description: "Run 'cd $(mod_root $FILE); revive [$ARGS] ./...' for each staged .go file"
Expand All @@ -393,11 +398,13 @@
# * Targets ALL folders with a go.mod file
# * Executes if any .go files modified
# * Executes if go.mod modified
# * Executes if revive.toml modified
# * Adds arg '-config=revive.toml' if present
# ==============================================================================
- id: go-revive-repo-mod
name: 'go-revive-repo-mod'
entry: go-revive-repo-mod.sh
files: '(\.go$)|(\bgo\.mod$)'
files: '(\.go$)|(\bgo\.mod$)|(\brevive\.toml$)'
exclude: '(^|/)vendor/'
language: 'script'
description: "Run 'cd $(mod_root); revive [$ARGS] ./...' for each module in the repo"
Expand Down
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -532,14 +532,27 @@ bingo install golang.org/x/lint/golint
-------------
### go-revive
~6x faster, stricter, configurable, extensible, and beautiful drop-in replacement for golint.
\~6x faster, stricter, configurable, extensible, and beautiful drop-in replacement for golint.
| Hook ID | Description
|-----------|------------
| `go-revive` | Run `'revive [$ARGS] $FILE'` for each staged .go file
| `go-revive-mod` | Run `'cd $(mod_root $FILE); revive [$ARGS] ./...'` for each staged .go file
| `go-revive-repo-mod` | Run `'cd $(mod_root); revive [$ARGS] ./...'` for each module in the repo
##### Support for Repository-Level Config
As of time of writing, revive only auto-checks for configs in `${HOME}/revive.toml`, and doesn't check the local folder (ie. `${REPO_ROOT}/revive.toml`).
To make revive more useful, these hooks add built-in support for a repository-level config file.
###### Auto-Configured
These hooks are configured to auto-add `-config=revive.toml` when the file is present in the repository root.
###### Triggerred When Modified
These hooks are configured to run when the repo-level `revive.toml` file is modified (and staged).
**NOTE:** Although configured to run, the file-based `go-revive` hook will, by default, effectively _do nothing_ if there are no staged `.go` files to run against.
##### Install (via [bingo](https://github.com/TekWizely/bingo))
```
bingo install github.com/mgechev/revive
Expand Down
3 changes: 3 additions & 0 deletions go-revive-mod.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#!/usr/bin/env bash
cmd=(revive)
if [ -f "revive.toml" ]; then
cmd+=( "-config=revive.toml" )
fi
. "$(dirname "${0}")/lib/cmd-mod.bash"
3 changes: 3 additions & 0 deletions go-revive-repo-mod.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#!/usr/bin/env bash
cmd=(revive)
if [ -f "revive.toml" ]; then
cmd+=( "-config=revive.toml" )
fi
. "$(dirname "${0}")/lib/cmd-repo-mod.bash"
4 changes: 4 additions & 0 deletions go-revive.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
#!/usr/bin/env bash
cmd=(revive)
if [ -f "revive.toml" ]; then
cmd+=( "-config=revive.toml" )
fi
ignore_file_pattern_array=( "revive.toml" )
. "$(dirname "${0}")/lib/cmd-files.bash"
16 changes: 10 additions & 6 deletions lib/common.bash
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

: "${use_dot_dot_dot:=1}"
: "${error_on_output:=0}"
: "${ignore_file_pattern_array:=}"

##
# prepare_repo_hook_cmd
Expand Down Expand Up @@ -38,7 +39,7 @@ function verify_hook_cmd {
# parse_file_hook_args
# Creates global vars:
# OPTIONS: List of options to passed to comand
# FILES : List of files to process
# FILES : List of files to process, filtered against ignore_file_pattern_array
#
function parse_file_hook_args {
OPTIONS=()
Expand Down Expand Up @@ -72,14 +73,17 @@ function parse_file_hook_args {
#
all_files+=("$@")

# Filter out vendor entries
# Filter out vendor entries and ignore_file_pattern_array
#
FILES=()
local file
local file pattern
ignore_file_pattern_array+=( "vendor/*" "*/vendor/*" "*/vendor" )
for file in "${all_files[@]}"; do
if [[ "${file}" == "vendor/"* || "${file}" == *"/vendor/"* || "${file}" == *"/vendor" ]]; then
continue
fi
for pattern in "${ignore_file_pattern_array[@]}"; do
if [[ "${file}" == ${pattern} ]] ; then # pattern => unquoted
continue 2
fi
done
FILES+=("${file}")
done
}
Expand Down

0 comments on commit bd69b81

Please sign in to comment.