Skip to content

Commit b5c4382

Browse files
committed
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.
1 parent fb13c51 commit b5c4382

File tree

1 file changed

+15
-56
lines changed

1 file changed

+15
-56
lines changed

verify-shellcheck.sh

Lines changed: 15 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,12 @@ set -o errexit
1818
set -o nounset
1919
set -o pipefail
2020

21-
KUBE_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
22-
source "${KUBE_ROOT}/hack/lib/init.sh"
23-
source "${KUBE_ROOT}/hack/lib/util.sh"
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}/..}"
2427

2528
# required version for this script, if not installed on the host we will
2629
# use the official docker image instead. keep this in sync with SHELLCHECK_IMAGE
@@ -56,15 +59,15 @@ create_container () {
5659
# we're done.
5760
# This is incredibly much faster than creating a container for each shellcheck
5861
# call ...
59-
docker run --name "${SHELLCHECK_CONTAINER}" -d --rm -v "${KUBE_ROOT}:${KUBE_ROOT}" -w "${KUBE_ROOT}" --entrypoint="sleep" "${SHELLCHECK_IMAGE}" 2147483647
62+
docker run --name "${SHELLCHECK_CONTAINER}" -d --rm -v "${ROOT}:${ROOT}" -w "${ROOT}" --entrypoint="sleep" "${SHELLCHECK_IMAGE}" 2147483647
6063
}
6164
# removes the shellcheck container
6265
remove_container () {
6366
docker rm -f "${SHELLCHECK_CONTAINER}" &> /dev/null || true
6467
}
6568

66-
# ensure we're linting the k8s source tree
67-
cd "${KUBE_ROOT}"
69+
# ensure we're linting the source tree
70+
cd "${ROOT}"
6871

6972
# find all shell scripts excluding ./_*, ./.git/*, ./vendor*,
7073
# and anything git-ignored
@@ -78,16 +81,6 @@ done < <(find . -name "*.sh" \
7881
-path ./vendor\* \
7982
\))
8083

81-
# make sure known failures are sorted
82-
failure_file="${KUBE_ROOT}/hack/.shellcheck_failures"
83-
kube::util::check-file-in-alphabetical-order "${failure_file}"
84-
85-
# load known failure files
86-
failing_files=()
87-
while IFS=$'\n' read -r script;
88-
do failing_files+=("$script");
89-
done < <(cat "${failure_file}")
90-
9184
# detect if the host machine has the required shellcheck version installed
9285
# if so, we will use that instead.
9386
HAVE_SHELLCHECK=false
@@ -119,7 +112,6 @@ fi
119112

120113
# lint each script, tracking failures
121114
errors=()
122-
not_failing=()
123115
for f in "${all_shell_scripts[@]}"; do
124116
set +o errexit
125117
if ${HAVE_SHELLCHECK}; then
@@ -129,18 +121,14 @@ for f in "${all_shell_scripts[@]}"; do
129121
shellcheck --exclude="${SHELLCHECK_DISABLED}" "${f}")
130122
fi
131123
set -o errexit
132-
kube::util::array_contains "${f}" "${failing_files[@]}" && in_failing=$? || in_failing=$?
133-
if [[ -n "${failedLint}" ]] && [[ "${in_failing}" -ne "0" ]]; then
134-
errors+=( "${failedLint}" )
135-
fi
136-
if [[ -z "${failedLint}" ]] && [[ "${in_failing}" -eq "0" ]]; then
137-
not_failing+=( "${f}" )
124+
if [[ -n "${failedLint}" ]]; then
125+
errors+=( "${failedLint}" )
138126
fi
139127
done
140128

141129
# Check to be sure all the packages that should pass lint are.
142130
if [ ${#errors[@]} -eq 0 ]; then
143-
echo 'Congratulations! All shell files are passing lint (excluding those in hack/.shellcheck_failures).'
131+
echo 'Congratulations! All shell files are passing lint.'
144132
else
145133
{
146134
echo "Errors from shellcheck:"
@@ -149,38 +137,9 @@ else
149137
done
150138
echo
151139
echo 'Please review the above warnings. You can test via "./hack/verify-shellcheck"'
152-
echo 'If the above warnings do not make sense, you can exempt this package from shellcheck'
153-
echo 'checking by adding it to hack/.shellcheck_failures (if your reviewer is okay with it).'
154-
echo
155-
} >&2
156-
false
157-
fi
158-
159-
if [[ ${#not_failing[@]} -gt 0 ]]; then
160-
{
161-
echo "Some packages in hack/.shellcheck_failures are passing shellcheck. Please remove them."
162-
echo
163-
for f in "${not_failing[@]}"; do
164-
echo " $f"
165-
done
166-
echo
167-
} >&2
168-
false
169-
fi
170-
171-
# Check that all failing_packages actually still exist
172-
gone=()
173-
for f in "${failing_files[@]}"; do
174-
kube::util::array_contains "$f" "${all_shell_scripts[@]}" || gone+=( "$f" )
175-
done
176-
177-
if [[ ${#gone[@]} -gt 0 ]]; then
178-
{
179-
echo "Some files in hack/.shellcheck_failures do not exist anymore. Please remove them."
180-
echo
181-
for f in "${gone[@]}"; do
182-
echo " $f"
183-
done
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).'
184143
echo
185144
} >&2
186145
false

0 commit comments

Comments
 (0)