Make verify-shellcheck take optional args

This commit is contained in:
Tim Hockin 2024-01-07 11:16:28 -08:00
parent 19b37f046f
commit 2c0da11c0a
No known key found for this signature in database

View File

@ -57,6 +57,8 @@ readonly SHELLCHECK_DISABLED
# ensure we're linting the k8s source tree # ensure we're linting the k8s source tree
cd "${KUBE_ROOT}" cd "${KUBE_ROOT}"
scripts_to_check=("$@")
if [[ "$#" == 0 ]]; then
# Find all shell scripts excluding: # Find all shell scripts excluding:
# - Anything git-ignored - No need to lint untracked files. # - Anything git-ignored - No need to lint untracked files.
# - ./_* - No need to lint output directories. # - ./_* - No need to lint output directories.
@ -64,9 +66,8 @@ cd "${KUBE_ROOT}"
# - ./vendor* - Vendored code should be fixed upstream instead. # - ./vendor* - Vendored code should be fixed upstream instead.
# - ./third_party/*, but re-include ./third_party/forked/* - only code we # - ./third_party/*, but re-include ./third_party/forked/* - only code we
# forked should be linted and fixed. # forked should be linted and fixed.
all_shell_scripts=()
while IFS=$'\n' read -r script; while IFS=$'\n' read -r script;
do git check-ignore -q "$script" || all_shell_scripts+=("$script"); do git check-ignore -q "$script" || scripts_to_check+=("$script");
done < <(find . -name "*.sh" \ done < <(find . -name "*.sh" \
-not \( \ -not \( \
-path ./_\* -o \ -path ./_\* -o \
@ -74,6 +75,7 @@ done < <(find . -name "*.sh" \
-path ./vendor\* -o \ -path ./vendor\* -o \
\( -path ./third_party\* -a -not -path ./third_party/forked\* \) \ \( -path ./third_party\* -a -not -path ./third_party/forked\* \) \
\)) \))
fi
# 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.
@ -110,13 +112,13 @@ SHELLCHECK_OPTIONS=(
res=0 res=0
if ${HAVE_SHELLCHECK}; then if ${HAVE_SHELLCHECK}; then
echo "Using host shellcheck ${SHELLCHECK_VERSION} binary." echo "Using host shellcheck ${SHELLCHECK_VERSION} binary."
shellcheck "${SHELLCHECK_OPTIONS[@]}" "${all_shell_scripts[@]}" >&2 || res=$? shellcheck "${SHELLCHECK_OPTIONS[@]}" "${scripts_to_check[@]}" >&2 || res=$?
else else
echo "Using shellcheck ${SHELLCHECK_VERSION} docker image." echo "Using shellcheck ${SHELLCHECK_VERSION} docker image."
"${DOCKER}" run \ "${DOCKER}" run \
--rm -v "${KUBE_ROOT}:${KUBE_ROOT}" -w "${KUBE_ROOT}" \ --rm -v "${KUBE_ROOT}:${KUBE_ROOT}" -w "${KUBE_ROOT}" \
"${SHELLCHECK_IMAGE}" \ "${SHELLCHECK_IMAGE}" \
shellcheck "${SHELLCHECK_OPTIONS[@]}" "${all_shell_scripts[@]}" >&2 || res=$? shellcheck "${SHELLCHECK_OPTIONS[@]}" "${scripts_to_check[@]}" >&2 || res=$?
fi fi
# print a message based on the result # print a message based on the result