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