verify-gofmt.sh 1.9 KB

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