| 1 |
#!/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=$(dirname "${BASH_SOURCE[0]}")/.. |
| 22 |
source "${KUBE_ROOT}/hack/lib/util.sh" |
| 23 |
|
| 24 |
if ! which golint > /dev/null; then |
| 25 |
echo "installing golint" |
| 26 |
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 < <(go list -e ./... | grep -vE "/(third_party)" | 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 < <(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 | 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 |
|
| 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 [ ${#errors[@]} -eq 0 ]; then |
| 76 |
echo 'Congratulations! All Go source files have been linted.' |
| 77 |
else |
| 78 |
{ |
| 79 |
echo "Errors from golint:" |
| 80 |
for err in "${errors[@]}"; do |
| 81 |
echo "$err" |
| 82 |
done |
| 83 |
echo |
| 84 |
echo 'Please review the above warnings. You can test via "golint" and commit the result.' |
| 85 |
echo 'If the above warnings do not make sense, you can exempt this package from golint' |
| 86 |
echo 'checking by adding it to hack/.golint_failures (if your reviewer is okay with it).' |
| 87 |
echo |
| 88 |
} >&2 |
| 89 |
exit 1 |
| 90 |
fi |
| 91 |
|
| 92 |
if [[ ${#not_failing[@]} -gt 0 ]]; then |
| 93 |
{ |
| 94 |
echo "Some packages in hack/.golint_failures are passing golint. Please remove them." |
| 95 |
echo |
| 96 |
for p in "${not_failing[@]}"; do |
| 97 |
echo " $p" |
| 98 |
done |
| 99 |
echo |
| 100 |
} >&2 |
| 101 |
exit 1 |
| 102 |
fi |
| 103 |
|
| 104 |
if [[ ${#gone[@]} -gt 0 ]]; then |
| 105 |
{ |
| 106 |
echo "Some packages in hack/.golint_failures do not exist anymore. Please remove them." |
| 107 |
echo |
| 108 |
for p in "${gone[@]}"; do |
| 109 |
echo " $p" |
| 110 |
done |
| 111 |
echo |
| 112 |
} >&2 |
| 113 |
exit 1 |
| 114 |
fi |