github.com/kaituanwang/hyperledger@v2.0.1+incompatible/scripts/golinter.sh (about) 1 #!/bin/bash 2 3 # Copyright Greg Haskins All Rights Reserved. 4 # 5 # SPDX-License-Identifier: Apache-2.0 6 7 set -e 8 9 # shellcheck source=/dev/null 10 source "$(cd "$(dirname "$0")" && pwd)/functions.sh" 11 12 fabric_dir="$(cd "$(dirname "$0")/.." && pwd)" 13 source_dirs=() 14 while IFS=$'\n' read -r source_dir; do 15 source_dirs+=("$source_dir") 16 done < <(go list -f '{{.Dir}}' ./... | sed s,"${fabric_dir}".,,g | cut -f 1 -d / | sort -u) 17 18 echo "Checking with gofmt" 19 OUTPUT="$(gofmt -l -s "${source_dirs[@]}")" 20 OUTPUT="$(filterExcludedAndGeneratedFiles "$OUTPUT")" 21 if [ -n "$OUTPUT" ]; then 22 echo "The following files contain gofmt errors" 23 echo "$OUTPUT" 24 echo "The gofmt command 'gofmt -l -s -w' must be run for these files" 25 exit 1 26 fi 27 28 echo "Checking with goimports" 29 OUTPUT="$(goimports -l "${source_dirs[@]}")" 30 OUTPUT="$(filterExcludedAndGeneratedFiles "$OUTPUT")" 31 if [ -n "$OUTPUT" ]; then 32 echo "The following files contain goimports errors" 33 echo "$OUTPUT" 34 echo "The goimports command 'goimports -l -w' must be run for these files" 35 exit 1 36 fi 37 38 # Now that context is part of the standard library, we should use it 39 # consistently. The only place where the legacy golang.org version should be 40 # referenced is in the generated protos. 41 echo "Checking for golang.org/x/net/context" 42 context_whitelist=( 43 "^github.com/hyperledger/fabric/core/comm/testpb:" 44 "^github.com/hyperledger/fabric/orderer/common/broadcast/mock:" 45 "^github.com/hyperledger/fabric/common/grpclogging/fakes:" 46 "^github.com/hyperledger/fabric/common/grpclogging/testpb:" 47 "^github.com/hyperledger/fabric/common/grpcmetrics/fakes:" 48 "^github.com/hyperledger/fabric/common/grpcmetrics/testpb:" 49 ) 50 # shellcheck disable=SC2016 51 TEMPLATE='{{with $d := .}}{{range $d.Imports}}{{ printf "%s:%s " $d.ImportPath . }}{{end}}{{end}}' 52 OUTPUT="$(go list -f "$TEMPLATE" ./... | grep -Ev "$(IFS='|' ; echo "${context_whitelist[*]}")" | grep 'golang.org/x/net/context' | cut -f1 -d:)" 53 if [ -n "$OUTPUT" ]; then 54 echo "The following packages import golang.org/x/net/context instead of context" 55 echo "$OUTPUT" 56 exit 1 57 fi 58 59 echo "Checking with go vet" 60 PRINTFUNCS="Print,Printf,Info,Infof,Warning,Warningf,Error,Errorf,Critical,Criticalf,Sprint,Sprintf,Log,Logf,Panic,Panicf,Fatal,Fatalf,Notice,Noticef,Wrap,Wrapf,WithMessage" 61 OUTPUT="$(go vet -all -printfuncs "$PRINTFUNCS" ./...)" 62 if [ -n "$OUTPUT" ]; then 63 echo "The following files contain go vet errors" 64 echo "$OUTPUT" 65 exit 1 66 fi