blob: 24eb6cc601cb9453178c2119c7e4c50311973b16 [file] [log] [blame]
Joey Armstrong5c494962023-07-25 16:48:48 -04001#!/bin/bash
2## -----------------------------------------------------------------------
3## Intent:
4## -----------------------------------------------------------------------
5
6##-------------------##
7##---] GLOBALS [---##
8##-------------------##
9set -eu -o pipefail
10umask 0
11
12## -----------------------------------------------------------------------
13## Intent: Display a message then exit with non-zero shell exit status.
14## -----------------------------------------------------------------------
15function error()
16{
17 echo "** ERROR: $*"
18 exit 1
19}
20
21## -----------------------------------------------------------------------
22## Intent: Display program usage
23## -----------------------------------------------------------------------
24function usage
25{
26 cat <<EOH
27Usage: $0
28 --cmd Kind command name to download (w/arch type)
29 --dir Target bin directory to download into
30 --ver Kind command version to download (default=v0.11.0)
31
32[MODEs]
33 --clean Clean download, remove kind binary if it exists.
34EOH
35 exit 1
36}
37
38##----------------##
39##---] MAIN [---##
40##----------------##
41
42WORKSPACE="${WORKSPACE:-$(/bin/pwd)}"
43dir="$WORKSPACE/bin"
44
45bin='kind-linux-amd64'
46ver='v0.11.0'
47
48while [ $# -gt 0 ]; do
49 arg="$1"; shift
50 case "$arg" in
51 -*clean) declare -i clean_mode=1 ;;
52 -*cmd) bin="$1";s shift ;; # cmd-by-arch
53 -*dir) dir="$1"; shift ;; # target dir
54 -*help) usage; exit 0 ;;
55 -*ver) ver="$1"; shift ;; # version
56 *) echo "[SKIP] Unknown argument [$arg]" ;;
57 esac
58done
59
60
61cmd="$dir/kind"
62[[ -v clean_mode ]] && /bin/rm -f "$cmd"
63
64if [ ! -f "$cmd" ]; then
65 mkdir -p "$dir"
66 pushd "$dir" || error "pushd $dir failed"
67 echo "** ${0##*/}: Download ${bin} ${ver}"
68
69 curl --silent -Lo ./kind "https://kind.sigs.k8s.io/dl/${ver}/${bin}"
70 readarray -t file_info < <(file "$cmd")
71
72 ## Validate binary
73 case "${file_info[@]}" in
74 *ASCII*)
75 echo "ERROR: kind command is invalid"
76 set -x; cat "$cmd"; set +x
77 echo
78 /bin/rm -f "$cmd"
79 ;;
80 *) chmod +x ./kind ;;
81 esac
82
83 popd || error "popd $dir failed"
84fi
85
86echo "Kind command version: $($cmd --version)"
87
88# [EOF]