-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck.sh
executable file
·199 lines (166 loc) · 4.35 KB
/
check.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#!/bin/bash
set -e
# shellcheck disable=SC2034
PROJECT_NAME="$(basename "${PWD}")"
tasks=(dep fmt revive vet err static sec vuln cyclo test cover)
while [[ "$1" =~ ^- ]]; do
case $1 in
-v)
TESTARGS='-v'
;;
esac
shift
done
function header() {
name="${1}"
shift
printf "\e[1;32m%-15s\e[0;34m%s\e[0m\n" "${name}" "${*}"
}
function warn() {
printf " \e[1;33m*** WARNING\e[0;33m %s\e[0m\n" "${*}"
}
function note() {
printf "\e[2;36m>> %s\e[0m\n" "${*}"
}
# Things to do before processing anything. Override in ./check_extra.sh or ./develop.env
function before() {
# nothing by default
:
}
# Thing to do after processing everything. Override in ./check_extra.sh or ./develop.env
function after() {
# nothing by default
:
}
if [[ ! -f "go.mod" ]]; then
note "Current directory is not the root of a go project"
exit
fi
modules=()
while read -r m; do
modules+=("$m")
done < <(go list ./... | grep -vE '/cmd/' )
#note "Processing Modules:"
#for i in "${modules[@]}"; do
# note "$i"
#done
if [[ "${#@}" -gt 0 ]]; then
tasks=("$@")
fi
### define stages
function process_stage_defined_stages() {
header "$task" "List of all defined stages"
declare -F | cut -d' ' -f3 | grep process_stage | cut -d'_' -f3-
}
function process_stage_dep() {
header "$task" "Ensuring dependencies are clean"
go mod tidy
go mod vendor
go mod verify
if grep -qcE ^replace go.mod; then
warn "go.mod contains 'replace' directives"
fi
}
function process_stage_fmt() {
header "$task" "Standardising formatting"
files=()
while IFS='' read -r filename; do
files+=("${filename}")
done < <(find . -name '*.go' -not -name '*.pb.go' -not -path '*/vendor/*')
for f in "${files[@]}"; do
sed -i -e '/import (/,/)/{/\/\//,/^$/N;/^$/d;}' "${f}"
goimports -w -local code.mantid.org "${f}"
done
}
function process_stage_revive() {
header "$task" "Checking linting rules"
shift
revive -formatter default -exclude vendor/... -exclude mocks/... "$@"
}
function process_stage_vet() {
header "$task" "Examining code for suspicious constructs"
shift
go vet "$@"
}
function process_stage_err() {
header "$task" "Checking for uncaught error returns"
shift
errcheck -ignoretests "$@"
}
function process_stage_static() {
header "$task" "Static checking of code for common errors"
shift
# https://staticcheck.io/docs/checks
staticcheck "$@"
}
function process_stage_sec() {
header "$task" "Looking for common programming mistakes that can lead to security problems"
gosec -exclude=G304 -quiet ./...
}
function process_stage_vuln() {
header "$task" "Checking code against published vulnerabilities"
govulncheck ./... || true
}
function process_stage_cyclo() {
header "$task" "Looking for potential refactoring required for functions with high complexity"
gocyclo -over 10 -avg .
true
}
function process_stage_test() {
header "$task" "Running all unit tests"
shift
COVERAGE=$(mktemp)
go test "$@" -cover -coverprofile="${COVERAGE}" ${TESTARGS}
grep -v '.pb.go:' "${COVERAGE}" > coverage.out
rm "${COVERAGE}"
}
function process_stage_cover() {
header "$task" "Generating coverage report"
go tool cover -html coverage.out -o coverage.html
go tool cover -func coverage.out
gocover-cobertura < coverage.out > coverage.xml
}
# install/update the tools used by this script
function process_stage_updatetools() {
header "$task" "Updating tool set"
tools=(
github.com/boumenot/gocover-cobertura@latest
github.com/fzipp/gocyclo/cmd/gocyclo@latest
github.com/mgechev/revive@latest
github.com/securego/gosec/v2/cmd/gosec@latest
golang.org/x/tools/cmd/goimports@latest
golang.org/x/vuln/cmd/govulncheck@latest
)
for t in "${tools[@]}"; do
note "Updating ${t}"
go install "${t}" &
done
wait
true
}
###
function not_stage_exists() {
LC_ALL=C [ "$(type -t "process_stage_${1}")" = "function" ]
}
# Add in project specific stuff
if [[ -f "./check_overrides.sh" ]]; then
source ./check_overrides.sh
fi
# add in any developer specific environment
if [[ -f "./develop.env" ]]; then
source ./develop.env
fi
# run all of the specified stages
before
for task in "${tasks[@]}"; do
if [[ $(not_stage_exists "${task}") ]]; then
warn "task '${task}' doesn't exist"
continue
fi
if ! "process_stage_${task}" "${modules[@]}"; then
warn "task '${task}' failed"
break
fi
done
after
exit 0