github.com/study-group-99/pilates@v0.2.2/install.sh (about)

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