Commit b2d25d4f by Patrick Ohly

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.
parent fb13c519
...@@ -18,9 +18,12 @@ set -o errexit ...@@ -18,9 +18,12 @@ set -o errexit
set -o nounset set -o nounset
set -o pipefail set -o pipefail
KUBE_ROOT=$(dirname "${BASH_SOURCE[0]}")/.. # The csi-release-tools directory.
source "${KUBE_ROOT}/hack/lib/init.sh" TOOLS="$(dirname "${BASH_SOURCE[0]}")"
source "${KUBE_ROOT}/hack/lib/util.sh" . "${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 # 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 # use the official docker image instead. keep this in sync with SHELLCHECK_IMAGE
...@@ -56,15 +59,15 @@ create_container () { ...@@ -56,15 +59,15 @@ create_container () {
# we're done. # we're done.
# This is incredibly much faster than creating a container for each shellcheck # This is incredibly much faster than creating a container for each shellcheck
# call ... # 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 # removes the shellcheck container
remove_container () { remove_container () {
docker rm -f "${SHELLCHECK_CONTAINER}" &> /dev/null || true docker rm -f "${SHELLCHECK_CONTAINER}" &> /dev/null || true
} }
# ensure we're linting the k8s source tree # ensure we're linting the source tree
cd "${KUBE_ROOT}" cd "${ROOT}"
# find all shell scripts excluding ./_*, ./.git/*, ./vendor*, # find all shell scripts excluding ./_*, ./.git/*, ./vendor*,
# and anything git-ignored # and anything git-ignored
...@@ -78,16 +81,6 @@ done < <(find . -name "*.sh" \ ...@@ -78,16 +81,6 @@ done < <(find . -name "*.sh" \
-path ./vendor\* \ -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 # detect if the host machine has the required shellcheck version installed
# if so, we will use that instead. # if so, we will use that instead.
HAVE_SHELLCHECK=false HAVE_SHELLCHECK=false
...@@ -119,7 +112,6 @@ fi ...@@ -119,7 +112,6 @@ fi
# lint each script, tracking failures # lint each script, tracking failures
errors=() errors=()
not_failing=()
for f in "${all_shell_scripts[@]}"; do for f in "${all_shell_scripts[@]}"; do
set +o errexit set +o errexit
if ${HAVE_SHELLCHECK}; then if ${HAVE_SHELLCHECK}; then
...@@ -129,18 +121,14 @@ for f in "${all_shell_scripts[@]}"; do ...@@ -129,18 +121,14 @@ for f in "${all_shell_scripts[@]}"; do
shellcheck --exclude="${SHELLCHECK_DISABLED}" "${f}") shellcheck --exclude="${SHELLCHECK_DISABLED}" "${f}")
fi fi
set -o errexit set -o errexit
kube::util::array_contains "${f}" "${failing_files[@]}" && in_failing=$? || in_failing=$? if [[ -n "${failedLint}" ]]; then
if [[ -n "${failedLint}" ]] && [[ "${in_failing}" -ne "0" ]]; then errors+=( "${failedLint}" )
errors+=( "${failedLint}" )
fi
if [[ -z "${failedLint}" ]] && [[ "${in_failing}" -eq "0" ]]; then
not_failing+=( "${f}" )
fi fi
done done
# Check to be sure all the packages that should pass lint are. # Check to be sure all the packages that should pass lint are.
if [ ${#errors[@]} -eq 0 ]; then 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 else
{ {
echo "Errors from shellcheck:" echo "Errors from shellcheck:"
...@@ -149,38 +137,9 @@ else ...@@ -149,38 +137,9 @@ else
done done
echo echo
echo 'Please review the above warnings. You can test via "./hack/verify-shellcheck"' 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 'If the above warnings do not make sense, you can exempt them from shellcheck'
echo 'checking by adding it to hack/.shellcheck_failures (if your reviewer is okay with it).' echo 'checking by adding the "shellcheck disable" directive'
echo echo '(https://github.com/koalaman/shellcheck/wiki/Directive#disable).'
} >&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 echo
} >&2 } >&2
false false
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment