Skip to content

Commit

Permalink
removes ./... from my-cmd hooks; adds go mod tidy, gofump, -repo hook…
Browse files Browse the repository at this point in the history
…s for formatters (#15)

* Removes default ./... arg from my-cmd hooks
* Adds support go mod tidy
* Adds support for gofumpt
* Adds -repo hooks for formatters:
  - go-fmt-repo
  - go-frumpt-repo
  - go-imports-repo
  - go-returns-repo 
* Simplifies .sh scripts:
  - Sets "error_on_output=0" globally by default
  - Disable SC2034 (unused args) globally
  - Removes "error_on_output=0" lines
  - Removes "shellcheck disable=SC2034" lines
  - Removes "shellcheck source=" lines

BREAKING CHANGE: my-cmd-mod, my-cmd-mod-repo, and my-cmd-pkg hooks no-longer auto-add ./... argument
  • Loading branch information
TekWizely committed Aug 9, 2021
1 parent 6344f32 commit 1c80b3a
Show file tree
Hide file tree
Showing 51 changed files with 272 additions and 136 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ repos:
files: \.(sh|bash)$
types: [file]
alias: shck
args: [ '-x' ]
args: [ '-x', '-e', 'SC2034' ]
# shfmt
#
- id: shfmt
Expand Down
123 changes: 121 additions & 2 deletions .pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -189,12 +189,62 @@
description: "Run 'gofmt -l -d [$ARGS] $FILE' for each staged .go file"
pass_filenames: true

# ==============================================================================
# go-fmt-repo
# * Repo-Based
# * Recursive
# * Executes if any .go files modified
# NOTES:
# `go fmt` delegates to `gofmt`, so we'll invote `gofmt` directly.
# ==============================================================================
- id: go-fmt-repo
name: 'go-fmt-repo'
entry: go-fmt-repo.sh
types: [go]
exclude: '(^|/)vendor/'
language: 'script'
description: "Run 'gofmt -l -d [$ARGS] .' in repo root folder"
pass_filenames: false

# ==============================================================================
# go-fumpt
# * File-based
# * Executes if any .go files modified
# NOTES:
# Replaces go-fmt
# ==============================================================================
- id: go-fumpt
name: 'go-fumpt'
entry: go-fumpt.sh
types: [go]
exclude: '(^|/)vendor/'
language: 'script'
description: "Run 'fumpt -l -d [$ARGS] $FILE' for each staged .go file"
pass_filenames: true

# ==============================================================================
# go-fumpt-repo
# * Repo-Based
# * Recursive
# * Executes if any .go files modified
# NOTES:
# Replaces go-fmt-repo
# ==============================================================================
- id: go-fumpt-repo
name: 'go-fumpt-repo'
entry: go-fumpt-repo.sh
types: [go]
exclude: '(^|/)vendor/'
language: 'script'
description: "Run 'fumpt -l -d [$ARGS] .' in repo root folder"
pass_filenames: false

# ==============================================================================
# go-imports
# * File-based
# * Executes if any .go files modified
# NOTES:
# Replaces go-fmt-fix
# Replaces go-fmt
# ==============================================================================
- id: go-imports
name: 'go-imports'
Expand All @@ -205,6 +255,23 @@
description: "Run 'goimports -l -d [$ARGS] $FILE' for each staged .go file"
pass_filenames: true

# ==============================================================================
# go-imports-repo
# * Repo-Based
# * Recursive
# * Executes if any .go files modified
# NOTES:
# Replaces go-fmt-repo
# ==============================================================================
- id: go-imports-repo
name: 'go-imports-repo'
entry: go-imports-repo.sh
types: [go]
exclude: '(^|/)vendor/'
language: 'script'
description: "Run 'goimports -l -d [$ARGS] .' in repo root folder"
pass_filenames: false

# ==============================================================================
# go-lint
# * File-based
Expand All @@ -219,12 +286,47 @@
description: "Run 'golint -set_exit_status [$ARGS] $FILE' for each staged .go file"
pass_filenames: true

# ==============================================================================
# go-mod-tidy
# * 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-mod-tidy
name: 'go-mod-tidy'
entry: go-mod-tidy.sh
files: '(\.go$)|(\bgo\.mod$)'
exclude: '(^|/)vendor/'
language: 'script'
description: "Run 'cd $(mod_root $FILE); go mod tidy [$ARGS]' for each staged .go file"
pass_filenames: true
require_serial: true

# ==============================================================================
# go-mod-tidy-repo
# * Repo-Based
# * Recursive
# * Targets ALL folders with a go.mod file
# * Executes if any .go files modified
# * Executes if go.mod modified
# ==============================================================================
- id: go-mod-tidy-repo
name: 'go-mod-tidy-repo'
entry: go-mod-tidy-repo.sh
files: '(\.go$)|(\bgo\.mod$)'
exclude: '(^|/)vendor/'
language: 'script'
description: "Run 'cd $(mod_root); go mod tidy [$ARGS]' for each module in the repo"
pass_filenames: false

# ==============================================================================
# go-returns
# * File-based
# * Executes if any .go files modified
# NOTES:
# Replaces go-imports-fix & go-fmt-fix
# Replaces go-imports & go-fmt
# ==============================================================================
- id: go-returns
name: 'go-returns'
Expand All @@ -235,6 +337,23 @@
description: "Run 'goreturns -l -d [$ARGS] $FILE' for each staged .go file"
pass_filenames: true

# ==============================================================================
# go-returns-repo
# * Repo-Based
# * Recursive
# * Executes if any .go files modified
# NOTES:
# Replaces go-imports-repo & go-fmt-repo
# ==============================================================================
- id: go-returns-repo
name: 'go-returns-repo'
entry: go-returns-repo.sh
types: [go]
exclude: '(^|/)vendor/'
language: 'script'
description: "Run 'goreturns -l -d [$ARGS] .' in repo root folder"
pass_filenames: false

# ==============================================================================
# go-revive
# * File-based
Expand Down
105 changes: 84 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ You can copy/paste the following snippet into your `.pre-commit-config.yaml` fil
- id: go-build-repo-mod
- id: go-build-repo-pkg
#
# Go Mod Tidy
#
- id: go-mod-tidy
- id: go-mod-tidy-repo
#
# Go Test
#
- id: go-test-mod
Expand Down Expand Up @@ -70,8 +75,13 @@ You can copy/paste the following snippet into your `.pre-commit-config.yaml` fil
# Formatters
#
- id: go-fmt
- id: go-imports # replaces go-fmt
- id: go-returns # replaces go-imports & go-fmt
- id: go-fmt-repo
- id: go-fumpt # replaces go-fmt
- id: go-fumpt-repo # replaces go-fmt-repo
- id: go-imports # replaces go-fmt
- id: go-imports-repo # replaces go-fmt-repo
- id: go-returns # replaces go-imports & go-fmt
- id: go-returns-repo # replaces go-imports-repo & go-fmt-repo
#
# Style Checkers
#
Expand All @@ -92,7 +102,7 @@ You can copy/paste the following snippet into your `.pre-commit-config.yaml` fil
#
# Invoking Custom Go Tools
# - Configured *entirely* through the `args` attribute, ie:
# args: [ go, test ]
# args: [ go, test, ./... ]
# - Use the `name` attribute to provide better messaging when the hook runs
# - Use the `alias` attribute to be able invoke your hook via `pre-commit run`
#
Expand Down Expand Up @@ -197,13 +207,16 @@ This can be useful, for example, for hooks that display warnings, but don't gene
--------
## Hooks

- Correctness Checkers
- Build Tools
- [go-build](#go-build)
- [go-mod-tidy](#go-mod-tidy)
- Correctness Checkers
- [go-test](#go-test)
- [go-vet](#go-vet)
- [go-sec](#go-sec)
- Formatters
- [go-fmt](#go-fmt)
- [go-fumpt](#go-fumpt)
- [go-imports](#go-imports)
- [go-returns](#go-returns)
- Style Checkers
Expand Down Expand Up @@ -233,9 +246,25 @@ Comes with Golang ( [golang.org](https://golang.org/) )
- https://golang.org/cmd/go/#hdr-Compile_packages_and_dependencies
- `go help build`

---------------
### go-mod-tidy
Makes sure `go.mod` matches the source code in the module.

| Hook ID | Description
|--------------------|------------
| `go-mod-tidy` | Run `'cd $(mod_root $FILE); go mod tidy [$ARGS] ./...'` for each staged .go file
| `go-mod-tidy-repo` | Run `'cd $(mod_root); go mod tidy [$ARGS] ./...'` for each module in the repo

##### Install
Comes with Golang ( [golang.org](https://golang.org/) )

##### Help
- https://golang.org/ref/mod#go-mod-tidy
- `go mod help tidy`

-----------
### go-test
Automates testing, printing a summary of test resutls.
Automates testing, printing a summary of test results.

| Hook ID | Description
|--------------------|------------
Expand Down Expand Up @@ -299,16 +328,17 @@ Formats Go programs. It uses tabs for indentation and blanks for alignment. Alig

- Can modify files (see `-w`)

| Hook ID | Description
|----------|------------
| `go-fmt` | Run `'gofmt -l -d [$ARGS] $FILE'` for each staged .go file
| Hook ID | Description
|---------------|------------
| `go-fmt` | Run `'gofmt -l -d [$ARGS] $FILE'` for each staged .go file
| `go-fmt-repo` | Run `'gofmt -l -d [$ARGS] .'` in repo root folder

##### Install
Comes with Golang ( [golang.org](https://golang.org/) )

##### Useful Args
```
-d=false : Don't display diffs
-d=false : Hide diffs
-s : Try to simplify code
-w : Update source file directly
```
Expand All @@ -317,16 +347,46 @@ Comes with Golang ( [golang.org](https://golang.org/) )
- https://godoc.org/github.com/golang/go/src/cmd/gofmt
- `gofmt -h`

------------
### go-fumpt
Enforce a stricter format than `gofmt`, while being backwards compatible.

- Replaces `go-fmt`
- Can modify files (see `-w`)

| Hook ID | Description
|-----------------|------------
| `go-fumpt` | Run `'gofumpt -l -d [$ARGS] $FILE'` for each staged .go file
| `go-fumpt-repo` | Run `'gofumpt -l -d [$ARGS] .'` in repo root folder

##### Install (via [bingo](https://github.com/TekWizely/bingo))
```
bingo install mvdan.cc/gofumpt
```

##### Useful Args
```
-d=false : Hide diffs
-extra : Enable extra rules which should be vetted by a human
-s : Try to simplify code
-w : Update source file directly
```

##### Help
- https://pkg.go.dev/mvdan.cc/gofumpt
- `gofumpt -h`

--------------
### go-imports
Updates your Go import lines, adding missing ones and removing unreferenced ones.

- Replaces `go-fmt`
- Can modify files (see `-w`)

| Hook ID | Description
|--------------|------------
| `go-imports` | Run `'goimports -l -d [$ARGS] $FILE'` for each staged .go file
| Hook ID | Description
|-------------------|------------
| `go-imports` | Run `'goimports -l -d [$ARGS] $FILE'` for each staged .go file
| `go-imports-repo` | Run `'goimports -l -d [$ARGS] .'` in repo root folder

##### Install (via [bingo](https://github.com/TekWizely/bingo))
```
Expand Down Expand Up @@ -354,9 +414,10 @@ Implements a Go pretty-printer (like `go-fmt`) that also adds zero-value return
- Replaces `go-fmt` and `go-imports`
- Can modify files (see `-w`)

| Hook ID | Description
|--------------|------------
| `go-returns` | Run `'goreturns -l -d [$ARGS] $FILE'` for each staged .go file
| Hook ID | Description
|-------------------|------------
| `go-returns` | Run `'goreturns -l -d [$ARGS] $FILE'` for each staged .go file
| `go-returns-repo` | Run `'goreturns -l -d [$ARGS] .'` in repo root folder

##### Install (via [bingo](https://github.com/TekWizely/bingo))
```
Expand Down Expand Up @@ -516,15 +577,17 @@ Using the `my-cmd-*` hooks, you can invoke custom go tools in various contexts.
| Hook ID | Description
|-------------------|------------
| `my-cmd` | Run `'$ARGS[0] [$ARGS[1:]] $FILE'` for each staged .go file
| `my-cmd-mod` | Run `'cd $(mod_root $FILE); $ARGS[0] [$ARGS[1:]] ./...'` for each staged .go file
| `my-cmd-pkg` | Run `'$ARGS[0] [$ARGS[1:]] ./$(dirname $FILE)'` for each staged .go file
| `my-cmd-mod` | Run `'cd $(mod_root $FILE); GO111MODULE=on $ARGS[0] [$ARGS[1:]]'` for each staged .go file
| `my-cmd-pkg` | Run `'GO111MODULE=off $ARGS[0] [$ARGS[1:]] ./$(dirname $FILE)'` for each staged .go file
| `my-cmd-repo` | Run `'$ARGS[0] [$ARGS[1:]]'` in the repo root folder
| `my-cmd-repo-mod` | Run `'cd $(mod_root); $ARGS[0] [$ARGS[1:]] /...'` for each module in the repo
| `my-cmd-repo-pkg` | Run `'$ARGS[0] [$ARGS[1:]] ./...'` in repo root folder
| `my-cmd-repo-mod` | Run `'cd $(mod_root); GO111MODULE=on $ARGS[0] [$ARGS[1:]]'` for each module in the repo
| `my-cmd-repo-pkg` | Run `'GO111MODULE=off $ARGS[0] [$ARGS[1:]]` in repo root folder

#### Configuring the hooks

The my-cmd hooks are configured **entirely** through the pre-commit `args` attribute, including specifying which tool to run (ie `$ARGS[0]` above)
The my-cmd hooks are configured **entirely** through the pre-commit `args` attribute, including specifying which tool to run (ie `$ARGS[0]` above).

This includes the need to manually add the `./...` target for module-based tools that require it.

#### Examples

Expand All @@ -538,7 +601,7 @@ _.pre-commit-config.yaml_
- id: my-cmd-mod
name: go-test-mod
alias: go-test-mod
args: [ go, test ]
args: [ go, test, ./... ]
```

##### Names & Aliases
Expand Down
3 changes: 0 additions & 3 deletions go-build-mod.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
#!/usr/bin/env bash
# shellcheck disable=SC2034 # vars used by sourced script
error_on_output=0
cmd=(go build -o /dev/null)
# shellcheck source=lib/cmd-mod.bash
. "$(dirname "${0}")/lib/cmd-mod.bash"
3 changes: 0 additions & 3 deletions go-build-pkg.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
#!/usr/bin/env bash
# shellcheck disable=SC2034 # vars used by sourced script
error_on_output=0
cmd=(go build -o /dev/null)
# shellcheck source=lib/cmd-pkg.bash
. "$(dirname "${0}")/lib/cmd-pkg.bash"
3 changes: 0 additions & 3 deletions go-build-repo-mod.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
#!/usr/bin/env bash
# shellcheck disable=SC2034 # vars used by sourced script
error_on_output=0
cmd=(go build -o /dev/null)
# shellcheck source=lib/cmd-repo-mod.bash
. "$(dirname "${0}")/lib/cmd-repo-mod.bash"
3 changes: 0 additions & 3 deletions go-build-repo-pkg.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
#!/usr/bin/env bash
# shellcheck disable=SC2034 # vars used by sourced script
error_on_output=0
cmd=(go build -o /dev/null)
# shellcheck source=lib/cmd-repo-pkg.bash
. "$(dirname "${0}")/lib/cmd-repo-pkg.bash"
Loading

0 comments on commit 1c80b3a

Please sign in to comment.