...
1#!/usr/bin/env bash
2
3# Copyright 2014 The Kubernetes Authors.
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17# This script checks whether the source code needs to be formatted or not by
18# `gofmt`. Run `hack/update-gofmt.sh` to actually format sources.
19#
20# Note: gofmt output can change between go versions.
21#
22# Usage: `hack/verify-gofmt.sh`.
23
24set -o errexit
25set -o nounset
26set -o pipefail
27
28KUBE_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
29source "${KUBE_ROOT}/hack/lib/init.sh"
30
31cd "${KUBE_ROOT}"
32
33kube::golang::setup_env
34
35find_files() {
36 find . -not \( \
37 \( \
38 -wholename './.git' \
39 -o -wholename './_output' \
40 -o -wholename './release' \
41 -o -wholename './target' \
42 -o -wholename '*/third_party/*' \
43 -o -wholename '*/vendor/*' \
44 -o -wholename '*/testdata/*' \
45 -o -wholename '*/bindata.go' \
46 \) -prune \
47 \) -name '*.go'
48}
49
50# gofmt exits with non-zero exit code if it finds a problem unrelated to
51# formatting (e.g., a file does not parse correctly). Without "|| true" this
52# would have led to no useful error message from gofmt, because the script would
53# have failed before getting to the "echo" in the block below.
54diff=$(find_files | xargs gofmt -d -s 2>&1) || true
55if [[ -n "${diff}" ]]; then
56 echo "${diff}" >&2
57 echo >&2
58 echo "Run ./hack/update-gofmt.sh" >&2
59 exit 1
60fi
View as plain text