-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcheckcode.sh
executable file
·57 lines (49 loc) · 2.04 KB
/
checkcode.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
#!/bin/bash -e
# Copyright Greg Haskins All Rights Reserved.
#
# SPDX-License-Identifier: Apache-2.0
set -e
# place the Go build cache directory into the default build tree if it exists
if [ -d "${GOPATH}/src/github.com/ok-chain/okchain/.build" ]; then
export GOCACHE="${GOPATH}/src/github.com/ok-chain/okchain/.build/go-cache"
fi
okchain_dir="$(cd "$(dirname "$0")" && pwd)"
source_dirs=$(go list -f '{{.Dir}}' ./... | sed s,"${okchain_dir}".,,g | cut -f 1 -d / | sort -u)
echo "Checking with gofmt"
OUTPUT="$(gofmt -l -s ${source_dirs})"
if [ -n "$OUTPUT" ]; then
echo "The following files contain gofmt errors"
echo "$OUTPUT"
echo "The gofmt command 'gofmt -l -s -w' must be run for these files"
exit 1
fi
echo "Checking with goimports"
OUTPUT="$(goimports -l ${source_dirs} | grep -Ev '(^|/)testdata/' || true)"
if [ -n "$OUTPUT" ]; then
echo "The following files contain goimports errors"
echo $OUTPUT
echo "The goimports command 'goimports -l -w' must be run for these files"
exit 1
fi
# Now that context is part of the standard library, we should use it
# consistently. The only place where the legacy golang.org version should be
# referenced is in the generated protos.
echo "Checking for golang.org/x/net/context"
context_whitelist=(
"^github.com/ok-chain/okchain/protos(:|/.*:)"
)
TEMPLATE='{{with $d := .}}{{range $d.Imports}}{{ printf "%s:%s " $d.ImportPath . }}{{end}}{{end}}'
OUTPUT="$(go list -f "$TEMPLATE" ./... | grep -Ev $(IFS='|' ; echo "${context_whitelist[*]}") | grep 'golang.org/x/net/context' | cut -f1 -d:)"
if [ -n "$OUTPUT" ]; then
echo "The following packages import golang.org/x/net/context instead of context"
echo "$OUTPUT"
exit 1
fi
echo "Checking with go vet"
PRINTFUNCS="Print,Printf,Info,Infof,Warning,Warningf,Error,Errorf,Critical,Criticalf,Sprint,Sprintf,Log,Logf,Panic,Panicf,Fatal,Fatalf,Notice,Noticef,Wrap,Wrapf,WithMessage"
OUTPUT="$(go vet -all -printfuncs $PRINTFUNCS ./...)"
if [ -n "$OUTPUT" ]; then
echo "The following files contain go vet errors"
echo $OUTPUT
exit 1
fi