verify-golint.sh 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. set -o errexit
  16. set -o nounset
  17. set -o pipefail
  18. KUBE_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
  19. source "${KUBE_ROOT}/hack/lib/init.sh"
  20. source "${KUBE_ROOT}/hack/lib/util.sh"
  21. kube::golang::verify_go_version
  22. # Ensure that we find the binaries we build before anything else.
  23. export GOBIN="${KUBE_OUTPUT_BINPATH}"
  24. PATH="${GOBIN}:${PATH}"
  25. # Install golint from vendor
  26. echo 'installing golint from vendor'
  27. go install k8s.io/kubernetes/vendor/golang.org/x/lint/golint
  28. cd "${KUBE_ROOT}"
  29. # Check that the file is in alphabetical order
  30. failure_file="${KUBE_ROOT}/hack/.golint_failures"
  31. kube::util::check-file-in-alphabetical-order "${failure_file}"
  32. export IFS=$'\n'
  33. # NOTE: when "go list -e ./..." is run within GOPATH, it turns the k8s.io/kubernetes
  34. # as the prefix, however if we run it outside it returns the full path of the file
  35. # with a leading underscore. We'll need to support both scenarios for all_packages.
  36. all_packages=()
  37. while IFS='' read -r line; do all_packages+=("$line"); done < <(go list -e ./... | grep -vE "/(third_party|vendor|staging/src/k8s.io/client-go/pkg|generated|clientset_generated)" | sed -e 's|^k8s.io/kubernetes/||' -e "s|^_\(${KUBE_ROOT}/\)\{0,1\}||")
  38. failing_packages=()
  39. while IFS='' read -r line; do failing_packages+=("$line"); done < <(cat "$failure_file")
  40. unset IFS
  41. errors=()
  42. not_failing=()
  43. for p in "${all_packages[@]}"; do
  44. # Run golint on package/*.go file explicitly to validate all go files
  45. # and not just the ones for the current platform. This also will ensure that
  46. # _test.go files are linted.
  47. # Generated files are ignored, and each file is passed through golint
  48. # individually, as if one file in the package contains a fatal error (such as
  49. # a foo package with a corresponding foo_test package), golint seems to choke
  50. # completely.
  51. # Ref: https://github.com/kubernetes/kubernetes/pull/67675
  52. # Ref: https://github.com/golang/lint/issues/68
  53. failedLint=$(find "$p"/*.go | grep -vE "(zz_generated.*.go|generated.pb.go|generated.proto|types_swagger_doc_generated.go)" | xargs -L1 golint 2>/dev/null)
  54. kube::util::array_contains "$p" "${failing_packages[@]}" && in_failing=$? || in_failing=$?
  55. if [[ -n "${failedLint}" ]] && [[ "${in_failing}" -ne "0" ]]; then
  56. errors+=( "${failedLint}" )
  57. fi
  58. if [[ -z "${failedLint}" ]] && [[ "${in_failing}" -eq "0" ]]; then
  59. not_failing+=( "$p" )
  60. fi
  61. done
  62. # Check that all failing_packages actually still exist
  63. gone=()
  64. for p in "${failing_packages[@]}"; do
  65. kube::util::array_contains "$p" "${all_packages[@]}" || gone+=( "$p" )
  66. done
  67. # Check to be sure all the packages that should pass lint are.
  68. if [ ${#errors[@]} -eq 0 ]; then
  69. echo 'Congratulations! All Go source files have been linted.'
  70. else
  71. {
  72. echo "Errors from golint:"
  73. for err in "${errors[@]}"; do
  74. echo "$err"
  75. done
  76. echo
  77. echo 'Please review the above warnings. You can test via "golint" and commit the result.'
  78. echo 'If the above warnings do not make sense, you can exempt this package from golint'
  79. echo 'checking by adding it to hack/.golint_failures (if your reviewer is okay with it).'
  80. echo
  81. } >&2
  82. exit 1
  83. fi
  84. if [[ ${#not_failing[@]} -gt 0 ]]; then
  85. {
  86. echo "Some packages in hack/.golint_failures are passing golint. Please remove them."
  87. echo
  88. for p in "${not_failing[@]}"; do
  89. echo " $p"
  90. done
  91. echo
  92. } >&2
  93. exit 1
  94. fi
  95. if [[ ${#gone[@]} -gt 0 ]]; then
  96. {
  97. echo "Some packages in hack/.golint_failures do not exist anymore. Please remove them."
  98. echo
  99. for p in "${gone[@]}"; do
  100. echo " $p"
  101. done
  102. echo
  103. } >&2
  104. exit 1
  105. fi