From a3fb07d00818513c902bffd41ea0e740d9c8b768 Mon Sep 17 00:00:00 2001 From: James Bardin Date: Wed, 2 Dec 2020 12:02:33 -0500 Subject: [PATCH] add staticcheck make target cleanup the old fmtcheck script while we're in here --- Makefile | 5 ++++- scripts/gofmtcheck.sh | 8 ++++---- scripts/staticcheck.sh | 16 ++++++++++++++++ 3 files changed, 24 insertions(+), 5 deletions(-) create mode 100755 scripts/staticcheck.sh diff --git a/Makefile b/Makefile index ede1c5cd6..16e2abaa5 100644 --- a/Makefile +++ b/Makefile @@ -20,6 +20,9 @@ protobuf: fmtcheck: @sh -c "'$(CURDIR)/scripts/gofmtcheck.sh'" +staticcheck: + @sh -c "'$(CURDIR)/scripts/staticcheck.sh'" + website: ifeq (,$(wildcard $(GOPATH)/src/$(WEBSITE_REPO))) echo "$(WEBSITE_REPO) not found in your GOPATH (necessary for layouts and assets), get-ting..." @@ -46,4 +49,4 @@ endif # under parallel conditions. .NOTPARALLEL: -.PHONY: fmtcheck generate protobuf website website-test +.PHONY: fmtcheck generate protobuf website website-test staticcheck diff --git a/scripts/gofmtcheck.sh b/scripts/gofmtcheck.sh index 9a341da94..00b81a8be 100755 --- a/scripts/gofmtcheck.sh +++ b/scripts/gofmtcheck.sh @@ -1,12 +1,12 @@ #!/usr/bin/env bash -# Check gofmt -echo "==> Checking that code complies with gofmt requirements..." -gofmt_files=$(gofmt -l `find . -name '*.go' | grep -v vendor`) +# Check go fmt +echo "==> Checking that code complies with go fmt requirements..." +gofmt_files=$(go fmt ./...) if [[ -n ${gofmt_files} ]]; then echo 'gofmt needs running on the following files:' echo "${gofmt_files}" - echo "You can use the command: \`gofmt -w .\` to reformat code." + echo "You can use the command: \`go fmt\` to reformat code." exit 1 fi diff --git a/scripts/staticcheck.sh b/scripts/staticcheck.sh new file mode 100755 index 000000000..66c47092f --- /dev/null +++ b/scripts/staticcheck.sh @@ -0,0 +1,16 @@ +#!/usr/bin/env bash + +echo "==> Checking that code complies with static analysis requirements..." +# Skip legacy code which is frozen, and can be removed once we can refactor the +# remote backends to no longer require it. +skip="internal/legacy|backend/remote-state/" + +# Skip generated code for protobufs. +skip=$skip"|internal/planproto|internal/tfplugin5|internal/tfplugin6" + +packages=$(go list ./... | egrep -v ${skip}) + +# We are skipping style-related checks, since terraform intentionally breaks +# some of these. The goal here is to find issues that reduce code clarity, or +# may result in bugs. +staticcheck -checks 'all,-ST*' ${packages}