github.com/pvitto98/fabric@v2.1.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 # shellcheck disable=SC2016 43 TEMPLATE='{{with $d := .}}{{range $d.Imports}}{{ printf "%s:%s " $d.ImportPath . }}{{end}}{{end}}' 44 OUTPUT="$(go list -f "$TEMPLATE" ./... | grep 'golang.org/x/net/context' | cut -f1 -d:)" 45 if [ -n "$OUTPUT" ]; then 46 echo "The following packages import golang.org/x/net/context instead of context" 47 echo "$OUTPUT" 48 exit 1 49 fi 50 51 # We use golang/protobuf but goimports likes to add gogo/protobuf. 52 # Prevent accidental import of gogo/protobuf. 53 echo "Checking for github.com/gogo/protobuf" 54 # shellcheck disable=SC2016 55 TEMPLATE='{{with $d := .}}{{range $d.Imports}}{{ printf "%s:%s " $d.ImportPath . }}{{end}}{{end}}' 56 OUTPUT="$(go list -f "$TEMPLATE" ./... | grep 'github.com/gogo/protobuf' | cut -f1 -d:)" 57 if [ -n "$OUTPUT" ]; then 58 echo "The following packages import github.com/gogo/protobuf instead of github.com/golang/protobuf" 59 echo "$OUTPUT" 60 exit 1 61 fi 62 63 echo "Checking with go vet" 64 PRINTFUNCS="Print,Printf,Info,Infof,Warning,Warningf,Error,Errorf,Critical,Criticalf,Sprint,Sprintf,Log,Logf,Panic,Panicf,Fatal,Fatalf,Notice,Noticef,Wrap,Wrapf,WithMessage" 65 OUTPUT="$(go vet -all -printfuncs "$PRINTFUNCS" ./...)" 66 if [ -n "$OUTPUT" ]; then 67 echo "The following files contain go vet errors" 68 echo "$OUTPUT" 69 exit 1 70 fi