From c163fbed35a4abfecf18551fa2dd11bd59a61fd2 Mon Sep 17 00:00:00 2001 From: Matthew Hooker Date: Tue, 1 May 2018 20:39:48 -0700 Subject: [PATCH 1/2] use xargs to check for formatting --- Makefile | 11 ++++++++++- scripts/gofmtcheck.sh | 14 -------------- 2 files changed, 10 insertions(+), 15 deletions(-) delete mode 100755 scripts/gofmtcheck.sh diff --git a/Makefile b/Makefile index 0ccd547b4..8510eee09 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") +BAD_FILES=$(shell echo $(GOFMT_FILES) | xargs gofmt -s -l) GOOS=$(shell go env GOOS) GOARCH=$(shell go env GOARCH) GOPATH=$(shell go env GOPATH) @@ -61,7 +62,15 @@ fmt: ## Format Go code @gofmt -w -s $(GOFMT_FILES) fmt-check: ## Check go code formatting - $(CURDIR)/scripts/gofmtcheck.sh $(GOFMT_FILES) + @echo "==> Checking that code complies with gofmt requirements..." + @if [ ! -z "$(BAD_FILES)" ]; then \ + echo "gofmt needs to be run on the following files:"; \ + echo "$(BAD_FILES)" | xargs -n1; \ + echo "You can use the command: \`make fmt\` to reformat code."; \ + exit 1; \ + else \ + echo "Check passed."; \ + fi 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 deleted file mode 100755 index 5b99bcdc4..000000000 --- a/scripts/gofmtcheck.sh +++ /dev/null @@ -1,14 +0,0 @@ -#!/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 From fac6b2e71ba3bd2ca9e62c746d1b51f331027c80 Mon Sep 17 00:00:00 2001 From: Matthew Hooker Date: Tue, 1 May 2018 22:55:10 -0700 Subject: [PATCH 2/2] don't send every source file through the shell --- Makefile | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/Makefile b/Makefile index 8510eee09..b22d61e93 100644 --- a/Makefile +++ b/Makefile @@ -4,12 +4,13 @@ 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") -BAD_FILES=$(shell echo $(GOFMT_FILES) | xargs gofmt -s -l) 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) + # Get the git commit GIT_DIRTY=$(shell test -n "`git status --porcelain`" && echo "+CHANGES" || true) GIT_COMMIT=$(shell git rev-parse --short HEAD) @@ -59,13 +60,13 @@ dev: deps ## Build and install a development build @cp $(GOPATH)/bin/packer pkg/$(GOOS)_$(GOARCH) fmt: ## Format Go code - @gofmt -w -s $(GOFMT_FILES) + @gofmt -w -s $(UNFORMATTED_FILES) fmt-check: ## Check go code formatting @echo "==> Checking that code complies with gofmt requirements..." - @if [ ! -z "$(BAD_FILES)" ]; then \ + @if [ ! -z "$(UNFORMATTED_FILES)" ]; then \ echo "gofmt needs to be run on the following files:"; \ - echo "$(BAD_FILES)" | xargs -n1; \ + echo "$(UNFORMATTED_FILES)" | xargs -n1; \ echo "You can use the command: \`make fmt\` to reformat code."; \ exit 1; \ else \