|
| 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 | +# The csi-release-tools directory. |
| 22 | +TOOLS="$(dirname "${BASH_SOURCE[0]}")" |
| 23 | +. "${TOOLS}/util.sh" |
| 24 | + |
| 25 | +# Directory to check. Default is the parent of the tools themselves. |
| 26 | +ROOT="${1:-${TOOLS}/..}" |
| 27 | + |
| 28 | +# required version for this script, if not installed on the host we will |
| 29 | +# use the official docker image instead. keep this in sync with SHELLCHECK_IMAGE |
| 30 | +SHELLCHECK_VERSION="0.6.0" |
| 31 | +# upstream shellcheck latest stable image as of January 10th, 2019 |
| 32 | +SHELLCHECK_IMAGE="koalaman/shellcheck-alpine:v0.6.0@sha256:7d4d712a2686da99d37580b4e2f45eb658b74e4b01caf67c1099adc294b96b52" |
| 33 | + |
| 34 | +# fixed name for the shellcheck docker container so we can reliably clean it up |
| 35 | +SHELLCHECK_CONTAINER="k8s-shellcheck" |
| 36 | + |
| 37 | +# disabled lints |
| 38 | +disabled=( |
| 39 | + # this lint disallows non-constant source, which we use extensively without |
| 40 | + # any known bugs |
| 41 | + 1090 |
| 42 | + # this lint prefers command -v to which, they are not the same |
| 43 | + 2230 |
| 44 | +) |
| 45 | +# comma separate for passing to shellcheck |
| 46 | +join_by() { |
| 47 | + local IFS="$1"; |
| 48 | + shift; |
| 49 | + echo "$*"; |
| 50 | +} |
| 51 | +SHELLCHECK_DISABLED="$(join_by , "${disabled[@]}")" |
| 52 | +readonly SHELLCHECK_DISABLED |
| 53 | + |
| 54 | +# creates the shellcheck container for later use |
| 55 | +create_container () { |
| 56 | + # TODO(bentheelder): this is a performance hack, we create the container with |
| 57 | + # a sleep MAX_INT32 so that it is effectively paused. |
| 58 | + # We then repeatedly exec to it to run each shellcheck, and later rm it when |
| 59 | + # we're done. |
| 60 | + # This is incredibly much faster than creating a container for each shellcheck |
| 61 | + # call ... |
| 62 | + docker run --name "${SHELLCHECK_CONTAINER}" -d --rm -v "${ROOT}:${ROOT}" -w "${ROOT}" --entrypoint="sleep" "${SHELLCHECK_IMAGE}" 2147483647 |
| 63 | +} |
| 64 | +# removes the shellcheck container |
| 65 | +remove_container () { |
| 66 | + docker rm -f "${SHELLCHECK_CONTAINER}" &> /dev/null || true |
| 67 | +} |
| 68 | + |
| 69 | +# ensure we're linting the source tree |
| 70 | +cd "${ROOT}" |
| 71 | + |
| 72 | +# find all shell scripts excluding ./_*, ./.git/*, ./vendor*, |
| 73 | +# and anything git-ignored |
| 74 | +all_shell_scripts=() |
| 75 | +while IFS=$'\n' read -r script; |
| 76 | + do git check-ignore -q "$script" || all_shell_scripts+=("$script"); |
| 77 | +done < <(find . -name "*.sh" \ |
| 78 | + -not \( \ |
| 79 | + -path ./_\* -o \ |
| 80 | + -path ./.git\* -o \ |
| 81 | + -path ./vendor\* \ |
| 82 | + \)) |
| 83 | + |
| 84 | +# detect if the host machine has the required shellcheck version installed |
| 85 | +# if so, we will use that instead. |
| 86 | +HAVE_SHELLCHECK=false |
| 87 | +if which shellcheck &>/dev/null; then |
| 88 | + detected_version="$(shellcheck --version | grep 'version: .*')" |
| 89 | + if [[ "${detected_version}" = "version: ${SHELLCHECK_VERSION}" ]]; then |
| 90 | + HAVE_SHELLCHECK=true |
| 91 | + fi |
| 92 | +fi |
| 93 | + |
| 94 | +# tell the user which we've selected and possibly set up the container |
| 95 | +if ${HAVE_SHELLCHECK}; then |
| 96 | + echo "Using host shellcheck ${SHELLCHECK_VERSION} binary." |
| 97 | +else |
| 98 | + echo "Using shellcheck ${SHELLCHECK_VERSION} docker image." |
| 99 | + # remove any previous container, ensure we will attempt to cleanup on exit, |
| 100 | + # and create the container |
| 101 | + remove_container |
| 102 | + kube::util::trap_add 'remove_container' EXIT |
| 103 | + if ! output="$(create_container 2>&1)"; then |
| 104 | + { |
| 105 | + echo "Failed to create shellcheck container with output: " |
| 106 | + echo "" |
| 107 | + echo "${output}" |
| 108 | + } >&2 |
| 109 | + exit 1 |
| 110 | + fi |
| 111 | +fi |
| 112 | + |
| 113 | +# lint each script, tracking failures |
| 114 | +errors=() |
| 115 | +for f in "${all_shell_scripts[@]}"; do |
| 116 | + set +o errexit |
| 117 | + if ${HAVE_SHELLCHECK}; then |
| 118 | + failedLint=$(shellcheck --exclude="${SHELLCHECK_DISABLED}" "${f}") |
| 119 | + else |
| 120 | + failedLint=$(docker exec -t ${SHELLCHECK_CONTAINER} \ |
| 121 | + shellcheck --exclude="${SHELLCHECK_DISABLED}" "${f}") |
| 122 | + fi |
| 123 | + set -o errexit |
| 124 | + if [[ -n "${failedLint}" ]]; then |
| 125 | + errors+=( "${failedLint}" ) |
| 126 | + fi |
| 127 | +done |
| 128 | + |
| 129 | +# Check to be sure all the packages that should pass lint are. |
| 130 | +if [ ${#errors[@]} -eq 0 ]; then |
| 131 | + echo 'Congratulations! All shell files are passing lint.' |
| 132 | +else |
| 133 | + { |
| 134 | + echo "Errors from shellcheck:" |
| 135 | + for err in "${errors[@]}"; do |
| 136 | + echo "$err" |
| 137 | + done |
| 138 | + echo |
| 139 | + echo 'Please review the above warnings. You can test via "./hack/verify-shellcheck"' |
| 140 | + echo 'If the above warnings do not make sense, you can exempt them from shellcheck' |
| 141 | + echo 'checking by adding the "shellcheck disable" directive' |
| 142 | + echo '(https://github.com/koalaman/shellcheck/wiki/Directive#disable).' |
| 143 | + echo |
| 144 | + } >&2 |
| 145 | + false |
| 146 | +fi |
0 commit comments