From b8893e1aa4df8d0d285646ff52e71ff16e967edd Mon Sep 17 00:00:00 2001 From: Matthew Hooker Date: Tue, 1 Nov 2016 13:48:10 -0700 Subject: [PATCH] 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