forked from juju/juju
-
Notifications
You must be signed in to change notification settings - Fork 0
/
golinters.bash
executable file
·60 lines (54 loc) · 1.74 KB
/
golinters.bash
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
#!/bin/bash
# Copyright 2019 Canonical Ltd.
# Licensed under the AGPLv3, see LICENCE file for details.
exitstatus=0
OUTPUT_FILE=$(mktemp) || { echo "Failed to create temp file"; exit 1; }
MOCK_HEADER="// Code generated by MockGen. DO NOT EDIT."
# go get the golangci-lint, which will install the binary
# into $GOPATH/bin
go get -u github.com/golangci/golangci-lint/cmd/golangci-lint
# verify that the golangci-lint exists in the $GOPATH bin
if [ ! -f "$GOPATH/bin/golangci-lint" ]; then
(>&2 echo "Error: golangci-lint is not installed.")
exit 1
fi
SKIP_DIRS=
if [[ "${CGO_ENABLED}" != "1" ]]; then
SKIP_DIRS=--skip-dirs worker/dbaccessor
fi
$GOPATH/bin/golangci-lint run \
--disable-all \
--no-config \
--max-issues-per-linter=50 \
--max-same-issues=50 \
--issues-exit-code=0 \
--enable=gofmt \
--enable=goimports \
--enable=misspell \
--enable=unconvert \
--enable=ineffassign \
${SKIP_DIRS} \
&> $OUTPUT_FILE
# go through each golangci-lint error and check to see if it's related
# to a mock file.
invalidLines=`cat $OUTPUT_FILE | grep -v '\(.*\/[^\/]*\.go\):[[:digit:]]*:[[:digit:]]*:.*' | wc -l`
if [ $invalidLines -ne 0 ]; then
# there was a parse error when reading the golangci-lint output
exitstatus=1
(>&2 cat $OUTPUT_FILE)
else
# valid set of lines, go through each and make sure they're valid
# mock files, if not exit out.
lines=`wc -l $OUTPUT_FILE | awk '{print $1}'`
if [ $lines -ne 0 ]; then
while read p; do
header=`echo $p | cut -d':' -f1 | xargs head -n1`
if [ "$header" != "$MOCK_HEADER" ]; then
exitstatus=1
(>&2 echo $p)
fi
done <$OUTPUT_FILE
fi
fi
rm $OUTPUT_FILE
exit $exitstatus