From 3f8578f1e1bc16a4a67b066d74747ad83ecb85cf Mon Sep 17 00:00:00 2001 From: Daniel Nephin Date: Wed, 11 Mar 2020 21:18:03 -0400 Subject: [PATCH] ci: Use golangci-lint for linting Using golangci-lint has a number of advantages: - adding new linters becomes much easier, its a couple lines of yaml config instead of more bash scripting - it enables whitelisting of issues using inline comments or regex - when running multiple linters less work is done. The parsed source can be reused by multiple linters - linters are run in parallel to reduce CI runtime. --- .circleci/config.yml | 44 +++++++++++++++++++++----------------------- .golangci.yml | 14 ++++++++++++++ GNUmakefile | 3 ++- api/.golangci.yml | 1 + sdk/.golangci.yml | 1 + 5 files changed, 39 insertions(+), 24 deletions(-) create mode 100644 .golangci.yml create mode 120000 api/.golangci.yml create mode 120000 sdk/.golangci.yml diff --git a/.circleci/config.yml b/.circleci/config.yml index 411363598b..fbdec4e4a0 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -33,30 +33,30 @@ jobs: - checkout - run: go get -u github.com/hashicorp/lint-consul-retry && lint-consul-retry - # Runs go fmt and go vet - go-fmt-and-vet: + # Runs Go linters + lint: docker: - image: *GOLANG_IMAGE steps: - checkout - run: - command: go mod download - - run: - name: check go fmt + name: Install golangci-lint command: | - files="$(go fmt ./... ; (cd api && go fmt ./... | sed 's@^@api/@') ; (cd sdk && go fmt ./... | sed 's@^@sdk/@'))" - if [ -n "$files" ]; then - echo "The following file(s) do not conform to go fmt:" - echo "$files" - exit 1 - fi + download=https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh + wget -O- -q $download | sh -s -- -b /go/bin/ v1.23.6 + - run: go mod download - run: - command: | - go vet ./... && \ - (cd api && go vet ./...) && \ - (cd sdk && go vet ./...) - environment: - <<: *ENVIRONMENT + name: lint + command: &lintcmd | + golangci-lint run -v --concurrency 2 + - run: + name: lint api + working_directory: api + command: *lintcmd + - run: + name: lint sdk + working_directory: sdk + command: *lintcmd # checks vendor directory is correct check-vendor: @@ -619,15 +619,13 @@ workflows: - stable-website - /^docs\/.*/ - /^ui\/.*/ - - go-fmt-and-vet: - requires: - - check-vendor - - dev-build: - requires: - - go-fmt-and-vet + - lint + - dev-build - go-test: &go-test requires: - dev-build + - lint + - check-vendor - go-test-api: *go-test - go-test-sdk: *go-test - coverage-merge: diff --git a/.golangci.yml b/.golangci.yml new file mode 100644 index 0000000000..10aa5cc2d8 --- /dev/null +++ b/.golangci.yml @@ -0,0 +1,14 @@ +linters: + disable-all: true + enable: + - gofmt + - govet + +issues: + # Disable the default exclude list so that all excludes are explicitly + # defined in this file. + exclude-use-default: false + +linters-settings: + gofmt: + simplify: false diff --git a/GNUmakefile b/GNUmakefile index def8b73e5d..6734de2605 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -7,7 +7,8 @@ GOTOOLS = \ golang.org/x/tools/cmd/stringer \ github.com/gogo/protobuf/protoc-gen-gofast@$(GOGOVERSION) \ github.com/hashicorp/protoc-gen-go-binary \ - github.com/vektra/mockery/cmd/mockery + github.com/vektra/mockery/cmd/mockery \ + github.com/golangci/golangci-lint/cmd/golangci-lint@v1.23.6 GOTAGS ?= GOOS?=$(shell go env GOOS) diff --git a/api/.golangci.yml b/api/.golangci.yml new file mode 120000 index 0000000000..ff197a7796 --- /dev/null +++ b/api/.golangci.yml @@ -0,0 +1 @@ +../.golangci.yml \ No newline at end of file diff --git a/sdk/.golangci.yml b/sdk/.golangci.yml new file mode 120000 index 0000000000..ff197a7796 --- /dev/null +++ b/sdk/.golangci.yml @@ -0,0 +1 @@ +../.golangci.yml \ No newline at end of file