Skip to content

Commit a38ec6c

Browse files
committed
feat(go-tool) Support for Go Tool Invocation (#47)
feat: Adds --hook:go-tool* arguments to invoke a hook's command via 'go tool' feat: Adds go-hook* hooks to explicitly invoke go tool with custom arguments docs: Updates README.md and sample-config.yaml for go-tool support fix: Small bug in prepare-my-cmd.bash chore: Run pre-commit hooks and fix shellcheck/shfmt warnings
1 parent f298c08 commit a38ec6c

16 files changed

Lines changed: 270 additions & 6 deletions

.pre-commit-hooks.yaml

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,70 @@
674674
description: "Run 'go test [$ARGS] ./...' in repo root folder"
675675
pass_filenames: false
676676

677+
# ==============================================================================
678+
# go-tool
679+
# * File-based
680+
# * Executes if any .go files modified
681+
# ==============================================================================
682+
- id: go-tool
683+
name: 'go-tool'
684+
entry: go-tool.sh
685+
types: [go]
686+
exclude: '(^|/)vendor/'
687+
language: 'script'
688+
description: "Run 'go tool [$ARGS] $FILE' for each staged .go file"
689+
pass_filenames: true
690+
691+
# ==============================================================================
692+
# go-tool-mod
693+
# * Folder-Based
694+
# * Recursive
695+
# * Targets first parent folder with a go.mod file
696+
# * Executes if any .go files modified
697+
# * Executes if go.mod modified
698+
# ==============================================================================
699+
- id: go-tool-mod
700+
name: 'go-tool-mod'
701+
entry: go-tool-mod.sh
702+
files: '(\.go$)|(\bgo\.mod$)'
703+
exclude: '(^|/)vendor/'
704+
language: 'script'
705+
description: "Run 'cd $(mod_root $FILE); go tool [$ARGS]' for each staged .go file"
706+
pass_filenames: true
707+
require_serial: true
708+
709+
# ==============================================================================
710+
# go-tool-repo
711+
# * Repo-Based
712+
# * Recursive
713+
# * Executes if any .go files modified
714+
# ==============================================================================
715+
- id: go-tool-repo
716+
name: 'go-tool-repo'
717+
entry: go-tool-repo.sh
718+
types: [go]
719+
exclude: '(^|/)vendor/'
720+
language: 'script'
721+
description: "Run 'go tool [$ARGS]' in the repo root folder"
722+
pass_filenames: false
723+
724+
# ==============================================================================
725+
# go-tool-repo-mod
726+
# * Repo-Based
727+
# * Recursive
728+
# * Targets ALL folders with a go.mod file
729+
# * Executes if any .go files modified
730+
# * Executes if go.mod modified
731+
# ==============================================================================
732+
- id: go-tool-repo-mod
733+
name: 'go-tool-repo-mod'
734+
entry: go-tool-repo-mod.sh
735+
files: '(\.go$)|(\bgo\.mod$)'
736+
exclude: '(^|/)vendor/'
737+
language: 'script'
738+
description: "Run 'cd $(mod_root); go tool [$ARGS]' for each module in the repo"
739+
pass_filenames: false
740+
677741
# ==============================================================================
678742
# go-vet-mod
679743
# * Folder-Based

README.md

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ You can copy/paste the following snippet into your `.pre-commit-config.yaml` fil
3131
#
3232
# --hook:env:NAME=VALUE
3333
#
34+
# NOTE: You can invoke hooks via 'go tool' using:
35+
#
36+
# --hook:go-tool
37+
# --hook:go-tool-mod=MODFILE
38+
# --hook:go-tool-arg:ARG
39+
#
3440
# Consider adding aliases to longer-named hooks for easier CLI usage.
3541
# ==========================================================================
3642
- repo: https://github.com/tekwizely/pre-commit-golang
@@ -45,6 +51,13 @@ You can copy/paste the following snippet into your `.pre-commit-config.yaml` fil
4551
- id: go-build-repo-mod
4652
- id: go-build-repo-pkg
4753
#
54+
# Go Tool (Go 1.24+)
55+
#
56+
- id: go-tool
57+
- id: go-tool-mod
58+
- id: go-tool-repo
59+
- id: go-tool-repo-mod
60+
#
4861
# Go Mod Tidy
4962
#
5063
- id: go-mod-tidy
@@ -234,6 +247,33 @@ You can pass multiple `--hook:env:` arguments.
234247
235248
The arguments can appear anywhere in the `args:` list.
236249
250+
#### Invoking Hooks Via go tool
251+
You can invoke hooks via `go tool` to ensure that a specific version of a tool is used (requires Go 1.24+).
252+
253+
This feature is enabled via support for several specially-formatted arguments:
254+
255+
* `--hook:go-tool` : Invoke the hook via `go tool <hook> ...`
256+
* `--hook:go-tool-mod=FILE` : Pass `-modfile=FILE` to the `go tool` invocation.
257+
* `--hook:go-tool-arg:ARG` : Pass `ARG` to the `go tool` invocation (can be repeated).
258+
259+
The hook script will detect these arguments and invoke the configured tool via `go tool`.
260+
261+
**NOTE:** When using `go-tool-mod` or `go-tool-arg` flags, you do not need to also provide the base `go-tool` flag; their presence triggers the use of the `go tool` invocation implicitly.
262+
263+
The arguments can appear anywhere in the `args:` list (before the optional `--`).
264+
265+
**NOTE:** The plain versions of these arguments (without the `hook:` prefix) are also supported, but **ONLY** if they appear at the very beginning of the `args:` list:
266+
267+
* `--go-tool`
268+
* `--go-tool-mod=FILE`
269+
* `--go-tool-arg:ARG`
270+
271+
For more information:
272+
* [Go v1.24 Tools Announcement](https://tip.golang.org/doc/go1.24#tools)
273+
* [Go Tools Documentation](https://tip.golang.org/doc/modules/managing-dependencies#tools)
274+
275+
See the [go-tool](#go-tool) hooks to invoke `go tools` directly with custom arguments.
276+
237277
#### Passing Ignore Patterns To Hooks
238278
239279
You can pass arguments to hooks to specify files / directories to ignore.
@@ -404,6 +444,7 @@ This can be useful, for example, for hooks that display warnings, but don't gene
404444
405445
- Build Tools
406446
- [go-build](#go-build)
447+
- [go-tool](#go-tool)
407448
- [go-mod-tidy](#go-mod-tidy)
408449
- Correctness Checkers
409450
- [go-test](#go-test)
@@ -444,6 +485,34 @@ Comes with Golang ( [golang.org](https://golang.org/) )
444485
- https://golang.org/cmd/go/#hdr-Compile_packages_and_dependencies
445486
- `go help build`
446487
488+
-----------
489+
### go-tool
490+
Runs the go tool command identified by the provided arguments.
491+
492+
| Hook ID | Description |
493+
|---------------------|--------------------------------------------------------------------------|
494+
| `go-tool` | Run `'go tool [$ARGS] $FILE'` for each staged .go file |
495+
| `go-tool-mod` | Run `'cd $(mod_root $FILE); go tool [$ARGS]'` for each staged .go file |
496+
| `go-tool-repo` | Run `'go tool [$ARGS]'` in the repo root folder |
497+
| `go-tool-repo-mod` | Run `'cd $(mod_root); go tool [$ARGS]'` for each module in the repo |
498+
499+
Go ships with a number of builtin tools, and additional tools may be defined in the go.mod of the current module.
500+
501+
##### See Also
502+
See [Invoking Hooks Via go tool](#invoking-hooks-via-go-tool) to invoke existing hooks via `go tool`.
503+
504+
##### Install
505+
Comes with Golang ( [golang.org](https://golang.org/) )
506+
507+
##### Useful Args
508+
```
509+
-modfile=file.mod : Use an alternate mod file (default: module's go.mod)
510+
```
511+
512+
##### Help
513+
- https://go.dev/doc/go1.24#tools
514+
- `go help tool`
515+
447516
---------------
448517
### go-mod-tidy
449518
Makes sure `go.mod` matches the source code in the module.

go-revive-mod.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env bash
22
cmd=(revive)
33
if [ -f "revive.toml" ]; then
4-
cmd+=( "-config=revive.toml" )
4+
cmd+=("-config=revive.toml")
55
fi
66
. "$(dirname "${0}")/lib/cmd-mod.bash"

go-revive-repo-mod.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env bash
22
cmd=(revive)
33
if [ -f "revive.toml" ]; then
4-
cmd+=( "-config=revive.toml" )
4+
cmd+=("-config=revive.toml")
55
fi
66
. "$(dirname "${0}")/lib/cmd-repo-mod.bash"

go-tool-mod.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env bash
2+
cmd=(go tool)
3+
. "$(dirname "${0}")/lib/cmd-mod.bash"

go-tool-repo-mod.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env bash
2+
cmd=(go tool)
3+
. "$(dirname "${0}")/lib/cmd-repo-mod.bash"

go-tool-repo.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env bash
2+
cmd=(go tool)
3+
. "$(dirname "${0}")/lib/cmd-repo.bash"

go-tool.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env bash
2+
cmd=(go tool)
3+
. "$(dirname "${0}")/lib/cmd-files.bash"

lib/cmd-files.bash

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
# shellcheck source=./common.bash
44
. "$(dirname "${0}")/lib/common.bash"
55

6+
# shellcheck source=./prepare-go-tool.bash
7+
. "$(dirname "${0}")/lib/prepare-go-tool.bash"
8+
69
prepare_file_hook_cmd "$@"
710

811
error_code=0

lib/cmd-mod.bash

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
# shellcheck source=./common.bash
66
. "$(dirname "${0}")/lib/common.bash"
77

8+
# shellcheck source=./prepare-go-tool.bash
9+
. "$(dirname "${0}")/lib/prepare-go-tool.bash"
10+
811
prepare_file_hook_cmd "$@"
912

1013
if [ "${use_dot_dot_dot:-}" -eq 1 ]; then

0 commit comments

Comments
 (0)