verify-golint.sh 4.4 KB

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