diff --git a/Makefile b/Makefile index a33dbef91..9fbaab871 100644 --- a/Makefile +++ b/Makefile @@ -4,6 +4,7 @@ VET?=$(shell ls -d */ | grep -v vendor | grep -v website) GITSHA:=$(shell git rev-parse HEAD) # Get the current local branch name from git (if we can, this may be blank) GITBRANCH:=$(shell git symbolic-ref --short HEAD 2>/dev/null) +GOFMT_FILES?=$$(find . -not -path "./vendor/*" -name "*.go") default: deps generate test dev @@ -40,7 +41,10 @@ dev: deps ## Build and install a development build @PACKER_DEV=1 GO15VENDOREXPERIMENT=1 sh -c "$(CURDIR)/scripts/build.sh" fmt: ## Format Go code - @gofmt -s -w `go list -f {{.Dir}} ./... | grep -v "/vendor/"` + @gofmt -w -s $(GOFMT_FILES) + +fmt-check: ## Check go code formatting + $(CURDIR)/scripts/gofmtcheck.sh $(GOFMT_FILES) # Install js-beautify with npm install -g js-beautify fmt-examples: @@ -52,7 +56,7 @@ generate: deps ## Generate dynamically generated code go generate . gofmt -w command/plugin.go -test: deps ## Run unit tests +test: deps fmt-check ## Run unit tests @go test $(TEST) $(TESTARGS) -timeout=2m @go tool vet $(VET) ; if [ $$? -eq 1 ]; then \ echo "ERROR: Vet found problems in the code."; \ diff --git a/scripts/gofmtcheck.sh b/scripts/gofmtcheck.sh new file mode 100755 index 000000000..5b99bcdc4 --- /dev/null +++ b/scripts/gofmtcheck.sh @@ -0,0 +1,14 @@ +#!/usr/bin/env bash + +# Check gofmt +echo "==> Checking that code complies with gofmt requirements..." +gofmt_files=$(gofmt -s -l ${@}) +if [[ -n ${gofmt_files} ]]; then + echo 'gofmt needs running on the following files:' + echo "${gofmt_files}" + echo "You can use the command: \`make fmt\` to reformat code." + exit 1 +fi +echo "Check passed." + +exit 0