Skip to content

Commit baf4eb6

Browse files
authored
Merge pull request kubernetes#74257 from ipuustin/lib-util-sh
Fix shellcheck-reported errors in hack/lib/util.sh.
2 parents f5574bf + f7e1058 commit baf4eb6

File tree

2 files changed

+58
-36
lines changed

2 files changed

+58
-36
lines changed

hack/.shellcheck_failures

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
./hack/lib/protoc.sh
4242
./hack/lib/swagger.sh
4343
./hack/lib/test.sh
44-
./hack/lib/util.sh
4544
./hack/lib/version.sh
4645
./hack/list-feature-tests.sh
4746
./hack/local-up-cluster.sh

hack/lib/util.sh

Lines changed: 58 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616

17+
function kube::util::sourced_variable {
18+
# Call this function to tell shellcheck that a variable is supposed to
19+
# be used from other calling context. This helps quiet an "unused
20+
# variable" warning from shellcheck and also document your code.
21+
true
22+
}
23+
1724
kube::util::sortable_date() {
1825
date "+%Y%m%d-%H%M%S"
1926
}
@@ -39,7 +46,7 @@ kube::util::wait_for_url() {
3946
local times=${4:-30}
4047
local maxtime=${5:-1}
4148

42-
which curl >/dev/null || {
49+
command -v curl >/dev/null || {
4350
kube::log::usage "curl must be installed"
4451
exit 1
4552
}
@@ -69,15 +76,19 @@ kube::util::trap_add() {
6976
local new_cmd
7077

7178
# Grab the currently defined trap commands for this trap
72-
existing_cmd=`trap -p "${trap_add_name}" | awk -F"'" '{print $2}'`
79+
existing_cmd=$(trap -p "${trap_add_name}" | awk -F"'" '{print $2}')
7380

7481
if [[ -z "${existing_cmd}" ]]; then
7582
new_cmd="${trap_add_cmd}"
7683
else
7784
new_cmd="${trap_add_cmd};${existing_cmd}"
7885
fi
7986

80-
# Assign the test
87+
# Assign the test. Disable the shellcheck warning telling that trap
88+
# commands should be single quoted to avoid evaluating them at this
89+
# point instead evaluating them at run time. The logic of adding new
90+
# commands to a single trap requires them to be evaluated right away.
91+
# shellcheck disable=SC2064
8192
trap "${new_cmd}" "${trap_add_name}"
8293
done
8394
}
@@ -173,8 +184,10 @@ kube::util::find-binary-for-platform() {
173184
# The bazel go rules place some binaries in subtrees like
174185
# "bazel-bin/source/path/linux_amd64_pure_stripped/binaryname", so make sure
175186
# the platform name is matched in the path.
176-
locations+=($(find "${KUBE_ROOT}/bazel-bin/" -type f -executable \
177-
\( -path "*/${platform/\//_}*/${lookfor}" -o -path "*/${lookfor}" \) 2>/dev/null || true) )
187+
while IFS=$'\n' read -r location; do
188+
locations+=("$location");
189+
done < <(find "${KUBE_ROOT}/bazel-bin/" -type f -executable \
190+
\( -path "*/${platform/\//_}*/${lookfor}" -o -path "*/${lookfor}" \) 2>/dev/null || true)
178191

179192
# List most recently-updated location.
180193
local -r bin=$( (ls -t "${locations[@]}" 2>/dev/null || true) | head -1 )
@@ -197,6 +210,10 @@ kube::util::gen-docs() {
197210
genyaml=$(kube::util::find-binary "genyaml")
198211
genfeddocs=$(kube::util::find-binary "genfeddocs")
199212

213+
# TODO: If ${genfeddocs} is not used from anywhere (it isn't used at
214+
# least from k/k tree), remove it completely.
215+
kube::util::sourced_variable "${genfeddocs}"
216+
200217
mkdir -p "${dest}/docs/user-guide/kubectl/"
201218
"${gendocs}" "${dest}/docs/user-guide/kubectl/"
202219
mkdir -p "${dest}/docs/admin/"
@@ -222,18 +239,18 @@ kube::util::gen-docs() {
222239
"${genyaml}" "${dest}/docs/yaml/kubectl/"
223240

224241
# create the list of generated files
225-
pushd "${dest}" > /dev/null
242+
pushd "${dest}" > /dev/null || return 1
226243
touch docs/.generated_docs
227244
find . -type f | cut -sd / -f 2- | LC_ALL=C sort > docs/.generated_docs
228-
popd > /dev/null
245+
popd > /dev/null || return 1
229246
}
230247

231248
# Removes previously generated docs-- we don't want to check them in. $KUBE_ROOT
232249
# must be set.
233250
kube::util::remove-gen-docs() {
234251
if [ -e "${KUBE_ROOT}/docs/.generated_docs" ]; then
235252
# remove all of the old docs; we don't want to check them in.
236-
while read file; do
253+
while read -r file; do
237254
rm "${KUBE_ROOT}/${file}" 2>/dev/null || true
238255
done <"${KUBE_ROOT}/docs/.generated_docs"
239256
# The docs/.generated_docs file lists itself, so we don't need to explicitly
@@ -249,18 +266,14 @@ kube::util::remove-gen-docs() {
249266
# * Special handling for groups suffixed with ".k8s.io": foo.k8s.io/v1 -> apis/foo/v1
250267
# * Very special handling for when both group and version are "": / -> api
251268
kube::util::group-version-to-pkg-path() {
252-
staging_apis=(
253-
$(
254-
cd "${KUBE_ROOT}/staging/src/k8s.io/api" &&
255-
find . -name types.go -exec dirname {} \; | sed "s|\./||g" | sort
256-
))
257-
258269
local group_version="$1"
259270

260-
if [[ " ${staging_apis[@]} " =~ " ${group_version/.*k8s.io/} " ]]; then
261-
echo "vendor/k8s.io/api/${group_version/.*k8s.io/}"
262-
return
263-
fi
271+
while IFS=$'\n' read -r api; do
272+
if [[ "${api}" = "${group_version/.*k8s.io/}" ]]; then
273+
echo "vendor/k8s.io/api/${group_version/.*k8s.io/}"
274+
return
275+
fi
276+
done < <(cd "${KUBE_ROOT}/staging/src/k8s.io/api" && find . -name types.go -exec dirname {} \; | sed "s|\./||g" | sort)
264277

265278
# "v1" is the API GroupVersion
266279
if [[ "${group_version}" == "v1" ]]; then
@@ -322,7 +335,7 @@ kube::util::git_upstream_remote_name() {
322335
kube::util::create-fake-git-tree() {
323336
local -r target_dir=${1:-$(pwd)}
324337

325-
pushd "${target_dir}" >/dev/null
338+
pushd "${target_dir}" >/dev/null || return 1
326339
git init >/dev/null
327340
git config --local user.email "[email protected]"
328341
git config --local user.name "$0"
@@ -331,7 +344,7 @@ kube::util::create-fake-git-tree() {
331344
if (( ${KUBE_VERBOSE:-5} >= 6 )); then
332345
kube::log::status "${target_dir} is now a git tree."
333346
fi
334-
popd >/dev/null
347+
popd >/dev/null || return 1
335348
}
336349

337350
# Checks whether godep restore was run in the current GOPATH, i.e. that all referenced repos exist
@@ -344,16 +357,16 @@ kube::util::godep_restored() {
344357

345358
local root
346359
local old_rev=""
347-
while read path rev; do
348-
rev=$(echo "${rev}" | sed "s/['\"]//g") # remove quotes which are around revs sometimes
360+
while read -r path rev; do
361+
rev="${rev//[\'\"]}" # remove quotes which are around revs sometimes
349362

350363
if [[ "${rev}" == "${old_rev}" ]] && [[ "${path}" == "${root}"* ]]; then
351364
# avoid checking the same git/hg root again
352365
continue
353366
fi
354367

355368
root="${path}"
356-
while [ "${root}" != "." -a ! -d "${gopath}/src/${root}/.git" -a ! -d "${gopath}/src/${root}/.hg" ]; do
369+
while [ "${root}" != "." ] && [ ! -d "${gopath}/src/${root}/.git" ] && [ ! -d "${gopath}/src/${root}/.hg" ]; do
357370
root=$(dirname "${root}")
358371
done
359372
if [ "${root}" == "." ]; then
@@ -387,7 +400,7 @@ kube::util::ensure_clean_working_dir() {
387400
exit 1
388401
fi | sed 's/^/ /'
389402
echo -e "\nCommit your changes in another terminal and then continue here by pressing enter."
390-
read
403+
read -r
391404
done 1>&2
392405
}
393406

@@ -479,7 +492,8 @@ kube::util::has_changes() {
479492
local -r pattern=$2
480493
local -r not_pattern=${3:-totallyimpossiblepattern}
481494

482-
local base_ref=$(kube::util::base_ref "${git_branch}")
495+
local base_ref
496+
base_ref=$(kube::util::base_ref "${git_branch}")
483497
echo "Checking for '${pattern}' changes against '${base_ref}'"
484498

485499
# notice this uses ... to find the first shared ancestor
@@ -499,11 +513,11 @@ kube::util::download_file() {
499513
local -r url=$1
500514
local -r destination_file=$2
501515

502-
rm ${destination_file} 2&> /dev/null || true
516+
rm "${destination_file}" 2&> /dev/null || true
503517

504518
for i in $(seq 5)
505519
do
506-
if ! curl -fsSL --retry 3 --keepalive-time 2 ${url} -o ${destination_file}; then
520+
if ! curl -fsSL --retry 3 --keepalive-time 2 "${url}" -o "${destination_file}"; then
507521
echo "Downloading ${url} failed. $((5-i)) retries left."
508522
sleep 1
509523
else
@@ -518,8 +532,7 @@ kube::util::download_file() {
518532
# Sets:
519533
# OPENSSL_BIN: The path to the openssl binary to use
520534
function kube::util::test_openssl_installed {
521-
openssl version >& /dev/null
522-
if [ "$?" != "0" ]; then
535+
if ! openssl version >& /dev/null; then
523536
echo "Failed to run openssl. Please ensure openssl is installed"
524537
exit 1
525538
fi
@@ -602,7 +615,7 @@ function kube::util::write_client_kubeconfig {
602615
local api_port=$5
603616
local client_id=$6
604617
local token=${7:-}
605-
cat <<EOF | ${sudo} tee "${dest_dir}"/${client_id}.kubeconfig > /dev/null
618+
cat <<EOF | ${sudo} tee "${dest_dir}"/"${client_id}".kubeconfig > /dev/null
606619
apiVersion: v1
607620
kind: Config
608621
clusters:
@@ -635,7 +648,8 @@ EOF
635648

636649
# Determines if docker can be run, failures may simply require that the user be added to the docker group.
637650
function kube::util::ensure_docker_daemon_connectivity {
638-
DOCKER=(docker ${DOCKER_OPTS})
651+
IFS=" " read -ra DOCKER <<< "${DOCKER_OPTS}"
652+
DOCKER=(docker "${DOCKER[@]}")
639653
if ! "${DOCKER[@]}" info > /dev/null 2>&1 ; then
640654
cat <<'EOF' >&2
641655
Can't connect to 'docker' daemon. please fix and retry.
@@ -713,7 +727,7 @@ function kube::util::ensure-cfssl {
713727
fi
714728

715729
mkdir -p "${cfssldir}"
716-
pushd "${cfssldir}" > /dev/null
730+
pushd "${cfssldir}" > /dev/null || return 1
717731

718732
echo "Unable to successfully run 'cfssl' from ${PATH}; downloading instead..."
719733
kernel=$(uname -s)
@@ -742,7 +756,7 @@ function kube::util::ensure-cfssl {
742756
echo "Hint: export PATH=\$PATH:\$GOPATH/bin; go get -u github.com/cloudflare/cfssl/cmd/..."
743757
exit 1
744758
fi
745-
popd > /dev/null
759+
popd > /dev/null || return 1
746760
}
747761

748762
# kube::util::ensure_dockerized
@@ -766,12 +780,13 @@ function kube::util::ensure_dockerized {
766780
function kube::util::ensure-gnu-sed {
767781
if LANG=C sed --help 2>&1 | grep -q GNU; then
768782
SED="sed"
769-
elif which gsed &>/dev/null; then
783+
elif command -v gsed &>/dev/null; then
770784
SED="gsed"
771785
else
772786
kube::log::error "Failed to find GNU sed as sed or gsed. If you are on Mac: brew install gnu-sed." >&2
773787
return 1
774788
fi
789+
kube::util::sourced_variable "${SED}"
775790
}
776791

777792
# kube::util::check-file-in-alphabetical-order <file>
@@ -794,7 +809,7 @@ function kube::util::check-file-in-alphabetical-order {
794809
# kube::util::require-jq
795810
# Checks whether jq is installed.
796811
function kube::util::require-jq {
797-
if ! which jq &>/dev/null; then
812+
if ! command -v jq &>/dev/null; then
798813
echo "jq not found. Please install." 1>&2
799814
return 1
800815
fi
@@ -809,6 +824,14 @@ if [[ -z "${color_start-}" ]]; then
809824
declare -r color_blue="${color_start}1;34m"
810825
declare -r color_cyan="${color_start}1;36m"
811826
declare -r color_norm="${color_start}0m"
827+
828+
kube::util::sourced_variable "${color_start}"
829+
kube::util::sourced_variable "${color_red}"
830+
kube::util::sourced_variable "${color_yellow}"
831+
kube::util::sourced_variable "${color_green}"
832+
kube::util::sourced_variable "${color_blue}"
833+
kube::util::sourced_variable "${color_cyan}"
834+
kube::util::sourced_variable "${color_norm}"
812835
fi
813836

814837
# ex: ts=2 sw=2 et filetype=sh

0 commit comments

Comments
 (0)