verify-shellcheck.sh: make it usable in csi-release-tools

These are the modifications that were necessary to call this outside
of Kubernetes. The support for excluding files from checking gets
removed to simplify the script. It shouldn't be needed, because
linting can be enabled after fixing whatever scripts might fail the
check.
This commit is contained in:
Patrick Ohly
2019-03-15 16:42:10 +01:00
parent fb13c5198f
commit b2d25d4f4d
2 changed files with 15 additions and 747 deletions

View File

@@ -18,9 +18,12 @@ set -o errexit
set -o nounset
set -o pipefail
KUBE_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
source "${KUBE_ROOT}/hack/lib/init.sh"
source "${KUBE_ROOT}/hack/lib/util.sh"
# The csi-release-tools directory.
TOOLS="$(dirname "${BASH_SOURCE[0]}")"
. "${TOOLS}/util.sh"
# Directory to check. Default is the parent of the tools themselves.
ROOT="${1:-${TOOLS}/..}"
# required version for this script, if not installed on the host we will
# use the official docker image instead. keep this in sync with SHELLCHECK_IMAGE
@@ -56,15 +59,15 @@ create_container () {
# we're done.
# This is incredibly much faster than creating a container for each shellcheck
# call ...
docker run --name "${SHELLCHECK_CONTAINER}" -d --rm -v "${KUBE_ROOT}:${KUBE_ROOT}" -w "${KUBE_ROOT}" --entrypoint="sleep" "${SHELLCHECK_IMAGE}" 2147483647
docker run --name "${SHELLCHECK_CONTAINER}" -d --rm -v "${ROOT}:${ROOT}" -w "${ROOT}" --entrypoint="sleep" "${SHELLCHECK_IMAGE}" 2147483647
}
# removes the shellcheck container
remove_container () {
docker rm -f "${SHELLCHECK_CONTAINER}" &> /dev/null || true
}
# ensure we're linting the k8s source tree
cd "${KUBE_ROOT}"
# ensure we're linting the source tree
cd "${ROOT}"
# find all shell scripts excluding ./_*, ./.git/*, ./vendor*,
# and anything git-ignored
@@ -78,16 +81,6 @@ done < <(find . -name "*.sh" \
-path ./vendor\* \
\))
# make sure known failures are sorted
failure_file="${KUBE_ROOT}/hack/.shellcheck_failures"
kube::util::check-file-in-alphabetical-order "${failure_file}"
# load known failure files
failing_files=()
while IFS=$'\n' read -r script;
do failing_files+=("$script");
done < <(cat "${failure_file}")
# detect if the host machine has the required shellcheck version installed
# if so, we will use that instead.
HAVE_SHELLCHECK=false
@@ -119,7 +112,6 @@ fi
# lint each script, tracking failures
errors=()
not_failing=()
for f in "${all_shell_scripts[@]}"; do
set +o errexit
if ${HAVE_SHELLCHECK}; then
@@ -129,18 +121,14 @@ for f in "${all_shell_scripts[@]}"; do
shellcheck --exclude="${SHELLCHECK_DISABLED}" "${f}")
fi
set -o errexit
kube::util::array_contains "${f}" "${failing_files[@]}" && in_failing=$? || in_failing=$?
if [[ -n "${failedLint}" ]] && [[ "${in_failing}" -ne "0" ]]; then
errors+=( "${failedLint}" )
fi
if [[ -z "${failedLint}" ]] && [[ "${in_failing}" -eq "0" ]]; then
not_failing+=( "${f}" )
if [[ -n "${failedLint}" ]]; then
errors+=( "${failedLint}" )
fi
done
# Check to be sure all the packages that should pass lint are.
if [ ${#errors[@]} -eq 0 ]; then
echo 'Congratulations! All shell files are passing lint (excluding those in hack/.shellcheck_failures).'
echo 'Congratulations! All shell files are passing lint.'
else
{
echo "Errors from shellcheck:"
@@ -149,38 +137,9 @@ else
done
echo
echo 'Please review the above warnings. You can test via "./hack/verify-shellcheck"'
echo 'If the above warnings do not make sense, you can exempt this package from shellcheck'
echo 'checking by adding it to hack/.shellcheck_failures (if your reviewer is okay with it).'
echo
} >&2
false
fi
if [[ ${#not_failing[@]} -gt 0 ]]; then
{
echo "Some packages in hack/.shellcheck_failures are passing shellcheck. Please remove them."
echo
for f in "${not_failing[@]}"; do
echo " $f"
done
echo
} >&2
false
fi
# Check that all failing_packages actually still exist
gone=()
for f in "${failing_files[@]}"; do
kube::util::array_contains "$f" "${all_shell_scripts[@]}" || gone+=( "$f" )
done
if [[ ${#gone[@]} -gt 0 ]]; then
{
echo "Some files in hack/.shellcheck_failures do not exist anymore. Please remove them."
echo
for f in "${gone[@]}"; do
echo " $f"
done
echo 'If the above warnings do not make sense, you can exempt them from shellcheck'
echo 'checking by adding the "shellcheck disable" directive'
echo '(https://github.com/koalaman/shellcheck/wiki/Directive#disable).'
echo
} >&2
false