Joey Armstrong | f128de8 | 2023-09-08 17:05:18 -0400 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | set -e |
| 3 | # Code generated by godownloader on 2019-05-28T19:49:53Z. DO NOT EDIT. |
| 4 | # Copyright 2019 - (disable makefiles/lint/license.mk) |
| 5 | # |
| 6 | |
| 7 | usage() { |
| 8 | this=$1 |
| 9 | cat <<EOF |
| 10 | $this: download go binaries for boz/kail |
| 11 | |
| 12 | Usage: $this [-b] bindir [-d] [tag] |
| 13 | -b sets bindir or installation directory, Defaults to ./bin |
| 14 | -d turns on debug logging |
| 15 | [tag] is a tag from |
| 16 | https://github.com/boz/kail/releases |
| 17 | If tag is missing, then the latest will be used. |
| 18 | |
| 19 | Generated by godownloader |
| 20 | https://github.com/goreleaser/godownloader |
| 21 | |
| 22 | EOF |
| 23 | exit 2 |
| 24 | } |
| 25 | |
| 26 | parse_args() { |
| 27 | #BINDIR is ./bin unless set be ENV |
| 28 | # over-ridden by flag below |
| 29 | |
| 30 | BINDIR=${BINDIR:-./bin} |
| 31 | while getopts "b:dh?x" arg; do |
| 32 | case "$arg" in |
| 33 | b) BINDIR="$OPTARG" ;; |
| 34 | d) log_set_priority 10 ;; |
| 35 | h | \?) usage "$0" ;; |
| 36 | x) set -x ;; |
| 37 | esac |
| 38 | done |
| 39 | shift $((OPTIND - 1)) |
| 40 | TAG=$1 |
| 41 | } |
| 42 | # this function wraps all the destructive operations |
| 43 | # if a curl|bash cuts off the end of the script due to |
| 44 | # network, either nothing will happen or will syntax error |
| 45 | # out preventing half-done work |
| 46 | execute() { |
| 47 | tmpdir=$(mktemp -d) |
| 48 | log_debug "downloading files into ${tmpdir}" |
| 49 | |
| 50 | # --------------------------------------------------------- |
| 51 | # [JOEY] - strange, curl fails to retrieve but wget is fine. |
| 52 | # --------------------------------------------------------- |
| 53 | # http_download "${tmpdir}/${TARBALL}" "${TARBALL_URL}" |
| 54 | http_download_wget "${tmpdir}/${TARBALL}" "${TARBALL_URL}" |
| 55 | |
| 56 | http_download "${tmpdir}/${CHECKSUM}" "${CHECKSUM_URL}" |
| 57 | hash_sha256_verify "${tmpdir}/${TARBALL}" "${tmpdir}/${CHECKSUM}" |
| 58 | srcdir="${tmpdir}" |
| 59 | (cd "${tmpdir}" && untar "${TARBALL}") |
| 60 | test ! -d "${BINDIR}" && install -d "${BINDIR}" |
| 61 | # shellcheck disable=SC2043 |
| 62 | for binexe in "kail" ; do |
| 63 | if [ "$OS" = "windows" ]; then |
| 64 | binexe="${binexe}.exe" |
| 65 | fi |
| 66 | install "${srcdir}/${binexe}" "${BINDIR}/" |
| 67 | log_info "installed ${BINDIR}/${binexe}" |
| 68 | done |
| 69 | rm -rf "${tmpdir}" |
| 70 | } |
| 71 | is_supported_platform() { |
| 72 | platform=$1 |
| 73 | found=1 |
| 74 | case "$platform" in |
| 75 | darwin/amd64) found=0 ;; |
| 76 | linux/amd64) found=0 ;; |
| 77 | esac |
| 78 | return $found |
| 79 | } |
| 80 | check_platform() { |
| 81 | if is_supported_platform "$PLATFORM"; then |
| 82 | # optional logging goes here |
| 83 | true |
| 84 | else |
| 85 | log_crit "platform $PLATFORM is not supported. Make sure this script is up-to-date and file request at https://github.com/${PREFIX}/issues/new" |
| 86 | exit 1 |
| 87 | fi |
| 88 | } |
| 89 | tag_to_version() { |
| 90 | if [ -z "${TAG}" ]; then |
| 91 | log_info "checking GitHub for latest tag" |
| 92 | else |
| 93 | log_info "checking GitHub for tag '${TAG}'" |
| 94 | fi |
| 95 | REALTAG=$(github_release "$OWNER/$REPO" "${TAG}") && true |
| 96 | if test -z "$REALTAG"; then |
| 97 | log_crit "unable to find '${TAG}' - use 'latest' or see https://github.com/${PREFIX}/releases for details" |
| 98 | exit 1 |
| 99 | fi |
| 100 | # if version starts with 'v', remove it |
| 101 | TAG="$REALTAG" |
| 102 | VERSION=${TAG#v} |
| 103 | } |
| 104 | adjust_format() { |
| 105 | # change format (tar.gz or zip) based on OS |
| 106 | true |
| 107 | } |
| 108 | adjust_os() { |
| 109 | # adjust archive name based on OS |
| 110 | true |
| 111 | } |
| 112 | adjust_arch() { |
| 113 | # adjust archive name based on ARCH |
| 114 | true |
| 115 | } |
| 116 | |
| 117 | cat /dev/null <<EOF |
| 118 | ------------------------------------------------------------------------ |
| 119 | https://github.com/client9/shlib - portable posix shell functions |
| 120 | Public domain - http://unlicense.org |
| 121 | https://github.com/client9/shlib/blob/master/LICENSE.md |
| 122 | but credit (and pull requests) appreciated. |
| 123 | ------------------------------------------------------------------------ |
| 124 | EOF |
| 125 | is_command() { |
| 126 | command -v "$1" >/dev/null |
| 127 | } |
| 128 | echoerr() { |
| 129 | echo "$@" 1>&2 |
| 130 | } |
| 131 | log_prefix() { |
| 132 | echo "$0" |
| 133 | } |
| 134 | _logp=6 |
| 135 | log_set_priority() { |
| 136 | _logp="$1" |
| 137 | } |
| 138 | log_priority() { |
| 139 | if test -z "$1"; then |
| 140 | echo "$_logp" |
| 141 | return |
| 142 | fi |
| 143 | [ "$1" -le "$_logp" ] |
| 144 | } |
| 145 | log_tag() { |
| 146 | case $1 in |
| 147 | 0) echo "emerg" ;; |
| 148 | 1) echo "alert" ;; |
| 149 | 2) echo "crit" ;; |
| 150 | 3) echo "err" ;; |
| 151 | 4) echo "warning" ;; |
| 152 | 5) echo "notice" ;; |
| 153 | 6) echo "info" ;; |
| 154 | 7) echo "debug" ;; |
| 155 | *) echo "$1" ;; |
| 156 | esac |
| 157 | } |
| 158 | log_debug() { |
| 159 | log_priority 7 || return 0 |
| 160 | echoerr "$(log_prefix)" "$(log_tag 7)" "$@" |
| 161 | } |
| 162 | log_info() { |
| 163 | log_priority 6 || return 0 |
| 164 | echoerr "$(log_prefix)" "$(log_tag 6)" "$@" |
| 165 | } |
| 166 | log_err() { |
| 167 | log_priority 3 || return 0 |
| 168 | echoerr "$(log_prefix)" "$(log_tag 3)" "$@" |
| 169 | } |
| 170 | log_crit() { |
| 171 | log_priority 2 || return 0 |
| 172 | echoerr "$(log_prefix)" "$(log_tag 2)" "$@" |
| 173 | } |
| 174 | uname_os() { |
| 175 | os=$(uname -s | tr '[:upper:]' '[:lower:]') |
| 176 | case "$os" in |
| 177 | msys_nt) os="windows" ;; |
| 178 | esac |
| 179 | echo "$os" |
| 180 | } |
| 181 | uname_arch() { |
| 182 | arch=$(uname -m) |
| 183 | case $arch in |
| 184 | x86_64) arch="amd64" ;; |
| 185 | x86) arch="386" ;; |
| 186 | i686) arch="386" ;; |
| 187 | i386) arch="386" ;; |
| 188 | aarch64) arch="arm64" ;; |
| 189 | armv5*) arch="armv5" ;; |
| 190 | armv6*) arch="armv6" ;; |
| 191 | armv7*) arch="armv7" ;; |
| 192 | esac |
| 193 | echo ${arch} |
| 194 | } |
| 195 | uname_os_check() { |
| 196 | os=$(uname_os) |
| 197 | case "$os" in |
| 198 | darwin) return 0 ;; |
| 199 | dragonfly) return 0 ;; |
| 200 | freebsd) return 0 ;; |
| 201 | linux) return 0 ;; |
| 202 | android) return 0 ;; |
| 203 | nacl) return 0 ;; |
| 204 | netbsd) return 0 ;; |
| 205 | openbsd) return 0 ;; |
| 206 | plan9) return 0 ;; |
| 207 | solaris) return 0 ;; |
| 208 | windows) return 0 ;; |
| 209 | esac |
| 210 | log_crit "uname_os_check '$(uname -s)' got converted to '$os' which is not a GOOS value. Please file bug at https://github.com/client9/shlib" |
| 211 | return 1 |
| 212 | } |
| 213 | uname_arch_check() { |
| 214 | arch=$(uname_arch) |
| 215 | case "$arch" in |
| 216 | 386) return 0 ;; |
| 217 | amd64) return 0 ;; |
| 218 | arm64) return 0 ;; |
| 219 | armv5) return 0 ;; |
| 220 | armv6) return 0 ;; |
| 221 | armv7) return 0 ;; |
| 222 | ppc64) return 0 ;; |
| 223 | ppc64le) return 0 ;; |
| 224 | mips) return 0 ;; |
| 225 | mipsle) return 0 ;; |
| 226 | mips64) return 0 ;; |
| 227 | mips64le) return 0 ;; |
| 228 | s390x) return 0 ;; |
| 229 | amd64p32) return 0 ;; |
| 230 | esac |
| 231 | log_crit "uname_arch_check '$(uname -m)' got converted to '$arch' which is not a GOARCH value. Please file bug report at https://github.com/client9/shlib" |
| 232 | return 1 |
| 233 | } |
| 234 | untar() { |
| 235 | tarball=$1 |
| 236 | case "${tarball}" in |
| 237 | *.tar.gz | *.tgz) tar -xzf "${tarball}" ;; |
| 238 | *.tar) tar -xf "${tarball}" ;; |
| 239 | *.zip) unzip "${tarball}" ;; |
| 240 | *) |
| 241 | log_err "untar unknown archive format for ${tarball}" |
| 242 | return 1 |
| 243 | ;; |
| 244 | esac |
| 245 | } |
| 246 | http_download_curl() { |
| 247 | local_file=$1 |
| 248 | source_url=$2 |
| 249 | header=$3 |
| 250 | if [ -z "$header" ]; then |
| 251 | code=$(curl -w '%{http_code}' -sL -o "$local_file" "$source_url") |
| 252 | else |
| 253 | code=$(curl -w '%{http_code}' -sL -H "$header" -o "$local_file" "$source_url") |
| 254 | fi |
| 255 | if [ "$code" != "200" ]; then |
| 256 | log_debug "http_download_curl received HTTP status $code" |
| 257 | return 1 |
| 258 | fi |
| 259 | return 0 |
| 260 | } |
| 261 | http_download_wget() { |
| 262 | local_file=$1 |
| 263 | source_url=$2 |
| 264 | header=$3 |
| 265 | if [ -z "$header" ]; then |
| 266 | wget -q -O "$local_file" "$source_url" |
| 267 | else |
| 268 | wget -q --header "$header" -O "$local_file" "$source_url" |
| 269 | fi |
| 270 | } |
| 271 | http_download() { |
| 272 | log_debug "http_download $2" |
| 273 | if is_command curl; then |
| 274 | http_download_curl "$@" |
| 275 | return |
| 276 | elif is_command wget; then |
| 277 | http_download_wget "$@" |
| 278 | return |
| 279 | fi |
| 280 | log_crit "http_download unable to find wget or curl" |
| 281 | return 1 |
| 282 | } |
| 283 | http_copy() { |
| 284 | tmp=$(mktemp) |
| 285 | http_download "${tmp}" "$1" "$2" || return 1 |
| 286 | body=$(cat "$tmp") |
| 287 | rm -f "${tmp}" |
| 288 | echo "$body" |
| 289 | } |
| 290 | github_release() { |
| 291 | owner_repo=$1 |
| 292 | version=$2 |
| 293 | test -z "$version" && version="latest" |
| 294 | giturl="https://github.com/${owner_repo}/releases/${version}" |
| 295 | json=$(http_copy "$giturl" "Accept:application/json") |
| 296 | test -z "$json" && return 1 |
| 297 | version=$(echo "$json" | tr -s '\n' ' ' | sed 's/.*"tag_name":"//' | sed 's/".*//') |
| 298 | test -z "$version" && return 1 |
| 299 | echo "$version" |
| 300 | } |
| 301 | hash_sha256() { |
| 302 | TARGET=${1:-/dev/stdin} |
| 303 | if is_command gsha256sum; then |
| 304 | hash=$(gsha256sum "$TARGET") || return 1 |
| 305 | echo "$hash" | cut -d ' ' -f 1 |
| 306 | elif is_command sha256sum; then |
| 307 | hash=$(sha256sum "$TARGET") || return 1 |
| 308 | echo "$hash" | cut -d ' ' -f 1 |
| 309 | elif is_command shasum; then |
| 310 | hash=$(shasum -a 256 "$TARGET" 2>/dev/null) || return 1 |
| 311 | echo "$hash" | cut -d ' ' -f 1 |
| 312 | elif is_command openssl; then |
| 313 | hash=$(openssl -dst openssl dgst -sha256 "$TARGET") || return 1 |
| 314 | echo "$hash" | cut -d ' ' -f a |
| 315 | else |
| 316 | log_crit "hash_sha256 unable to find command to compute sha-256 hash" |
| 317 | return 1 |
| 318 | fi |
| 319 | } |
| 320 | hash_sha256_verify() { |
| 321 | TARGET=$1 |
| 322 | checksums=$2 |
| 323 | if [ -z "$checksums" ]; then |
| 324 | log_err "hash_sha256_verify checksum file not specified in arg2" |
| 325 | return 1 |
| 326 | fi |
| 327 | BASENAME=${TARGET##*/} |
| 328 | want=$(grep "${BASENAME}" "${checksums}" 2>/dev/null | tr '\t' ' ' | cut -d ' ' -f 1) |
| 329 | if [ -z "$want" ]; then |
| 330 | log_err "hash_sha256_verify unable to find checksum for '${TARGET}' in '${checksums}'" |
| 331 | return 1 |
| 332 | fi |
| 333 | got=$(hash_sha256 "$TARGET") |
| 334 | if [ "$want" != "$got" ]; then |
| 335 | log_err "hash_sha256_verify checksum for '$TARGET' did not verify ${want} vs $got" |
| 336 | return 1 |
| 337 | fi |
| 338 | } |
| 339 | cat /dev/null <<EOF |
| 340 | ------------------------------------------------------------------------ |
| 341 | End of functions from https://github.com/client9/shlib |
| 342 | ------------------------------------------------------------------------ |
| 343 | EOF |
| 344 | |
| 345 | PROJECT_NAME="kail" |
| 346 | OWNER=boz |
| 347 | REPO="kail" |
| 348 | # shellcheck disable=SC2034 |
| 349 | BINARY=kail |
| 350 | FORMAT=tar.gz |
| 351 | OS=$(uname_os) |
| 352 | ARCH=$(uname_arch) |
| 353 | PREFIX="$OWNER/$REPO" |
| 354 | |
| 355 | # use in logging routines |
| 356 | log_prefix() { |
| 357 | echo "$PREFIX" |
| 358 | } |
| 359 | PLATFORM="${OS}/${ARCH}" |
| 360 | GITHUB_DOWNLOAD=https://github.com/${OWNER}/${REPO}/releases/download |
| 361 | |
| 362 | uname_os_check "$OS" |
| 363 | uname_arch_check "$ARCH" |
| 364 | |
| 365 | parse_args "$@" |
| 366 | |
| 367 | check_platform |
| 368 | |
| 369 | tag_to_version |
| 370 | |
| 371 | adjust_format |
| 372 | |
| 373 | adjust_os |
| 374 | |
| 375 | adjust_arch |
| 376 | |
| 377 | log_info "found version: ${VERSION} for ${TAG}/${OS}/${ARCH}" |
| 378 | |
| 379 | # --------------------------------------------------------- |
| 380 | # [JOEY] - as of v0.16.1, download name contains an embedded 'v' |
| 381 | # --------------------------------------------------------- |
| 382 | # NAME=${PROJECT_NAME}_${VERSION}_${OS}_${ARCH} |
| 383 | NAME=${PROJECT_NAME}_v${VERSION}_${OS}_${ARCH} |
| 384 | |
| 385 | TARBALL=${NAME}.${FORMAT} |
| 386 | TARBALL_URL=${GITHUB_DOWNLOAD}/${TAG}/${TARBALL} |
| 387 | CHECKSUM=checksums.txt |
| 388 | CHECKSUM_URL=${GITHUB_DOWNLOAD}/${TAG}/${CHECKSUM} |
| 389 | |
| 390 | execute |