From 7a9cc10c39fefe26405aef6cf20887abce190004 Mon Sep 17 00:00:00 2001 From: "TekWize.ly" Date: Tue, 2 Jul 2019 12:42:27 -0700 Subject: [PATCH] Added module-level hooks for build, test, vet, golang-ci --- .pre-commit-hooks.yaml | 76 ++++++++++++++++++++++++++++++++++++++++ README.md | 15 ++++++-- go-build-mod.sh | 29 +++++++++++++++ go-test-mod.sh | 29 +++++++++++++++ go-vet-mod.sh | 29 +++++++++++++++ golangci-lint-mod-fix.sh | 29 +++++++++++++++ golangci-lint-mod.sh | 29 +++++++++++++++ sample-config.yaml | 9 ++++- 8 files changed, 242 insertions(+), 3 deletions(-) create mode 100755 go-build-mod.sh create mode 100755 go-test-mod.sh create mode 100755 go-vet-mod.sh create mode 100755 golangci-lint-mod-fix.sh create mode 100755 golangci-lint-mod.sh diff --git a/.pre-commit-hooks.yaml b/.pre-commit-hooks.yaml index 18a22a8..65b1b41 100644 --- a/.pre-commit-hooks.yaml +++ b/.pre-commit-hooks.yaml @@ -1,3 +1,18 @@ +# ============================================================================== +# go-build-mod +# * Directory-Based +# * Targets first parent folder with a go.mod file +# * Executes if any .go files modified +# ============================================================================== +- id: go-build-mod + name: 'go-build-mod' + entry: go-build-mod.sh + types: [go] + language: 'script' + description: "Run 'cd $(mod_root $FILE); go build ./...' for each staged .go file" + pass_filenames: true + require_serial: true + # ============================================================================== # go-build-dir # * Directory-Based @@ -67,6 +82,21 @@ description: "Run 'go build $(go list)/...' in repo root folder" pass_filenames: false +# ============================================================================== +# go-test-mod +# * Directory-Based +# * Targets first parent folder with a go.mod file +# * Executes if any .go files modified +# ============================================================================== +- id: go-test-mod + name: 'go-test-mod' + entry: go-test-mod.sh + types: [go] + language: 'script' + description: "Run 'cd $(mod_root $FILE); go test ./...' for each staged .go file" + pass_filenames: true + require_serial: true + # ============================================================================== # go-test-dir # * Directory-Based @@ -150,6 +180,21 @@ description: "Run 'go vet $FILE' for each staged .go file" pass_filenames: true +# ============================================================================== +# go-vet-mod +# * Directory-Based +# * Targets first parent folder with a go.mod file +# * Executes if any .go files modified +# ============================================================================== +- id: go-vet-mod + name: 'go-vet-mod' + entry: go-vet-mod.sh + types: [go] + language: 'script' + description: "Run 'cd $(mod_root $FILE); go vet ./...' for each staged .go file" + pass_filenames: true + require_serial: true + # ============================================================================== # go-vet-dir # * Directory-Based @@ -317,6 +362,37 @@ description: "Run 'golangci-lint run --fix $FILE' for each staged .go file" pass_filenames: true +# ============================================================================== +# golangci-lint-mod +# * Directory-Based +# * Targets first parent folder with a go.mod file +# * Executes if any .go files modified +# ============================================================================== +- id: golangci-lint-mod + name: 'golangci-lint-mod' + entry: golangci-lint-mod.sh + types: [go] + language: 'script' + description: "Run 'cd $(mod_root $FILE); golangci-lint run ./...' for each staged .go file" + pass_filenames: true + require_serial: true + +# ============================================================================== +# golangci-lint-mod-fix +# * Directory-Based +# * Targets first parent folder with a go.mod file +# * Modifies files +# * Executes if any .go files modified +# ============================================================================== +- id: golangci-lint-mod-fix + name: 'golangci-lint-mod-fix' + entry: golangci-lint-mod-fix.sh + types: [go] + language: 'script' + description: "Run 'cd $(mod_root $FILE); golangci-lint run --fix ./...' for each staged .go file" + pass_filenames: true + require_serial: true + # ============================================================================== # golangci-lint-dir # * Directory-Based diff --git a/README.md b/README.md index 3900e1a..dfbf3d9 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,7 @@ You can copy/paste the following snippet into your `.pre-commit-config.yaml` fil # # Go Build # + - id: go-build-mod - id: go-build-dir - id: go-build-pkg - id: go-build-repo @@ -36,6 +37,7 @@ You can copy/paste the following snippet into your `.pre-commit-config.yaml` fil # # Go Test # + - id: go-test-mod - id: go-test-dir - id: go-test-pkg - id: go-test-repo @@ -45,6 +47,7 @@ You can copy/paste the following snippet into your `.pre-commit-config.yaml` fil # Go Vet # - id: go-vet + - id: go-vet-mod - id: go-vet-dir - id: go-vet-pkg - id: go-vet-repo @@ -70,6 +73,8 @@ You can copy/paste the following snippet into your `.pre-commit-config.yaml` fil # - id: golangci-lint - id: golangci-lint-fix + - id: golangci-lint-mod + - id: golangci-lint-mod-fix - id: golangci-lint-dir - id: golangci-lint-dir-fix - id: golangci-lint-pkg @@ -100,8 +105,9 @@ Some hooks work on a per-directory basis. The hooks run against the directory c Currently, directory-based hooks DO NOT accept user-args. #### Directory-Hook Suffixes - - ``*-dir-*`` : Hook runs using `./$(dirname $FILE)` as target. - - ``*-pkg-*`` : Hook runs using `'$(go list)/$(dirname $FILE)` as target. + - `*-mod-*` : Hook runs inside first module root directory going up $FILE path. + - `*-dir-*` : Hook runs using `./$(dirname $FILE)` as target. + - `*-pkg-*` : Hook runs using `'$(go list)/$(dirname $FILE)` as target. #### Multiple Hook Invocations By design, the directory-based hooks only execute against a given directory once per hook invocation. @@ -156,6 +162,7 @@ Consider adding aliases to longer-named hooks for easier CLI usage. ### go-build-repo-* #### Directory-Based Hooks + - `go-build-mod` - `go-build-dir` - `go-build-pkg` @@ -175,6 +182,7 @@ Comes with Golang ( [golang.org](https://golang.org/) ) ### go-test-repo-* #### Directory-Based Hooks + - `go-test-mod` - `go-test-dir` - `go-test-pkd` @@ -197,6 +205,7 @@ Comes with Golang ( [golang.org](https://golang.org/) ) - `go-vet` - Runs against staged `.go` files #### Directory-Based Hooks + - `go-vet-mod` - `go-vet-dir` - `go-vet-pkg` @@ -332,6 +341,8 @@ go get -u golang.org/x/lint/golint - `golangci-lint-fix` #### Directory-Based Hooks +- `golangci-lint-mod` +- `golangci-lint-mod-fix` - `golangci-lint-dir` - `golangci-lint-dir-fix` - `golangci-lint-pkg` diff --git a/go-build-mod.sh b/go-build-mod.sh new file mode 100755 index 0000000..e944a67 --- /dev/null +++ b/go-build-mod.sh @@ -0,0 +1,29 @@ +#!/usr/bin/env bash + +# Walks up the file path looking for go.mod +# +function find_module_roots() { + for arg in "$@" ; do + local path="${arg}" + if [ "${path}" == "" ]; then + path="." + elif [ -f "${path}" ]; then + path=$(dirname "${path}") + fi + while [ "${path}" != "." ] && [ ! -f "${path}/go.mod" ]; do + path=$(dirname "${path}") + done + echo "${path}" + done +} + +errCode=0 +for sub in $(find_module_roots "$@" | sort -u) ; do + pushd "${sub}" >/dev/null + go build ./... + if [ $? -ne 0 ]; then + errCode=1 + fi + popd >/dev/null +done +exit $errCode diff --git a/go-test-mod.sh b/go-test-mod.sh new file mode 100755 index 0000000..4bbdc35 --- /dev/null +++ b/go-test-mod.sh @@ -0,0 +1,29 @@ +#!/usr/bin/env bash + +# Walks up the file path looking for go.mod +# +function find_module_roots() { + for arg in "$@" ; do + local path="${arg}" + if [ "${path}" == "" ]; then + path="." + elif [ -f "${path}" ]; then + path=$(dirname "${path}") + fi + while [ "${path}" != "." ] && [ ! -f "${path}/go.mod" ]; do + path=$(dirname "${path}") + done + echo "${path}" + done +} + +errCode=0 +for sub in $(find_module_roots "$@" | sort -u) ; do + pushd "${sub}" >/dev/null + go test ./... + if [ $? -ne 0 ]; then + errCode=1 + fi + popd >/dev/null +done +exit $errCode diff --git a/go-vet-mod.sh b/go-vet-mod.sh new file mode 100755 index 0000000..bf83ff5 --- /dev/null +++ b/go-vet-mod.sh @@ -0,0 +1,29 @@ +#!/usr/bin/env bash + +# Walks up the file path looking for go.mod +# +function find_module_roots() { + for arg in "$@" ; do + local path="${arg}" + if [ "${path}" == "" ]; then + path="." + elif [ -f "${path}" ]; then + path=$(dirname "${path}") + fi + while [ "${path}" != "." ] && [ ! -f "${path}/go.mod" ]; do + path=$(dirname "${path}") + done + echo "${path}" + done +} + +errCode=0 +for sub in $(find_module_roots "$@" | sort -u) ; do + pushd "${sub}" >/dev/null + go vet ./... + if [ $? -ne 0 ]; then + errCode=1 + fi + popd >/dev/null +done +exit $errCode diff --git a/golangci-lint-mod-fix.sh b/golangci-lint-mod-fix.sh new file mode 100755 index 0000000..dd1946f --- /dev/null +++ b/golangci-lint-mod-fix.sh @@ -0,0 +1,29 @@ +#!/usr/bin/env bash + +# Walks up the file path looking for go.mod +# +function find_module_roots() { + for arg in "$@" ; do + local path="${arg}" + if [ "${path}" == "" ]; then + path="." + elif [ -f "${path}" ]; then + path=$(dirname "${path}") + fi + while [ "${path}" != "." ] && [ ! -f "${path}/go.mod" ]; do + path=$(dirname "${path}") + done + echo "${path}" + done +} + +errCode=0 +for sub in $(find_module_roots "$@" | sort -u) ; do + pushd "${sub}" >/dev/null + golangci-lint run --fix ./... + if [ $? -ne 0 ]; then + errCode=1 + fi + popd >/dev/null +done +exit $errCode diff --git a/golangci-lint-mod.sh b/golangci-lint-mod.sh new file mode 100755 index 0000000..d0127b8 --- /dev/null +++ b/golangci-lint-mod.sh @@ -0,0 +1,29 @@ +#!/usr/bin/env bash + +# Walks up the file path looking for go.mod +# +function find_module_roots() { + for arg in "$@" ; do + local path="${arg}" + if [ "${path}" == "" ]; then + path="." + elif [ -f "${path}" ]; then + path=$(dirname "${path}") + fi + while [ "${path}" != "." ] && [ ! -f "${path}/go.mod" ]; do + path=$(dirname "${path}") + done + echo "${path}" + done +} + +errCode=0 +for sub in $(find_module_roots "$@" | sort -u) ; do + pushd "${sub}" >/dev/null + golangci-lint run ./... + if [ $? -ne 0 ]; then + errCode=1 + fi + popd >/dev/null +done +exit $errCode diff --git a/sample-config.yaml b/sample-config.yaml index de23792..8bcd51e 100644 --- a/sample-config.yaml +++ b/sample-config.yaml @@ -30,8 +30,10 @@ repos: # Currently, directory-based hooks DO NOT accept user-args. # # Directory-Hook Suffixes: + # *-mod-* : Hook runs inside first module root directory going up + # $FILE path. # *-dir-* : Hook runs using './$(dirname $FILE)' as target. - # *-pkg-* : Hook runs using ''$(go list)/$(dirname $FILE)' as target. + # *-pkg-* : Hook runs using '$(go list)/$(dirname $FILE)' as target. # # ! Multiple Hook Invocations # ! By design, the directory-based hooks only execute against a given @@ -68,6 +70,7 @@ repos: # # Go Build # + - id: go-build-mod - id: go-build-dir - id: go-build-pkg - id: go-build-repo @@ -76,6 +79,7 @@ repos: # # Go Test # + - id: go-test-mod - id: go-test-dir - id: go-test-pkg - id: go-test-repo @@ -85,6 +89,7 @@ repos: # Go Vet # - id: go-vet + - id: go-vet-mod - id: go-vet-dir - id: go-vet-pkg - id: go-vet-repo @@ -110,6 +115,8 @@ repos: # - id: golangci-lint - id: golangci-lint-fix + - id: golangci-lint-mod + - id: golangci-lint-mod-fix - id: golangci-lint-dir - id: golangci-lint-dir-fix - id: golangci-lint-pkg