github.com/bananabytelabs/wazero@v0.0.0-20240105073314-54b22a776da8/site/static/install.sh (about)

     1  #!/bin/sh
     2  
     3  # Copyright 2023 Tetrate
     4  #
     5  # Licensed under the Apache License, Version 2.0 (the "License");
     6  # you may not use this file except in compliance with the License.
     7  # You may obtain a copy of the License at
     8  #
     9  #     http://www.apache.org/licenses/LICENSE-2.0
    10  #
    11  # Unless required by applicable law or agreed to in writing, software
    12  # distributed under the License is distributed on an "AS IS" BASIS,
    13  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  # See the License for the specific language governing permissions and
    15  # limitations under the License.
    16  set -e
    17  # Code generated by godownloader on 2021-05-25T10:02:41Z. DO NOT EDIT.
    18  #
    19  
    20  usage() {
    21    this=$1
    22    cat <<EOF
    23  $this: download go binaries for tetratelabs/wazero
    24  
    25  Usage: $this [-b] bindir [-d] [tag]
    26    -b sets bindir or installation directory, Defaults to ./bin
    27    -d turns on debug logging
    28     [tag] is a tag from
    29     https://github.com/bananabytelabs/wazero/releases
    30     If tag is missing, then the latest will be used.
    31  
    32   Generated by godownloader
    33    https://github.com/goreleaser/godownloader
    34  
    35  EOF
    36    exit 2
    37  }
    38  
    39  parse_args() {
    40    #BINDIR is ./bin unless set be ENV
    41    # over-ridden by flag below
    42  
    43    BINDIR=${BINDIR:-./bin}
    44    while getopts "b:dh?x" arg; do
    45      case "$arg" in
    46        b) BINDIR="$OPTARG" ;;
    47        d) log_set_priority 10 ;;
    48        h | \?) usage "$0" ;;
    49        x) set -x ;;
    50      esac
    51    done
    52    shift $((OPTIND - 1))
    53    TAG=$1
    54  }
    55  # this function wraps all the destructive operations
    56  # if a curl|bash cuts off the end of the script due to
    57  # network, either nothing will happen or will syntax error
    58  # out preventing half-done work
    59  execute() {
    60    tmpdir=$(mktemp -d)
    61    log_debug "downloading files into ${tmpdir}"
    62    http_download "${tmpdir}/${TARBALL}" "${TARBALL_URL}"
    63    http_download "${tmpdir}/${CHECKSUM}" "${CHECKSUM_URL}"
    64    hash_sha256_verify "${tmpdir}/${TARBALL}" "${tmpdir}/${CHECKSUM}"
    65    srcdir="${tmpdir}"
    66    (cd "${tmpdir}" && untar "${TARBALL}")
    67    test ! -d "${BINDIR}" && install -d "${BINDIR}"
    68    for binexe in $BINARIES; do
    69      if [ "$OS" = "windows" ]; then
    70        binexe="${binexe}.exe"
    71      fi
    72      install "${srcdir}/${binexe}" "${BINDIR}/"
    73      log_info "installed ${BINDIR}/${binexe}"
    74    done
    75    rm -rf "${tmpdir}"
    76  }
    77  get_binaries() {
    78    case "$PLATFORM" in
    79      darwin/amd64) BINARIES="wazero" ;;
    80      darwin/arm64) BINARIES="wazero" ;;
    81      linux/amd64) BINARIES="wazero" ;;
    82      linux/arm64) BINARIES="wazero" ;;
    83      *)
    84        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"
    85        exit 1
    86        ;;
    87    esac
    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      cygwin_nt*) os="windows" ;;
   178      mingw*) os="windows" ;;
   179      msys_nt*) os="windows" ;;
   180    esac
   181    echo "$os"
   182  }
   183  uname_arch() {
   184    arch=$(uname -m)
   185    case $arch in
   186      x86_64) arch="amd64" ;;
   187      x86) arch="386" ;;
   188      i686) arch="386" ;;
   189      i386) arch="386" ;;
   190      aarch64) arch="arm64" ;;
   191      armv5*) arch="armv5" ;;
   192      armv6*) arch="armv6" ;;
   193      armv7*) arch="armv7" ;;
   194    esac
   195    echo ${arch}
   196  }
   197  uname_os_check() {
   198    os=$(uname_os)
   199    case "$os" in
   200      darwin) return 0 ;;
   201      dragonfly) return 0 ;;
   202      freebsd) return 0 ;;
   203      linux) return 0 ;;
   204      android) return 0 ;;
   205      nacl) return 0 ;;
   206      netbsd) return 0 ;;
   207      openbsd) return 0 ;;
   208      plan9) return 0 ;;
   209      solaris) return 0 ;;
   210      windows) return 0 ;;
   211    esac
   212    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"
   213    return 1
   214  }
   215  uname_arch_check() {
   216    arch=$(uname_arch)
   217    case "$arch" in
   218      386) return 0 ;;
   219      amd64) return 0 ;;
   220      arm64) return 0 ;;
   221      armv5) return 0 ;;
   222      armv6) return 0 ;;
   223      armv7) return 0 ;;
   224      ppc64) return 0 ;;
   225      ppc64le) return 0 ;;
   226      mips) return 0 ;;
   227      mipsle) return 0 ;;
   228      mips64) return 0 ;;
   229      mips64le) return 0 ;;
   230      s390x) return 0 ;;
   231      amd64p32) return 0 ;;
   232    esac
   233    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"
   234    return 1
   235  }
   236  untar() {
   237    tarball=$1
   238    case "${tarball}" in
   239      *.tar.gz | *.tgz) tar --no-same-owner -xzf "${tarball}" ;;
   240      *.tar) tar --no-same-owner -xf "${tarball}" ;;
   241      *.zip) unzip "${tarball}" ;;
   242      *)
   243        log_err "untar unknown archive format for ${tarball}"
   244        return 1
   245        ;;
   246    esac
   247  }
   248  http_download_curl() {
   249    local_file=$1
   250    source_url=$2
   251    header=$3
   252    if [ -z "$header" ]; then
   253      code=$(curl -w '%{http_code}' -sL -o "$local_file" "$source_url")
   254    else
   255      code=$(curl -w '%{http_code}' -sL -H "$header" -o "$local_file" "$source_url")
   256    fi
   257    if [ "$code" != "200" ]; then
   258      log_debug "http_download_curl received HTTP status $code"
   259      return 1
   260    fi
   261    return 0
   262  }
   263  http_download_wget() {
   264    local_file=$1
   265    source_url=$2
   266    header=$3
   267    if [ -z "$header" ]; then
   268      wget -q -O "$local_file" "$source_url"
   269    else
   270      wget -q --header "$header" -O "$local_file" "$source_url"
   271    fi
   272  }
   273  http_download() {
   274    log_debug "http_download $2"
   275    if is_command curl; then
   276      http_download_curl "$@"
   277      return
   278    elif is_command wget; then
   279      http_download_wget "$@"
   280      return
   281    fi
   282    log_crit "http_download unable to find wget or curl"
   283    return 1
   284  }
   285  http_copy() {
   286    tmp=$(mktemp)
   287    http_download "${tmp}" "$1" "$2" || return 1
   288    body=$(cat "$tmp")
   289    rm -f "${tmp}"
   290    echo "$body"
   291  }
   292  github_release() {
   293    owner_repo=$1
   294    version=$2
   295    test -z "$version" && version="latest"
   296    giturl="https://github.com/${owner_repo}/releases/${version}"
   297    json=$(http_copy "$giturl" "Accept:application/json")
   298    test -z "$json" && return 1
   299    version=$(echo "$json" | tr -s '\n' ' ' | sed 's/.*"tag_name":"//' | sed 's/".*//')
   300    test -z "$version" && return 1
   301    echo "$version"
   302  }
   303  hash_sha256() {
   304    TARGET=${1:-/dev/stdin}
   305    if is_command gsha256sum; then
   306      hash=$(gsha256sum "$TARGET") || return 1
   307      echo "$hash" | cut -d ' ' -f 1
   308    elif is_command sha256sum; then
   309      hash=$(sha256sum "$TARGET") || return 1
   310      echo "$hash" | cut -d ' ' -f 1
   311    elif is_command shasum; then
   312      hash=$(shasum -a 256 "$TARGET" 2>/dev/null) || return 1
   313      echo "$hash" | cut -d ' ' -f 1
   314    elif is_command openssl; then
   315      hash=$(openssl -dst openssl dgst -sha256 "$TARGET") || return 1
   316      echo "$hash" | cut -d ' ' -f a
   317    else
   318      log_crit "hash_sha256 unable to find command to compute sha-256 hash"
   319      return 1
   320    fi
   321  }
   322  hash_sha256_verify() {
   323    TARGET=$1
   324    checksums=$2
   325    if [ -z "$checksums" ]; then
   326      log_err "hash_sha256_verify checksum file not specified in arg2"
   327      return 1
   328    fi
   329    BASENAME=${TARGET##*/}
   330    want=$(grep "${BASENAME}" "${checksums}" 2>/dev/null | tr '\t' ' ' | cut -d ' ' -f 1)
   331    if [ -z "$want" ]; then
   332      log_err "hash_sha256_verify unable to find checksum for '${TARGET}' in '${checksums}'"
   333      return 1
   334    fi
   335    got=$(hash_sha256 "$TARGET")
   336    if [ "$want" != "$got" ]; then
   337      log_err "hash_sha256_verify checksum for '$TARGET' did not verify ${want} vs $got"
   338      return 1
   339    fi
   340  }
   341  cat /dev/null <<EOF
   342  ------------------------------------------------------------------------
   343  End of functions from https://github.com/client9/shlib
   344  ------------------------------------------------------------------------
   345  EOF
   346  
   347  PROJECT_NAME="wazero"
   348  OWNER=tetratelabs
   349  REPO="wazero"
   350  BINARY=wazero
   351  FORMAT=tar.gz
   352  OS=$(uname_os)
   353  ARCH=$(uname_arch)
   354  PREFIX="$OWNER/$REPO"
   355  
   356  # use in logging routines
   357  log_prefix() {
   358  	echo "$PREFIX"
   359  }
   360  PLATFORM="${OS}/${ARCH}"
   361  GITHUB_DOWNLOAD=https://github.com/${OWNER}/${REPO}/releases/download
   362  
   363  uname_os_check "$OS"
   364  uname_arch_check "$ARCH"
   365  
   366  parse_args "$@"
   367  
   368  get_binaries
   369  
   370  tag_to_version
   371  
   372  adjust_format
   373  
   374  adjust_os
   375  
   376  adjust_arch
   377  
   378  log_info "found version: ${VERSION} for ${TAG}/${OS}/${ARCH}"
   379  
   380  NAME=${PROJECT_NAME}_${VERSION}_${OS}_${ARCH}
   381  TARBALL=${NAME}.${FORMAT}
   382  TARBALL_URL=${GITHUB_DOWNLOAD}/${TAG}/${TARBALL}
   383  CHECKSUM=wazero_${VERSION}_checksums.txt
   384  CHECKSUM_URL=${GITHUB_DOWNLOAD}/${TAG}/${CHECKSUM}
   385  
   386  
   387  execute