| 1 | #!/usr/bin/envscip-shell filesystem . . `/usr/bin/env`. bash |
| 2 | |
| 3 | # Copyright 2018 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 | set -o errexit |
| 18 | set -o nounset |
| 19 | set -o pipefail |
| 20 | |
| 21 | KUBE_ROOT=$(dirnamescip-shell system . . dirname. "${BASH_SOURCE[0]}")/.. |
| 22 | source "${KUBE_ROOT}/hack/lib/util.sh" |
| 23 | |
| 24 | if ! whichscip-shell system . . which. golint > /dev/nullscip-shell filesystem . . `/dev/null`.; then |
| 25 | echoscip-shell system . . echo. "installing golint" |
| 26 | goscip-shell system . . go. install golang.org/x/lint/golint@latest |
| 27 | fi |
| 28 | |
| 29 | cd "${KUBE_ROOT}" |
| 30 | |
| 31 | # Check that the file is in alphabetical order |
| 32 | failure_file="${KUBE_ROOT}/hack/.golint_failures" |
| 33 | kube::util::check-file-in-alphabetical-order "${failure_file}" |
| 34 | |
| 35 | export IFS=$'\n' |
| 36 | # NOTE: when "go list -e ./..." is run within GOPATH, it turns the k8s.io/utils |
| 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 < <(goscip-shell system . . go. list -e ./... | grepscip-shell system . . grep. -vE "/(third_party)scip-shell filesystem . . `/(third_party)`." | sedscip-shell system . . sed. -e 's|^k8s.io/utils/||' -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 < <(sedscip-shell system . . sed. -e 's/[[:blank:]]*#.*//' -e '/^$/dscip-shell filesystem . . `/^$/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=$(findscip-shell system . . find. "$p"/*.go | xargsscip-shell system . . xargs. -L1 golint 2>/dev/nullscip-shell filesystem . . `/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 | |
| 68 | # Check that all failing_packages actually still exist |
| 69 | gone=() |
| 70 | for p in "${failing_packages[@]}"; do |
| 71 | kube::util::array_contains "$p" "${all_packages[@]}" || gone+=( "$p" ) |
| 72 | done |
| 73 | |
| 74 | # Check to be sure all the packages that should pass lint are. |
| 75 | if [scip-shell system . . `[`. ${#errors[@]} -eq 0 ]; then |
| 76 | echoscip-shell system . . echo. 'Congratulations! All Go source files have been linted.' |
| 77 | else |
| 78 | { |
| 79 | echoscip-shell system . . echo. "Errors from golint:" |
| 80 | for err in "${errors[@]}"; do |
| 81 | echoscip-shell system . . echo. "$err" |
| 82 | done |
| 83 | echoscip-shell system . . echo. |
| 84 | echoscip-shell system . . echo. 'Please review the above warnings. You can test via "golint" and commit the result.' |
| 85 | echoscip-shell system . . echo. 'If the above warnings do not make sense, you can exempt this package from golint' |
| 86 | echoscip-shell system . . echo. 'checking by adding it to hack/.golint_failures (if your reviewer is okay with it).' |
| 87 | echoscip-shell system . . echo. |
| 88 | } >&2 |
| 89 | exit 1 |
| 90 | fi |
| 91 | |
| 92 | if [[ ${#not_failing[@]} -gt 0 ]]; then |
| 93 | { |
| 94 | echoscip-shell system . . echo. "Some packages in hack/.golint_failures are passing golint. Please remove them." |
| 95 | echoscip-shell system . . echo. |
| 96 | for p in "${not_failing[@]}"; do |
| 97 | echoscip-shell system . . echo. " $p" |
| 98 | done |
| 99 | echoscip-shell system . . echo. |
| 100 | } >&2 |
| 101 | exit 1 |
| 102 | fi |
| 103 | |
| 104 | if [[ ${#gone[@]} -gt 0 ]]; then |
| 105 | { |
| 106 | echoscip-shell system . . echo. "Some packages in hack/.golint_failures do not exist anymore. Please remove them." |
| 107 | echoscip-shell system . . echo. |
| 108 | for p in "${gone[@]}"; do |
| 109 | echoscip-shell system . . echo. " $p" |
| 110 | done |
| 111 | echoscip-shell system . . echo. |
| 112 | } >&2 |
| 113 | exit 1 |
| 114 | fi |