From 573ce7d0e7b7bfc9247c31b7a30c8881618293fa Mon Sep 17 00:00:00 2001 From: Andy Goldstein Date: Tue, 23 Oct 2018 10:31:24 -0400 Subject: [PATCH] Update formatting script - Pin to a specific revision of goimports - Use -local flag with goimports to keep ark imports separated - Correct shellcheck errors Signed-off-by: Andy Goldstein --- hack/build-image/Dockerfile | 4 +++ hack/update-fmt.sh | 55 ++++++++++++++++++++++++++++++++----- hack/verify-fmt.sh | 20 ++------------ 3 files changed, 55 insertions(+), 24 deletions(-) diff --git a/hack/build-image/Dockerfile b/hack/build-image/Dockerfile index 5aeec9595..9025e922e 100644 --- a/hack/build-image/Dockerfile +++ b/hack/build-image/Dockerfile @@ -19,4 +19,8 @@ RUN apk add --update --no-cache git bash && \ cd /go/src/k8s.io && \ git clone -b kubernetes-1.11.0 https://github.com/kubernetes/code-generator && \ git clone -b kubernetes-1.11.0 https://github.com/kubernetes/apimachinery && \ + go get golang.org/x/tools/cmd/goimports && \ + cd /go/src/golang.org/x/tools && \ + git checkout 40a48ad93fbe707101afb2099b738471f70594ec && \ + go install ./cmd/goimports && \ echo chmod -R a+w /go diff --git a/hack/update-fmt.sh b/hack/update-fmt.sh index d54e61a6a..bf32a983f 100755 --- a/hack/update-fmt.sh +++ b/hack/update-fmt.sh @@ -1,4 +1,4 @@ -#!/bin/bash -e +#!/bin/bash # # Copyright 2017 the Heptio Ark contributors. # @@ -14,13 +14,54 @@ # See the License for the specific language governing permissions and # limitations under the License. -HACK_DIR=$(dirname "${BASH_SOURCE}") +set -o errexit +set -o nounset +set -o pipefail -echo "Updating formatting" +if [[ ${1:-} == '--verify' ]]; then + # List file diffs that need formatting updates + MODE='-d' + ACTION='Verifying' +else + # Write formatting updates to files + MODE='-w' + ACTION='Updating' +fi -gofmt -w -s $(find . -type f -name "*.go" -not -path "./vendor/*" -not -path "./pkg/generated/*" -not -name "zz_generated*") +if ! command -v goimports > /dev/null; then + echo 'goimports is missing - please run "go get golang.org/x/tools/cmd/goimports"' + exit 1 +fi -command -v goimports > /dev/null || go get golang.org/x/tools/cmd/goimports -goimports -w -d $(find . -type f -name "*.go" -not -path "./vendor/*" -not -path "./pkg/generated/*" -not -name "zz_generated*") +files="$(find . -type f -name '*.go' -not -path './vendor/*' -not -path './pkg/generated/*' -not -name 'zz_generated*')" +echo "${ACTION} gofmt" +for file in ${files}; do + output=$(gofmt "${MODE}" -s "${file}") + if [[ -n "${output}" ]]; then + VERIFY_FMT_FAILED=1 + echo "${output}" + fi +done +if [[ -n "${VERIFY_FMT_FAILED:-}" ]]; then + echo "${ACTION} gofmt - failed! Please run 'make update'." +else + echo "${ACTION} gofmt - done!" +fi -echo "Success!" +echo "${ACTION} goimports" +for file in ${files}; do + output=$(goimports "${MODE}" -local github.com/heptio/ark "${file}") + if [[ -n "${output}" ]]; then + VERIFY_IMPORTS_FAILED=1 + echo "${output}" + fi +done +if [[ -n "${VERIFY_IMPORTS_FAILED:-}" ]]; then + echo "${ACTION} goimports - failed! Please run 'make update'." +else + echo "${ACTION} goimports - done!" +fi + +if [[ -n "${VERIFY_FMT_FAILED:-}" || -n "${VERIFY_IMPORTS_FAILED:-}" ]]; then + exit 1 +fi diff --git a/hack/verify-fmt.sh b/hack/verify-fmt.sh index 18de618f6..18f478274 100755 --- a/hack/verify-fmt.sh +++ b/hack/verify-fmt.sh @@ -1,4 +1,4 @@ -#!/bin/bash -e +#!/bin/bash # # Copyright 2017 the Heptio Ark contributors. # @@ -14,19 +14,5 @@ # See the License for the specific language governing permissions and # limitations under the License. -HACK_DIR=$(dirname "${BASH_SOURCE}") - -echo "Verifying gofmt" -files=$(gofmt -l -s $(find . -type f -name "*.go" -not -path "./vendor/*" -not -path "./pkg/generated/*" -not -name "zz_generated*")) -if [[ -n "${files}" ]]; then - echo "The following files need gofmt updating - please run 'make update'" - echo "${files}" - exit 1 -fi -echo "Success!" - -echo "Verifying goimports" -command -v goimports > /dev/null || go get golang.org/x/tools/cmd/goimports -goimports -l $(find . -type f -name "*.go" -not -path "./vendor/*" -not -path "./pkg/generated/*" -not -name "zz_generated*") -echo "Success!" - +HACK_DIR=$(dirname "${BASH_SOURCE[0]}") +"${HACK_DIR}"/update-fmt.sh --verify