You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
use lib/; my-cmd hooks; use printf; more vendor filtering (#13)
* Refactored to use include files, removing duplicate code
* Adds my-cmd hooks to invoke custom tools
* Replaces all usages of echo with printf
* Adds more vendor filtering
* Updates project description
* Reverts copy/pasta in go-test hook docs added in gosec commit
* Adds shellcheck hook to lint project files
* Adds shfmt hook to normalize formatting in project files
* Updates copyright year in LICENSE file
A set of git pre-commit hooks for Golang with support for Modules.
3
+
A set of git pre-commit hooks for Golang with support for multi-module monorepos, the ability to pass arguments to all hooks, and the ability to invoke custom go tools.
4
4
5
-
Requires the [Pre-Commit.com](https://pre-commit.com) Hook Management framework.
5
+
Requires the [Pre-Commit.com](https://pre-commit.com) Hook Management Framework.
6
6
7
7
---------------
8
8
## Installation
@@ -89,6 +89,19 @@ You can copy/paste the following snippet into your `.pre-commit-config.yaml` fil
89
89
- id: golangci-lint-pkg
90
90
- id: golangci-lint-repo-mod
91
91
- id: golangci-lint-repo-pkg
92
+
#
93
+
# Invoking Custom Go Tools
94
+
# - Configured *entirely* through the `args` attribute, ie:
95
+
# args: [ go, test ]
96
+
# - Use the `name` attribute to provide better messaging when the hook runs
97
+
# - Use the `alias` attribute to be able invoke your hook via `pre-commit run`
98
+
#
99
+
- id: my-cmd
100
+
- id: my-cmd-mod
101
+
- id: my-cmd-pkg
102
+
- id: my-cmd-repo
103
+
- id: my-cmd-repo-mod
104
+
- id: my-cmd-repo-pkg
92
105
```
93
106
94
107
-----------
@@ -127,6 +140,7 @@ Hooks have suffixes in their name that indicate their targets:
| `-repo-mod` | All Modules | Targets all module root folders in the repo |
131
145
| `-repo-pkg` | All Packages | Targets all package folders in the repo |
132
146
@@ -136,6 +150,14 @@ Due to OS command-line-length limits, Pre-Commit can invoke a hook multiple time
136
150
137
151
For file and repo-based hooks, this isn't an issue, but for module and package-based hooks, there is a potential for the hook to run against the same module or package multiple times, duplicating any errors or warnings.
138
152
153
+
-------------------------
154
+
### Invoking Custom Tools
155
+
While this project includes builtin hooks for many popular go tools, it's not possible to include builtin hooks for every tool that users might want to use.
156
+
157
+
To help accommodate those users, this project includes the ability to invoke custom go tools.
158
+
159
+
See the [my-cmd](#my-cmd) hooks for more information.
160
+
139
161
--------------------------
140
162
### Useful Hook Parameters
141
163
```
@@ -190,6 +212,8 @@ This can be useful, for example, for hooks that display warnings, but don't gene
190
212
- [go-revive](#go-revive)
191
213
- GolangCI-Lint
192
214
- [golangci-lint](#golangci-lint)
215
+
- Invoking Custom Tools
216
+
- [my-cmd](#my-cmd)
193
217
194
218
------------
195
219
### go-build
@@ -215,10 +239,10 @@ Automates testing, printing a summary of test resutls.
215
239
216
240
| Hook ID | Description
217
241
|--------------------|------------
218
-
| `go-test-mod` | Run `'cd $(mod_root $FILE); gosec [$ARGS] ./...'` for each staged .go file
219
-
| `go-test-pkg` | Run `'gosec [$ARGS] ./$(dirname $FILE)'` for each staged .go file
220
-
| `go-test-repo-mod` | Run `'cd $(mod_root); gosec [$ARGS] ./...'` for each module in the repo
221
-
| `go-test-repo-pkg` | Run `'gosec [$ARGS] ./...'` in repo root folder
242
+
| `go-test-mod` | Run `'cd $(mod_root $FILE); go test [$ARGS] ./...'` for each staged .go file
243
+
| `go-test-pkg` | Run `'go test [$ARGS] ./$(dirname $FILE)'` for each staged .go file
244
+
| `go-test-repo-mod` | Run `'cd $(mod_root); go test [$ARGS] ./...'` for each module in the repo
245
+
| `go-test-repo-pkg` | Run `'go test [$ARGS] ./...'` in repo root folder
222
246
223
247
##### Install
224
248
Comes with Golang ( [golang.org](https://golang.org/) )
Using the `my-cmd-*` hooks, you can invoke custom go tools in various contexts.
515
+
516
+
| Hook ID | Description
517
+
|-------------------|------------
518
+
| `my-cmd` | Run `'$ARGS[0] [$ARGS[1:]] $FILE'` for each staged .go file
519
+
| `my-cmd-mod` | Run `'cd $(mod_root $FILE); $ARGS[0] [$ARGS[1:]] ./...'` for each staged .go file
520
+
| `my-cmd-pkg` | Run `'$ARGS[0] [$ARGS[1:]] ./$(dirname $FILE)'` for each staged .go file
521
+
| `my-cmd-repo` | Run `'$ARGS[0] [$ARGS[1:]]'` in the repo root folder
522
+
| `my-cmd-repo-mod` | Run `'cd $(mod_root); $ARGS[0] [$ARGS[1:]] /...'` for each module in the repo
523
+
| `my-cmd-repo-pkg` | Run `'$ARGS[0] [$ARGS[1:]] ./...'` in repo root folder
524
+
525
+
#### Configuring the hooks
526
+
527
+
The my-cmd hooks are configured **entirely** through the pre-commit `args` attribute, including specifying which tool to run (ie `$ARGS[0]` above)
528
+
529
+
#### Examples
530
+
531
+
Here's an example of what it would look like to use the my-cmd hooks to invoke `go test` if it wasn't already included:
532
+
533
+
_.pre-commit-config.yaml_
534
+
```
535
+
# ...
536
+
hooks:
537
+
# Run 'cd $(mod_root $FILE); go test ./...' for each staged .go file
538
+
- id: my-cmd-mod
539
+
name: go-test-mod
540
+
alias: go-test-mod
541
+
args: [ go, test ]
542
+
```
543
+
544
+
##### Names & Aliases
545
+
546
+
It is recommended that you use both `name` and `alias` attributes when defining my-cmd hooks.
547
+
548
+
The name will provide better messaging when the hook runs.
549
+
550
+
The alias will enable you to invoke the hook manually from the command-line when needed (see `pre-commit help run`)
551
+
552
+
##### error-on-output
553
+
554
+
Some tools, like `gofmt`, `goimports`, and `goreturns`, don't generate error codes, but instead expect the presence of any output to indicate warning/error conditions.
555
+
556
+
The my-cmd hooks accept an `--error-on-output` argument to indicate this behavior.
557
+
558
+
Here's an example of what it would look like to use the my-cmd hooks to invoke `gofmt` if it wasn't already included:
559
+
560
+
_.pre-commit-config.yaml_
561
+
```
562
+
# ...
563
+
hooks:
564
+
# Run 'gofmt -l -d $FILE' for each staged .go file
565
+
# Treat any output as indication of failure
566
+
- id: my-cmd
567
+
name: go-fmt
568
+
alias: go-fmt
569
+
args: [ --error-on-output, gofmt, -l, -d ]
570
+
```
571
+
572
+
**NOTE:** When used, the `--error-on-output` option **must** be the first argument.
0 commit comments