From deb2212e9e8c31826b9b5e6aa11a351fb72d490b Mon Sep 17 00:00:00 2001 From: Adrien Delorme Date: Mon, 27 May 2019 15:37:03 +0200 Subject: [PATCH] Makefile: use `go fmt` fmt-check no runs go fmt and then a git diff; this should also fail on line ending diffs --- .circleci/config.yml | 6 ++++++ Makefile | 20 +++++++------------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 36731d4b5..68f56531a 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -27,6 +27,11 @@ jobs: - checkout - run: GO111MODULE=on go run . --help - run: make check-vendor-vs-mod + check-fmt: + <<: *golang + steps: + - checkout + - run: make fmt-check build_linux: <<: *golang environment: @@ -74,6 +79,7 @@ workflows: jobs: - build - check-vendor-vs-mod + - check-fmt - build_linux: filters: tags: diff --git a/Makefile b/Makefile index 993620a07..5c4067fd3 100644 --- a/Makefile +++ b/Makefile @@ -8,9 +8,6 @@ GOOS=$(shell go env GOOS) GOARCH=$(shell go env GOARCH) GOPATH=$(shell go env GOPATH) -# gofmt -UNFORMATTED_FILES=$(shell find . -not -path "./vendor/*" -name "*.go" | xargs gofmt -s -l) - EXECUTABLE_FILES=$(shell find . -type f -executable | egrep -v '^\./(website/[vendor|tmp]|vendor/|\.git|bin/|scripts/|pkg/)' | egrep -v '.*(\.sh|\.bats|\.git)' | egrep -v './provisioner/(ansible|inspec)/test-fixtures/exit1') # Get the git commit @@ -64,17 +61,14 @@ dev: ## Build and install a development build @cp $(GOPATH)/bin/packer pkg/$(GOOS)_$(GOARCH) fmt: ## Format Go code - @gofmt -w -s main.go $(UNFORMATTED_FILES) + @go fmt ./... -fmt-check: ## Check go code formatting - @echo "==> Checking that code complies with gofmt requirements..." - @if [ ! -z "$(UNFORMATTED_FILES)" ]; then \ - echo "gofmt needs to be run on the following files:"; \ - echo "$(UNFORMATTED_FILES)" | xargs -n1; \ +fmt-check: fmt ## Check go code formatting + @echo "==> Checking that code complies with go fmt requirements..." + @git diff --exit-code; if [ $$? -eq 1 ]; then \ + echo "Found files that are not fmt'ed."; \ echo "You can use the command: \`make fmt\` to reformat code."; \ exit 1; \ - else \ - echo "Check passed."; \ fi mode-check: ## Check that only certain files are executable @@ -97,9 +91,9 @@ fmt-examples: # source files. generate: install-gen-deps ## Generate dynamically generated code go generate ./... - gofmt -w common/bootcommand/boot_command.go + go fmt -w common/bootcommand/boot_command.go goimports -w common/bootcommand/boot_command.go - gofmt -w command/plugin.go + go fmt -w command/plugin.go test: fmt-check mode-check vet ## Run unit tests @go test $(TEST) $(TESTARGS) -timeout=3m