-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgolangci-lint-hook
More file actions
executable file
·83 lines (64 loc) · 2.31 KB
/
golangci-lint-hook
File metadata and controls
executable file
·83 lines (64 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env bash
set -e
# ######################################
# Ensure that a go module is initialized if not, skip the tests
# #######################################
ensure_go_module_initialized() {
if [[ ! -f go.mod ]]; then
echo "go.mod file not found, skipping the tests..."
exit 0
fi
}
# #######################################
# Constants
# #######################################
GO_BIN_PATH="$(go env GOPATH)/bin"
GOLANGCI_LINT_INSTALL_URL="https://raw.githubusercontent.com/golangci/golangci-lint/HEAD/install.sh"
ROOT_DIR="$(git rev-parse --show-toplevel)"
if [[ -f "${ROOT_DIR}/.project-settings.env" ]]; then
# shellcheck disable=SC1090
source "${ROOT_DIR}/.project-settings.env"
fi
GOLANGCI_LINT_VERSION="${GOLANGCI_LINT_VERSION:-v2.11.4}"
# #######################################
# Install dependencies to run the pre-commit hook
# #######################################
install_dependencies() {
# check if golangci-lint is installed or not
if ! command -v golangci-lint >/dev/null 2>&1; then
echo "installing golangci-lint ${GOLANGCI_LINT_VERSION}..."
# binary will be $(go env GOPATH)/bin/golangci-lint
curl -sSfL "${GOLANGCI_LINT_INSTALL_URL}" | sh -s -- -b "$(go env GOPATH)/bin" "${GOLANGCI_LINT_VERSION}"
echo "Installed golangci-lint ${GOLANGCI_LINT_VERSION}"
"$(go env GOPATH)"/bin/golangci-lint --version
# export the golangci-lint path to the PATH variable
export PATH="${GO_BIN_PATH}:${PATH}"
else
echo "golangci-lint is already installed"
fi
}
# #######################################
# Run the pre-commit
# #######################################
hook() {
install_dependencies
# get the root of the project
local root_dir
root_dir=$(git rev-parse --show-toplevel)
# run the pre-commit hook
pushd "${root_dir}" || exit
echo "Running golangci-lint..."
golangci-lint run --build-tags test ./... || exit 1
popd >/dev/null || exit
}
cat <<EOF
golangci-lint hook
=============================================================================
This hook ensures and runs golangci-lint on the project.
It should be installed and run as a pre-commit hook.
If golangci-lint finds any errors it will prevent the commit.
=============================================================================
EOF
# run the hook if the go module is initialized
ensure_go_module_initialized
hook