-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck.sh
executable file
·226 lines (190 loc) · 5.32 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#!/bin/bash
# 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
# all modules that we want to process iun the various stages
modules=()
while read -r m; do
modules+=("$m")
done < <(go list ./... | grep -vE '^code.mantid.org/gtg/tests')
#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"
if grep -qcE ^replace go.mod; then
warn "go.mod contains 'replace' directives"
fi
go mod tidy
go mod verify
go mod vendor
}
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"
revive -formatter default -exclude vendor/... -exclude mocks/... "$@"
}
function process_stage_vet() {
header "$task" "Examining code for suspicious constructs"
go vet "$@"
}
function process_stage_err() {
header "$task" "Checking for uncaught error returns"
errcheck -ignoretests "$@"
}
function process_stage_static() {
header "$task" "Static checking of code for common errors"
# 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 (> 10)"
gocyclo -over 10 -avg .
true
}
function process_stage_test() {
header "$task" "Running all unit tests"
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
}
function process_stage_updatedeps() {
header "$task" "Updating dependencies"
note "get"
go get -u -t ./...
note "tidy"
go mod tidy
note "vendor"
go mod vendor
note "verify"
go mod verify
}
# 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/ofabry/go-callvis@latest
github.com/securego/gosec/v2/cmd/gosec@latest
golang.org/x/exp/cmd/modgraphviz@latest
golang.org/x/tools/cmd/goimports@latest
golang.org/x/vuln/cmd/govulncheck@latest
honnef.co/go/tools/cmd/staticcheck@latest
github.com/kisielk/errcheck@latest
)
for t in "${tools[@]}"; do
note "Updating ${t}"
go install "${t}" &
done
wait
true
}
function process_stage_depgraph() {
header "$task" "Generating a dependency graph"
if [[ -z "$(which dot)" ]]; then
printf " dot from graphviz is needed to generate dependency graphs\n"
printf " run 'brew install graphviz' to install, or see https://graphviz.org/download/ more more info\n"
fi
if [[ -n "$(which modgraphviz)" ]] && [[ -n "$(which dot)" ]]; then
go mod graph | modgraphviz | dot -T png -o _dependencies.png
fi
}
function process_stage_callgraph() {
header "$task" "Generating a call graph"
go-callvis -file=_callgraphy.png cmd/gtg/*
}
###
function not_stage_exists() {
LC_ALL=C test "$(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