github.com/saucelabs/saucectl@v0.175.1/install (about)

     1  #!/bin/sh
     2  set -e
     3  # Original code generated by godownloader on 2021-08-16T21:37:49Z.
     4  # Due to deprecation of godownloader, this file has been modified by hand thereafter.
     5  #
     6  
     7  usage() {
     8    this=$1
     9    cat <<EOF
    10  $this: download go binaries for saucelabs/saucectl
    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/saucelabs/saucectl/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    http_download "${tmpdir}/${TARBALL}" "${TARBALL_URL}"
    50    http_download "${tmpdir}/${CHECKSUM}" "${CHECKSUM_URL}"
    51    hash_sha256_verify "${tmpdir}/${TARBALL}" "${tmpdir}/${CHECKSUM}"
    52    srcdir="${tmpdir}"
    53    (cd "${tmpdir}" && untar "${TARBALL}")
    54    test ! -d "${BINDIR}" && install -d "${BINDIR}"
    55    for binexe in $BINARIES; do
    56      if [ "$OS" = "windows" ]; then
    57        binexe="${binexe}.exe"
    58      fi
    59      install "${srcdir}/${binexe}" "${BINDIR}/"
    60      log_info "installed ${BINDIR}/${binexe}"
    61    done
    62    rm -rf "${tmpdir}"
    63  }
    64  get_binaries() {
    65    case "$PLATFORM" in
    66      darwin/386) BINARIES="saucectl" ;;
    67      darwin/amd64) BINARIES="saucectl" ;;
    68      darwin/arm64) BINARIES="saucectl" ;;
    69      linux/386) BINARIES="saucectl" ;;
    70      linux/arm64) BINARIES="saucectl" ;;
    71      linux/amd64) BINARIES="saucectl" ;;
    72      windows/386) BINARIES="saucectl" ;;
    73      windows/amd64) BINARIES="saucectl" ;;
    74      *)
    75        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"
    76        exit 1
    77        ;;
    78    esac
    79  }
    80  tag_to_version() {
    81    if [ -z "${TAG}" ]; then
    82      log_info "checking GitHub for latest tag"
    83    else
    84      log_info "checking GitHub for tag '${TAG}'"
    85    fi
    86    REALTAG=$(github_release "$OWNER/$REPO" "${TAG}") && true
    87    if test -z "$REALTAG"; then
    88      log_crit "unable to find '${TAG}' - use 'latest' or see https://github.com/${PREFIX}/releases for details"
    89      exit 1
    90    fi
    91    # if version starts with 'v', remove it
    92    TAG="$REALTAG"
    93    VERSION=${TAG#v}
    94  }
    95  adjust_format() {
    96    # change format (tar.gz or zip) based on OS
    97    case ${OS} in
    98      windows) FORMAT=zip ;;
    99    esac
   100    true
   101  }
   102  adjust_os() {
   103    # adjust archive name based on OS
   104    case ${OS} in
   105      386) OS=32-bit ;;
   106      amd64) OS=64-bit ;;
   107      darwin) OS=mac ;;
   108      linux) OS=linux ;;
   109      windows) OS=win ;;
   110    esac
   111    true
   112  }
   113  adjust_arch() {
   114    # adjust archive name based on ARCH
   115    case ${ARCH} in
   116      386) ARCH=32-bit ;;
   117      amd64) ARCH=64-bit ;;
   118      darwin) ARCH=mac ;;
   119      linux) ARCH=linux ;;
   120      windows) ARCH=win ;;
   121    esac
   122    true
   123  }
   124  
   125  cat /dev/null <<EOF
   126  ------------------------------------------------------------------------
   127  https://github.com/saucelabs/saucectl - portable posix shell functions
   128  Public domain - http://unlicense.org
   129  https://github.com/saucelabs/saucectl/blob/master/LICENSE.md
   130  but credit (and pull requests) appreciated.
   131  ------------------------------------------------------------------------
   132  EOF
   133  is_command() {
   134    command -v "$1" >/dev/null
   135  }
   136  echoerr() {
   137    echo "$@" 1>&2
   138  }
   139  log_prefix() {
   140    echo "$0"
   141  }
   142  _logp=6
   143  log_set_priority() {
   144    _logp="$1"
   145  }
   146  log_priority() {
   147    if test -z "$1"; then
   148      echo "$_logp"
   149      return
   150    fi
   151    [ "$1" -le "$_logp" ]
   152  }
   153  log_tag() {
   154    case $1 in
   155      0) echo "emerg" ;;
   156      1) echo "alert" ;;
   157      2) echo "crit" ;;
   158      3) echo "err" ;;
   159      4) echo "warning" ;;
   160      5) echo "notice" ;;
   161      6) echo "info" ;;
   162      7) echo "debug" ;;
   163      *) echo "$1" ;;
   164    esac
   165  }
   166  log_debug() {
   167    log_priority 7 || return 0
   168    echoerr "$(log_prefix)" "$(log_tag 7)" "$@"
   169  }
   170  log_info() {
   171    log_priority 6 || return 0
   172    echoerr "$(log_prefix)" "$(log_tag 6)" "$@"
   173  }
   174  log_err() {
   175    log_priority 3 || return 0
   176    echoerr "$(log_prefix)" "$(log_tag 3)" "$@"
   177  }
   178  log_crit() {
   179    log_priority 2 || return 0
   180    echoerr "$(log_prefix)" "$(log_tag 2)" "$@"
   181  }
   182  uname_os() {
   183    os=$(uname -s | tr '[:upper:]' '[:lower:]')
   184    case "$os" in
   185      cygwin_nt*) os="windows" ;;
   186      mingw*) os="windows" ;;
   187      msys_nt*) os="windows" ;;
   188    esac
   189    echo "$os"
   190  }
   191  uname_arch() {
   192    arch=$(uname -m)
   193    case $arch in
   194      x86_64) arch="amd64" ;;
   195      x86) arch="386" ;;
   196      i686) arch="386" ;;
   197      i386) arch="386" ;;
   198      aarch64) arch="arm64" ;;
   199      armv5*) arch="armv5" ;;
   200      armv6*) arch="armv6" ;;
   201      armv7*) arch="armv7" ;;
   202    esac
   203    echo ${arch}
   204  }
   205  uname_os_check() {
   206    os=$(uname_os)
   207    case "$os" in
   208      darwin) return 0 ;;
   209      dragonfly) return 0 ;;
   210      freebsd) return 0 ;;
   211      linux) return 0 ;;
   212      android) return 0 ;;
   213      nacl) return 0 ;;
   214      netbsd) return 0 ;;
   215      openbsd) return 0 ;;
   216      plan9) return 0 ;;
   217      solaris) return 0 ;;
   218      windows) return 0 ;;
   219    esac
   220    log_crit "uname_os_check '$(uname -s)' got converted to '$os' which is not a GOOS value. Please file bug at https://github.com/saucelabs/saucectl"
   221    return 1
   222  }
   223  uname_arch_check() {
   224    arch=$(uname_arch)
   225    case "$arch" in
   226      386) return 0 ;;
   227      amd64) return 0 ;;
   228      arm64) return 0 ;;
   229      armv5) return 0 ;;
   230      armv6) return 0 ;;
   231      armv7) return 0 ;;
   232      ppc64) return 0 ;;
   233      ppc64le) return 0 ;;
   234      mips) return 0 ;;
   235      mipsle) return 0 ;;
   236      mips64) return 0 ;;
   237      mips64le) return 0 ;;
   238      s390x) return 0 ;;
   239      amd64p32) return 0 ;;
   240    esac
   241    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/saucelabs/saucectl"
   242    return 1
   243  }
   244  untar() {
   245    tarball=$1
   246    case "${tarball}" in
   247      *.tar.gz | *.tgz) tar --no-same-owner -xzf "${tarball}" ;;
   248      *.tar) tar --no-same-owner -xf "${tarball}" ;;
   249      *.zip) unzip "${tarball}" ;;
   250      *)
   251        log_err "untar unknown archive format for ${tarball}"
   252        return 1
   253        ;;
   254    esac
   255  }
   256  http_download_curl() {
   257    local_file=$1
   258    source_url=$2
   259    header=$3
   260    if [ -z "$header" ]; then
   261      code=$(curl -w '%{http_code}' -sL -o "$local_file" "$source_url")
   262    else
   263      code=$(curl -w '%{http_code}' -sL -H "$header" -o "$local_file" "$source_url")
   264    fi
   265    if [ "$code" != "200" ]; then
   266      log_debug "http_download_curl received HTTP status $code"
   267      return 1
   268    fi
   269    return 0
   270  }
   271  http_download_wget() {
   272    local_file=$1
   273    source_url=$2
   274    header=$3
   275    if [ -z "$header" ]; then
   276      wget -q -O "$local_file" "$source_url"
   277    else
   278      wget -q --header "$header" -O "$local_file" "$source_url"
   279    fi
   280  }
   281  http_download() {
   282    log_debug "http_download $2"
   283    if is_command curl; then
   284      http_download_curl "$@"
   285      return
   286    elif is_command wget; then
   287      http_download_wget "$@"
   288      return
   289    fi
   290    log_crit "http_download unable to find wget or curl"
   291    return 1
   292  }
   293  http_copy() {
   294    tmp=$(mktemp)
   295    http_download "${tmp}" "$1" "$2" || return 1
   296    body=$(cat "$tmp")
   297    rm -f "${tmp}"
   298    echo "$body"
   299  }
   300  github_release() {
   301    owner_repo=$1
   302    version=$2
   303    test -z "$version" && version="latest"
   304    giturl="https://github.com/${owner_repo}/releases/${version}"
   305    json=$(http_copy "$giturl" "Accept:application/json")
   306    test -z "$json" && return 1
   307    version=$(echo "$json" | tr -s '\n' ' ' | sed 's/.*"tag_name":"//' | sed 's/".*//')
   308    test -z "$version" && return 1
   309    echo "$version"
   310  }
   311  hash_sha256() {
   312    TARGET=${1:-/dev/stdin}
   313    if is_command gsha256sum; then
   314      hash=$(gsha256sum "$TARGET") || return 1
   315      echo "$hash" | cut -d ' ' -f 1
   316    elif is_command sha256sum; then
   317      hash=$(sha256sum "$TARGET") || return 1
   318      echo "$hash" | cut -d ' ' -f 1
   319    elif is_command shasum; then
   320      hash=$(shasum -a 256 "$TARGET" 2>/dev/null) || return 1
   321      echo "$hash" | cut -d ' ' -f 1
   322    elif is_command openssl; then
   323      hash=$(openssl -dst openssl dgst -sha256 "$TARGET") || return 1
   324      echo "$hash" | cut -d ' ' -f a
   325    else
   326      log_crit "hash_sha256 unable to find command to compute sha-256 hash"
   327      return 1
   328    fi
   329  }
   330  hash_sha256_verify() {
   331    TARGET=$1
   332    checksums=$2
   333    if [ -z "$checksums" ]; then
   334      log_err "hash_sha256_verify checksum file not specified in arg2"
   335      return 1
   336    fi
   337    BASENAME=${TARGET##*/}
   338    want=$(grep "${BASENAME}" "${checksums}" 2>/dev/null | tr '\t' ' ' | cut -d ' ' -f 1)
   339    if [ -z "$want" ]; then
   340      log_err "hash_sha256_verify unable to find checksum for '${TARGET}' in '${checksums}'"
   341      return 1
   342    fi
   343    got=$(hash_sha256 "$TARGET")
   344    if [ "$want" != "$got" ]; then
   345      log_err "hash_sha256_verify checksum for '$TARGET' did not verify ${want} vs $got"
   346      return 1
   347    fi
   348  }
   349  cat /dev/null <<EOF
   350  ------------------------------------------------------------------------
   351  End of functions from https://github.com/saucelabs/saucectl
   352  ------------------------------------------------------------------------
   353  EOF
   354  
   355  PROJECT_NAME="saucectl"
   356  OWNER=saucelabs
   357  REPO="saucectl"
   358  BINARY=saucectl
   359  FORMAT=tar.gz
   360  OS=$(uname_os)
   361  ARCH=$(uname_arch)
   362  PREFIX="$OWNER/$REPO"
   363  USE_SUDO="true"
   364  
   365  # use in logging routines
   366  log_prefix() {
   367  	echo "$PREFIX"
   368  }
   369  PLATFORM="${OS}/${ARCH}"
   370  GITHUB_DOWNLOAD=https://github.com/${OWNER}/${REPO}/releases/download
   371  
   372  uname_os_check "$OS"
   373  uname_arch_check "$ARCH"
   374  
   375  parse_args "$@"
   376  
   377  get_binaries
   378  
   379  tag_to_version
   380  
   381  adjust_format
   382  
   383  adjust_os
   384  
   385  adjust_arch
   386  
   387  log_info "found version: ${VERSION} for ${TAG}/${OS}/${ARCH}"
   388  
   389  NAME=${PROJECT_NAME}_${VERSION}_${OS}_${ARCH}
   390  TARBALL=${NAME}.${FORMAT}
   391  TARBALL_URL=${GITHUB_DOWNLOAD}/${TAG}/${TARBALL}
   392  CHECKSUM=checksums.txt
   393  CHECKSUM_URL=${GITHUB_DOWNLOAD}/${TAG}/${CHECKSUM}
   394  
   395  
   396  execute