Skip to content

Commit

Permalink
Added module-level hooks for build, test, vet, golang-ci
Browse files Browse the repository at this point in the history
  • Loading branch information
TekWizely committed Jul 2, 2019
1 parent 0cacfe3 commit 7a9cc10
Show file tree
Hide file tree
Showing 8 changed files with 242 additions and 3 deletions.
76 changes: 76 additions & 0 deletions .pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
15 changes: 13 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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`
Expand All @@ -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`
Expand All @@ -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`
Expand Down Expand Up @@ -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`
Expand Down
29 changes: 29 additions & 0 deletions go-build-mod.sh
Original file line number Diff line number Diff line change
@@ -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
29 changes: 29 additions & 0 deletions go-test-mod.sh
Original file line number Diff line number Diff line change
@@ -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
29 changes: 29 additions & 0 deletions go-vet-mod.sh
Original file line number Diff line number Diff line change
@@ -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
29 changes: 29 additions & 0 deletions golangci-lint-mod-fix.sh
Original file line number Diff line number Diff line change
@@ -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
29 changes: 29 additions & 0 deletions golangci-lint-mod.sh
Original file line number Diff line number Diff line change
@@ -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
9 changes: 8 additions & 1 deletion sample-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -68,6 +70,7 @@ repos:
#
# Go Build
#
- id: go-build-mod
- id: go-build-dir
- id: go-build-pkg
- id: go-build-repo
Expand All @@ -76,6 +79,7 @@ repos:
#
# Go Test
#
- id: go-test-mod
- id: go-test-dir
- id: go-test-pkg
- id: go-test-repo
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 7a9cc10

Please sign in to comment.