verify-golint.sh 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #!/bin/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}")/..
  19. source "${KUBE_ROOT}/hack/lib/init.sh"
  20. kube::golang::verify_go_version
  21. cd "${KUBE_ROOT}"
  22. array_contains () {
  23. local seeking=$1; shift # shift will iterate through the array
  24. local in=1 # in holds the exit status for the function
  25. for element; do
  26. if [[ "$element" == "$seeking" ]]; then
  27. in=0 # set in to 0 since we found it
  28. break
  29. fi
  30. done
  31. return $in
  32. }
  33. # Check that the file is in alphabetical order
  34. linted_file="${KUBE_ROOT}/hack/.linted_packages"
  35. if ! diff -u "${linted_file}" <(LANG=C sort "${linted_file}"); then
  36. {
  37. echo
  38. echo "hack/.linted_packages is not in alphabetical order. Please sort it:"
  39. echo
  40. echo " LANG=C sort -o hack/.linted_packages hack/.linted_packages"
  41. echo
  42. } >&2
  43. false
  44. fi
  45. export IFS=$'\n'
  46. all_packages=(
  47. $(go list -e ./... | egrep -v "/(third_party|vendor|staging|generated|clientset_generated)" | sed 's/k8s.io\/kubernetes\///g')
  48. )
  49. linted_packages=(
  50. $(cat $linted_file)
  51. )
  52. unset IFS
  53. linted=()
  54. errors=()
  55. for p in "${all_packages[@]}"; do
  56. # Run golint on package/*.go file explicitly to validate all go files
  57. # and not just the ones for the current platform.
  58. failedLint=$(golint "$p"/*.go)
  59. if [ "$failedLint" ]; then
  60. if array_contains "$p" "${linted_packages[@]}"; then
  61. errors+=( "$failedLint" )
  62. fi
  63. else
  64. array_contains "$p" "${linted_packages[@]}" || linted+=( "$p" )
  65. fi
  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 fix the above errors. You can test via "golint" and commit the result.'
  78. echo
  79. } >&2
  80. false
  81. fi
  82. # check to make sure all packages that pass lint are in the linted file.
  83. echo
  84. if [ ${#linted[@]} -eq 0 ]; then
  85. echo 'Success! All packages that should pass lint are listed in the linted file.'
  86. else
  87. {
  88. echo "Some packages passed golint but are not listed in hack/.linted_packages."
  89. echo "Please add them in alphabetical order:"
  90. echo
  91. for p in "${linted[@]}"; do
  92. echo " echo $p >> hack/.linted_packages"
  93. done
  94. echo " LANG=C sort -o hack/.linted_packages hack/.linted_packages"
  95. echo
  96. echo 'You can test via this script and commit the result.'
  97. echo
  98. } >&2
  99. false
  100. fi