Skip to content

Commit a52af3a

Browse files
committed
fix shellcheck failure golang.sh
1 parent c48ed1c commit a52af3a

File tree

2 files changed

+30
-25
lines changed

2 files changed

+30
-25
lines changed

hack/.shellcheck_failures

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
./hack/jenkins/test-dockerized.sh
3535
./hack/jenkins/upload-to-gcs.sh
3636
./hack/jenkins/verify-dockerized.sh
37-
./hack/lib/golang.sh
3837
./hack/lib/init.sh
3938
./hack/lib/logging.sh
4039
./hack/lib/protoc.sh

hack/lib/golang.sh

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -345,12 +345,13 @@ kube::golang::unset_platform_envs() {
345345
# Create the GOPATH tree under $KUBE_OUTPUT
346346
kube::golang::create_gopath_tree() {
347347
local go_pkg_dir="${KUBE_GOPATH}/src/${KUBE_GO_PACKAGE}"
348-
local go_pkg_basedir=$(dirname "${go_pkg_dir}")
348+
local go_pkg_basedir
349+
go_pkg_basedir=$(dirname "${go_pkg_dir}")
349350

350351
mkdir -p "${go_pkg_basedir}"
351352

352353
# TODO: This symlink should be relative.
353-
if [[ ! -e "${go_pkg_dir}" || "$(readlink ${go_pkg_dir})" != "${KUBE_ROOT}" ]]; then
354+
if [[ ! -e "${go_pkg_dir}" || "$(readlink "${go_pkg_dir}")" != "${KUBE_ROOT}" ]]; then
354355
ln -snf "${KUBE_ROOT}" "${go_pkg_dir}"
355356
fi
356357

@@ -427,10 +428,11 @@ kube::golang::setup_env() {
427428
# cross-compiling, and `go install -o <file>` only works for a single pkg.
428429
local subdir
429430
subdir=$(kube::realpath . | sed "s|${KUBE_ROOT}||")
430-
cd "${KUBE_GOPATH}/src/${KUBE_GO_PACKAGE}/${subdir}"
431+
cd "${KUBE_GOPATH}/src/${KUBE_GO_PACKAGE}/${subdir}" || return 1
431432

432433
# Set GOROOT so binaries that parse code can work properly.
433-
export GOROOT=$(go env GOROOT)
434+
GOROOT=$(go env GOROOT)
435+
export GOROOT
434436

435437
# Unset GOBIN in case it already exists in the current session.
436438
unset GOBIN
@@ -479,10 +481,11 @@ kube::golang::outfile_for_binary() {
479481
local binary=$1
480482
local platform=$2
481483
local output_path="${KUBE_GOPATH}/bin"
484+
local bin
485+
bin=$(basename "${binary}")
482486
if [[ "${platform}" != "${host_platform}" ]]; then
483487
output_path="${output_path}/${platform//\//_}"
484488
fi
485-
local bin=$(basename "${binary}")
486489
if [[ ${GOOS} == "windows" ]]; then
487490
bin="${bin}.exe"
488491
fi
@@ -493,15 +496,16 @@ kube::golang::outfile_for_binary() {
493496
# Returns 0 if the binary can be built with coverage, 1 otherwise.
494497
# NB: this ignores whether coverage is globally enabled or not.
495498
kube::golang::is_instrumented_package() {
496-
return $(kube::util::array_contains "$1" "${KUBE_COVERAGE_INSTRUMENTED_PACKAGES[@]}")
499+
return "$(kube::util::array_contains "$1" "${KUBE_COVERAGE_INSTRUMENTED_PACKAGES[@]}")"
497500
}
498501

499502
# Argument: the name of a Kubernetes package (e.g. k8s.io/kubernetes/cmd/kube-scheduler)
500503
# Echos the path to a dummy test used for coverage information.
501504
kube::golang::path_for_coverage_dummy_test() {
502505
local package="$1"
503506
local path="${KUBE_GOPATH}/src/${package}"
504-
local name=$(basename "${package}")
507+
local name
508+
name=$(basename "${package}")
505509
echo "${path}/zz_generated_${name}_test.go"
506510
}
507511

@@ -510,8 +514,9 @@ kube::golang::path_for_coverage_dummy_test() {
510514
# This unit test will invoke the package's standard entry point when run.
511515
kube::golang::create_coverage_dummy_test() {
512516
local package="$1"
513-
local name="$(basename "${package}")"
514-
cat <<EOF > $(kube::golang::path_for_coverage_dummy_test "${package}")
517+
local name
518+
name="$(basename "${package}")"
519+
cat <<EOF > "$(kube::golang::path_for_coverage_dummy_test "${package}")"
515520
package main
516521
import (
517522
"testing"
@@ -539,7 +544,7 @@ EOF
539544
# It is not an error to call this for a nonexistent test.
540545
kube::golang::delete_coverage_dummy_test() {
541546
local package="$1"
542-
rm -f $(kube::golang::path_for_coverage_dummy_test "${package}")
547+
rm -f "$(kube::golang::path_for_coverage_dummy_test "${package}")"
543548
}
544549

545550
# Arguments: a list of kubernetes packages to build.
@@ -572,7 +577,7 @@ kube::golang::build_some_binaries() {
572577
fi
573578
done
574579
if [[ "${#uncovered[@]}" != 0 ]]; then
575-
V=2 kube::log::info "Building ${uncovered[@]} without coverage..."
580+
V=2 kube::log::info "Building ${uncovered[*]} without coverage..."
576581
go install "${build_args[@]}" "${uncovered[@]}"
577582
else
578583
V=2 kube::log::info "Nothing to build without coverage."
@@ -594,11 +599,11 @@ kube::golang::build_binaries_for_platform() {
594599

595600
for binary in "${binaries[@]}"; do
596601
if [[ "${binary}" =~ ".test"$ ]]; then
597-
tests+=(${binary})
602+
tests+=("${binary}")
598603
elif kube::golang::is_statically_linked_library "${binary}"; then
599-
statics+=(${binary})
604+
statics+=("${binary}")
600605
else
601-
nonstatics+=(${binary})
606+
nonstatics+=("${binary}")
602607
fi
603608
done
604609

@@ -625,10 +630,11 @@ kube::golang::build_binaries_for_platform() {
625630
fi
626631

627632
for test in "${tests[@]:+${tests[@]}}"; do
628-
local outfile=$(kube::golang::outfile_for_binary "${test}" "${platform}")
629-
local testpkg="$(dirname ${test})"
633+
local outfile testpkg
634+
outfile=$(kube::golang::outfile_for_binary "${test}" "${platform}")
635+
testpkg=$(dirname "${test}")
630636

631-
mkdir -p "$(dirname ${outfile})"
637+
mkdir -p "$(dirname "${outfile}")"
632638
go test -c \
633639
${goflags:+"${goflags[@]}"} \
634640
-gcflags "${gogcflags:-}" \
@@ -645,13 +651,13 @@ kube::golang::get_physmem() {
645651

646652
# Linux kernel version >=3.14, in kb
647653
if mem=$(grep MemAvailable /proc/meminfo | awk '{ print $2 }'); then
648-
echo $(( ${mem} / 1048576 ))
654+
echo $(( mem / 1048576 ))
649655
return
650656
fi
651657

652658
# Linux, in kb
653659
if mem=$(grep MemTotal /proc/meminfo | awk '{ print $2 }'); then
654-
echo $(( ${mem} / 1048576 ))
660+
echo $(( mem / 1048576 ))
655661
return
656662
fi
657663

@@ -660,7 +666,7 @@ kube::golang::get_physmem() {
660666
# platform case, which is a Dockerized build), but this is provided
661667
# for completeness.
662668
if mem=$(sysctl -n hw.memsize 2>/dev/null); then
663-
echo $(( ${mem} / 1073741824 ))
669+
echo $(( mem / 1073741824 ))
664670
return
665671
fi
666672

@@ -713,7 +719,7 @@ kube::golang::build_binaries() {
713719
fi
714720

715721
local binaries
716-
binaries=($(kube::golang::binaries_from_targets "${targets[@]}"))
722+
while IFS="" read -r binary; do binaries+=("$binary"); done < <(kube::golang::binaries_from_targets "${targets[@]}")
717723

718724
local parallel=false
719725
if [[ ${#platforms[@]} -gt 1 ]]; then
@@ -735,14 +741,14 @@ kube::golang::build_binaries() {
735741
for platform in "${platforms[@]}"; do (
736742
kube::golang::set_platform_envs "${platform}"
737743
kube::log::status "${platform}: build started"
738-
kube::golang::build_binaries_for_platform ${platform}
744+
kube::golang::build_binaries_for_platform "${platform}"
739745
kube::log::status "${platform}: build finished"
740746
) &> "/tmp//${platform//\//_}.build" &
741747
done
742748

743749
local fails=0
744750
for job in $(jobs -p); do
745-
wait ${job} || let "fails+=1"
751+
wait "${job}" || (( fails+=1 ))
746752
done
747753

748754
for platform in "${platforms[@]}"; do
@@ -755,7 +761,7 @@ kube::golang::build_binaries() {
755761
kube::log::status "Building go targets for ${platform}:" "${targets[@]}"
756762
(
757763
kube::golang::set_platform_envs "${platform}"
758-
kube::golang::build_binaries_for_platform ${platform}
764+
kube::golang::build_binaries_for_platform "${platform}"
759765
)
760766
done
761767
fi

0 commit comments

Comments
 (0)