From 417f5a9f996d0353cc46a8e8dcd0a811f108b805 Mon Sep 17 00:00:00 2001 From: Solly Ross Date: Tue, 27 Jun 2017 18:25:35 -0400 Subject: [PATCH] [build] Add gofmt-related Makefile targets This adds `verify-gofmt` and `gofmt` make targets for quick go-fmting. It also adds a `verify` target which verifies `gofmt` and the runs the unit tests. Travis now runs `make verify` instead of just `make test` as the tests. --- .travis.yml | 2 +- Makefile | 10 +++++++++- hack/gofmt-all.sh | 41 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 2 deletions(-) create mode 100755 hack/gofmt-all.sh diff --git a/.travis.yml b/.travis.yml index 6ff351d9..8425c6f4 100644 --- a/.travis.yml +++ b/.travis.yml @@ -16,7 +16,7 @@ addons: install: - make -B vendor -script: make test +script: make verify cache: directories: diff --git a/Makefile b/Makefile index b60221a7..3ec912db 100644 --- a/Makefile +++ b/Makefile @@ -24,7 +24,7 @@ ifeq ($(ARCH),s390x) BASEIMAGE?=s390x/busybox endif -.PHONY: all build docker-build push-% push test +.PHONY: all build docker-build push-% push test verify-gofmt gofmt verify all: build build: vendor @@ -56,3 +56,11 @@ vendor: glide.lock test: vendor CGO_ENABLED=0 go test ./pkg/... + +verify-gofmt: + ./hack/gofmt-all.sh -v + +gofmt: + ./hack/gofmt-all.sh + +verify: verify-gofmt test diff --git a/hack/gofmt-all.sh b/hack/gofmt-all.sh new file mode 100755 index 00000000..1dbfc344 --- /dev/null +++ b/hack/gofmt-all.sh @@ -0,0 +1,41 @@ +#!/bin/bash + +# Copyright 2017 The Kubernetes Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +set -o errexit +set -o nounset +set -o pipefail + +verify=0 +if [[ ${1:-} = "--verify" || ${1:-} = "-v" ]]; then + verify=1 +fi + +find_files() { + find . -not \( \( \ + -wholename './_output' \ + -o -wholename './vendor' \ + \) -prune \) -name '*.go' +} + +if [[ $verify -eq 1 ]]; then + diff=$(find_files | xargs gofmt -s -d 2>&1) + if [[ -n "${diff}" ]]; then + echo "gofmt -s -w $(echo "${diff}" | awk '/^diff / { print $2 }' | tr '\n' ' ')" + exit 1 + fi +else + find_files | xargs gofmt -s -w +fi