Skip to content

Commit 1eb729b

Browse files
committed
make ensure scripts fail if GOPATH/bin not in PATH
ensure-kind.sh and ensure-kubectl.sh install binaries into $GOPATH/bin, but nowhere it checks that $GOPATH/bin is in user's PATH. In this case, the newly installed binaries won't be found later on.
1 parent 6ce0026 commit 1eb729b

File tree

4 files changed

+29
-0
lines changed

4 files changed

+29
-0
lines changed

hack/ensure-go.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ if [[ "${TRACE-0}" == "1" ]]; then
2222
set -o xtrace
2323
fi
2424

25+
# shellcheck source=./hack/utils.sh
26+
source "$(dirname "${BASH_SOURCE[0]}")/utils.sh"
27+
2528
# Ensure the go tool exists and is a viable version.
2629
verify_go_version() {
2730
if [[ -z "$(command -v go)" ]]; then
@@ -46,7 +49,9 @@ EOF
4649
fi
4750
}
4851

52+
4953
verify_go_version
54+
verify_gopath_bin
5055

5156
# Explicitly opt into go modules, even though we're inside a GOPATH directory
5257
export GO111MODULE=on

hack/ensure-kind.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ if [[ "${TRACE-0}" == "1" ]]; then
2222
set -o xtrace
2323
fi
2424

25+
# shellcheck source=./hack/utils.sh
26+
source "$(dirname "${BASH_SOURCE[0]}")/utils.sh"
27+
2528
GOPATH_BIN="$(go env GOPATH)/bin"
2629
MINIMUM_KIND_VERSION=v0.17.0
2730
goarch="$(go env GOARCH)"
@@ -39,6 +42,7 @@ verify_kind_version() {
3942
fi
4043
curl -sLo "${GOPATH_BIN}/kind" "https://github.com/kubernetes-sigs/kind/releases/download/${MINIMUM_KIND_VERSION}/kind-${goos}-${goarch}"
4144
chmod +x "${GOPATH_BIN}/kind"
45+
verify_gopath_bin
4246
else
4347
echo "Missing required binary in path: kind"
4448
return 2

hack/ensure-kubectl.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ if [[ "${TRACE-0}" == "1" ]]; then
2222
set -o xtrace
2323
fi
2424

25+
26+
# shellcheck source=./hack/utils.sh
27+
source "$(dirname "${BASH_SOURCE[0]}")/utils.sh"
28+
2529
GOPATH_BIN="$(go env GOPATH)/bin/"
2630
MINIMUM_KUBECTL_VERSION=v1.19.0
2731
goarch="$(go env GOARCH)"
@@ -39,6 +43,7 @@ verify_kubectl_version() {
3943
echo 'kubectl not found, installing'
4044
curl -sLo "${GOPATH_BIN}/kubectl" "https://dl.k8s.io/release/${MINIMUM_KUBECTL_VERSION}/bin/${goos}/${goarch}/kubectl"
4145
chmod +x "${GOPATH_BIN}/kubectl"
46+
verify_gopath_bin
4247
else
4348
echo "Missing required binary in path: kubectl"
4449
return 2

hack/utils.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,18 @@ get_capd_root_path() {
3232
cd_capd_root_path() {
3333
cd "$(get_capd_root_path)" || exit
3434
}
35+
36+
# ensure GOPATH/bin is in PATH as we may install binaries to that directory in
37+
# other ensure-* scripts, and expect them to be found in PATH later on
38+
verify_gopath_bin() {
39+
local gopath_bin
40+
41+
gopath_bin="$(go env GOPATH)/bin"
42+
if ! printenv PATH | grep -q "${gopath_bin}"; then
43+
cat <<EOF
44+
error: \$GOPATH/bin=${gopath_bin} is not in your PATH.
45+
See https://go.dev/doc/gopath_code for more instructions.
46+
EOF
47+
return 2
48+
fi
49+
}

0 commit comments

Comments
 (0)