From b8893e1aa4df8d0d285646ff52e71ff16e967edd Mon Sep 17 00:00:00 2001 From: Matthew Hooker Date: Tue, 1 Nov 2016 13:48:10 -0700 Subject: [PATCH 1/3] fix fmt and add check --- Makefile | 7 +++++-- scripts/gofmtcheck.sh | 13 +++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) create mode 100755 scripts/gofmtcheck.sh diff --git a/Makefile b/Makefile index a33dbef91..95a627f81 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 . -not -path "./vendor/*" -name "*.go") default: deps generate test dev ci: deps test @@ -40,7 +40,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: diff --git a/scripts/gofmtcheck.sh b/scripts/gofmtcheck.sh new file mode 100755 index 000000000..7bc3f4b1a --- /dev/null +++ b/scripts/gofmtcheck.sh @@ -0,0 +1,13 @@ +#!/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 + +exit 0 From 3a79620e92373b98a163da9665569f700f1033fe Mon Sep 17 00:00:00 2001 From: Matthew Hooker Date: Tue, 1 Nov 2016 13:49:13 -0700 Subject: [PATCH 2/3] fail test target if we have gofmt problems --- Makefile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 95a627f81..9fbaab871 100644 --- a/Makefile +++ b/Makefile @@ -5,6 +5,7 @@ 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 ci: deps test @@ -55,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."; \ From 9cf65d8e1b925421a95466d7751d491328980254 Mon Sep 17 00:00:00 2001 From: Matthew Hooker Date: Tue, 1 Nov 2016 14:00:41 -0700 Subject: [PATCH 3/3] say when we finish checking gofmt --- scripts/gofmtcheck.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/scripts/gofmtcheck.sh b/scripts/gofmtcheck.sh index 7bc3f4b1a..5b99bcdc4 100755 --- a/scripts/gofmtcheck.sh +++ b/scripts/gofmtcheck.sh @@ -9,5 +9,6 @@ if [[ -n ${gofmt_files} ]]; then echo "You can use the command: \`make fmt\` to reformat code." exit 1 fi +echo "Check passed." exit 0