Joey Armstrong | 5c49496 | 2023-07-25 16:48:48 -0400 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | ## ----------------------------------------------------------------------- |
| 3 | ## Intent: |
| 4 | ## ----------------------------------------------------------------------- |
| 5 | |
| 6 | ##-------------------## |
| 7 | ##---] GLOBALS [---## |
| 8 | ##-------------------## |
| 9 | set -eu -o pipefail |
| 10 | umask 0 |
| 11 | |
| 12 | ## ----------------------------------------------------------------------- |
| 13 | ## Intent: Display a message then exit with non-zero shell exit status. |
| 14 | ## ----------------------------------------------------------------------- |
| 15 | function error() |
| 16 | { |
| 17 | echo "** ERROR: $*" |
| 18 | exit 1 |
| 19 | } |
| 20 | |
| 21 | ## ----------------------------------------------------------------------- |
| 22 | ## Intent: Display program usage |
| 23 | ## ----------------------------------------------------------------------- |
| 24 | function usage |
| 25 | { |
| 26 | cat <<EOH |
| 27 | Usage: $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. |
| 34 | EOH |
| 35 | exit 1 |
| 36 | } |
| 37 | |
| 38 | ##----------------## |
| 39 | ##---] MAIN [---## |
| 40 | ##----------------## |
| 41 | |
| 42 | WORKSPACE="${WORKSPACE:-$(/bin/pwd)}" |
| 43 | dir="$WORKSPACE/bin" |
| 44 | |
| 45 | bin='kind-linux-amd64' |
| 46 | ver='v0.11.0' |
| 47 | |
| 48 | while [ $# -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 |
| 58 | done |
| 59 | |
| 60 | |
| 61 | cmd="$dir/kind" |
| 62 | [[ -v clean_mode ]] && /bin/rm -f "$cmd" |
| 63 | |
| 64 | if [ ! -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" |
| 84 | fi |
| 85 | |
| 86 | echo "Kind command version: $($cmd --version)" |
| 87 | |
| 88 | # [EOF] |