diff --git a/Makefile b/Makefile index 45f107947..0ccd547b4 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +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 . -path ./vendor -prune -o -name "*.go" +GOFMT_FILES?=$$(find . -not -path "./vendor/*" -name "*.go") GOOS=$(shell go env GOOS) GOARCH=$(shell go env GOARCH) GOPATH=$(shell go env GOPATH) @@ -58,13 +58,10 @@ dev: deps ## Build and install a development build @cp $(GOPATH)/bin/packer pkg/$(GOOS)_$(GOARCH) fmt: ## Format Go code - @$(GOFMT_FILES) | xargs gofmt -w -s + @gofmt -w -s $(GOFMT_FILES) fmt-check: ## Check go code formatting - @echo "==> Checking that code complies with gofmt requirements..." - @echo "You can use the command: \`make fmt\` to reformat code." - @$(GOFMT_FILES) | xargs $(CURDIR)/scripts/gofmtcheck.sh - @echo "Check complete." + $(CURDIR)/scripts/gofmtcheck.sh $(GOFMT_FILES) fmt-docs: @find ./website/source/docs -name "*.md" -exec pandoc --wrap auto --columns 79 --atx-headers -s -f "markdown_github+yaml_metadata_block" -t "markdown_github+yaml_metadata_block" {} -o {} \; diff --git a/scripts/gofmtcheck.sh b/scripts/gofmtcheck.sh index cc6deb81e..5b99bcdc4 100755 --- a/scripts/gofmtcheck.sh +++ b/scripts/gofmtcheck.sh @@ -1,8 +1,14 @@ #!/usr/bin/env bash -for f in $@; do - [ -n "`dos2unix 2>/dev/null < $f | gofmt -s -d`" ] && echo $f -done +# 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." -# always return success or else 'make' will abort exit 0