forked from TekWizely/pre-commit-golang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
golangci-lint-mod.sh
executable file
·70 lines (62 loc) · 1.31 KB
/
golangci-lint-mod.sh
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
#!/usr/bin/env bash
cmd=(golangci-lint run)
export GO111MODULE=on
# 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
if [ -f "${path}/go.mod" ]; then
echo "${path}"
fi
done
}
OPTIONS=()
# If arg doesn't pass [ -f ] check, then it is assumed to be an option
#
while [ $# -gt 0 ] && [ "$1" != "-" ] && [ "$1" != "--" ] && [ ! -f "$1" ]; do
OPTIONS+=("$1")
shift
done
FILES=()
# Assume start of file list (may still be options)
#
while [ $# -gt 0 ] && [ "$1" != "-" ] && [ "$1" != "--" ]; do
FILES+=("$1")
shift
done
# If '--' next, then files = options
#
if [ $# -gt 0 ]; then
if [ "$1" == "-" ] || [ "$1" == "--" ]; then
shift
# Append to previous options
#
OPTIONS=("${OPTIONS[@]}" "${FILES[@]}")
FILES=()
fi
fi
# Any remaining arguments are assumed to be files
#
while [ $# -gt 0 ]; do
FILES+=("$1")
shift
done
errCode=0
for sub in $(find_module_roots "${FILES[@]}" | sort -u) ; do
pushd "${sub}" >/dev/null
"${cmd[@]}" "${OPTIONS[@]}" ./...
if [ $? -ne 0 ]; then
errCode=1
fi
popd >/dev/null
done
exit $errCode