From d2c89f3000b8e597e8784449534f35c87e42539a Mon Sep 17 00:00:00 2001 From: Olivier Bellone Date: Fri, 2 Aug 2019 15:13:05 +0800 Subject: [PATCH] Use golangci-lint for linting (#71) --- .golangci.yml | 18 +++++++++ .travis.yml | 15 ++++---- .vscode/settings.json | 7 ++++ Makefile | 88 +++++++++++++++++++++++++++++-------------- 4 files changed, 93 insertions(+), 35 deletions(-) create mode 100644 .golangci.yml diff --git a/.golangci.yml b/.golangci.yml new file mode 100644 index 000000000..5dc9deb0a --- /dev/null +++ b/.golangci.yml @@ -0,0 +1,18 @@ +run: + tests: false + +linters: + goimports: + local-prefixes: github.com/stripe/stripe-cli + +linters: + enable-all: true + disable: + - errcheck + - gochecknoglobals + - gochecknoinits + - gosec + - lll + - maligned + - unparam + - stylecheck diff --git a/.travis.yml b/.travis.yml index 18f5bae06..4d4b782e5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,18 +1,19 @@ +dist: bionic + language: go -go: -- 1.12.x +go: '1.12.x' addons: apt: packages: - rpm -env: - global: - - GO111MODULE=on - - GOBIN="${GOPATH}/bin" - - PATH="${GOBIN}:${PATH}" +install: + - make setup + +script: + - make ci deploy: - provider: script diff --git a/.vscode/settings.json b/.vscode/settings.json index 5c0365966..9cb42f8e4 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -5,6 +5,13 @@ "editor.formatOnSave": false, "go.formatTool": "goimports", + "go.formatFlags": [ + "-local", "github.com/stripe/stripe-cli", + ], + "go.lintTool": "golangci-lint", + "go.lintFlags": [ + "--fast", + ], "[go]": { "editor.formatOnSave": true }, diff --git a/Makefile b/Makefile index 1ddc00dd7..1b2690ef1 100644 --- a/Makefile +++ b/Makefile @@ -1,50 +1,80 @@ +SOURCE_FILES?=./... +TEST_PATTERN?=. +TEST_OPTIONS?= + export GO111MODULE := on export GOBIN := $(shell pwd)/bin export PATH := $(GOBIN):$(PATH) +export GOPROXY := https://gocenter.io -all: test +# Install all the build and lint dependencies +setup: + curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh + curl -L https://git.io/misspell | sh + go mod download +.PHONY: setup -install-deps: - go get -.PHONY: install-deps +# Run all the tests +test: + go test $(TEST_OPTIONS) -failfast -race -coverpkg=./... -covermode=atomic -coverprofile=coverage.txt $(SOURCE_FILES) -run $(TEST_PATTERN) -timeout=2m +.PHONY: test -update-deps: - go get -u -.PHONY: update-deps +# Run all the tests and opens the coverage report +cover: test + go tool cover -html=coverage.txt +.PHONY: cover -update-openapi-spec: - rm -f ./api/openapi-spec/spec3.sdk.json - wget https://raw.githubusercontent.com/stripe/openapi/master/openapi/spec3.sdk.json -P ./api/openapi-spec -.PHONY: update-openapi-spec +# gofmt and goimports all go files +fmt: + find . -name '*.go' | while read -r file; do gofmt -w -s "$$file"; goimports -w "$$file"; done +.PHONY: fmt +# Run all the linters lint: -# In travis, we need to install golint explicitly. Don't do this in other -# environments -ifeq ($(ENVIRONMENT), travis) - go get golang.org/x/lint/golint - git checkout . -endif - golint -set_exit_status ./... + # TODO: fix tests and disabled linter issues + ./bin/golangci-lint run ./... + ./bin/misspell -error **/*.go .PHONY: lint -vet: - go vet $(shell go list ./... | grep -v /vendor/) -.PHONY: vet +# Clean go.mod +go-mod-tidy: + @go mod tidy -v + @git diff HEAD + @git diff-index --quiet HEAD +.PHONY: go-mod-tidy -test: install-deps lint vet - go test -race -cover -v ./... - @echo '\o/ yay, we did it!' -.PHONY: test +# Run all the tests and code checks +ci: build test lint go-mod-tidy +.PHONY: ci +# Build a beta version of stripe build: - go mod download go generate ./... - go build -o stripe -ldflags "-s -w" cmd/stripe/main.go + go build -o stripe cmd/stripe/main.go .PHONY: build +# Show to-do items per file +todo: + @grep \ + --exclude-dir=vendor \ + --exclude-dir=node_modules \ + --exclude=Makefile \ + --text \ + --color \ + -nRo -E ' TODO:.*|SkipNow' . +.PHONY: todo + +# Updates the OpenAPI spec +update-openapi-spec: + rm -f ./api/openapi-spec/spec3.sdk.json + wget https://raw.githubusercontent.com/stripe/openapi/master/openapi/spec3.sdk.json -P ./api/openapi-spec +.PHONY: update-openapi-spec + +# Releases a new version +release: # This does not release anything from your local machine but creates a tag # for our CI to handle it -release: + git pull origin master # Makefile's execute each line in its own subshell so variables don't @@ -54,3 +84,5 @@ release: git tag $$version git push --tags .PHONY: release + +.DEFAULT_GOAL := build