github.com/buildtool/scaffold@v0.0.2/install.sh (about)

     1  #!/bin/sh
     2  set -e
     3  # Code generated by godownloader on 2019-11-12T08:46:59Z. DO NOT EDIT.
     4  #
     5  
     6  usage() {
     7    this=$1
     8    cat <<EOF
     9  $this: download go binaries for buildtool/scaffold
    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/buildtool/scaffold/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:-./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/amd64) BINARIES="scaffold" ;;
    66      linux/amd64) BINARIES="scaffold" ;;
    67      *)
    68        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"
    69        exit 1
    70        ;;
    71    esac
    72  }
    73  tag_to_version() {
    74    if [ -z "${TAG}" ]; then
    75      log_info "checking GitHub for latest tag"
    76    else
    77      log_info "checking GitHub for tag '${TAG}'"
    78    fi
    79    REALTAG=$(github_release "$OWNER/$REPO" "${TAG}") && true
    80    if test -z "$REALTAG"; then
    81      log_crit "unable to find '${TAG}' - use 'latest' or see https://github.com/${PREFIX}/releases for details"
    82      exit 1
    83    fi
    84    # if version starts with 'v', remove it
    85    TAG="$REALTAG"
    86    VERSION=${TAG#v}
    87  }
    88  adjust_format() {
    89    # change format (tar.gz or zip) based on OS
    90    true
    91  }
    92  adjust_os() {
    93    # adjust archive name based on OS
    94    case ${OS} in
    95      386) OS=i386 ;;
    96      amd64) OS=x86_64 ;;
    97      darwin) OS=Darwin ;;
    98      linux) OS=Linux ;;
    99      windows) OS=Windows ;;
   100    esac
   101    true
   102  }
   103  adjust_arch() {
   104    # adjust archive name based on ARCH
   105    case ${ARCH} in
   106      386) ARCH=i386 ;;
   107      amd64) ARCH=x86_64 ;;
   108      darwin) ARCH=Darwin ;;
   109      linux) ARCH=Linux ;;
   110      windows) ARCH=Windows ;;
   111    esac
   112    true
   113  }
   114  
   115  cat /dev/null <<EOF
   116  ------------------------------------------------------------------------
   117  https://github.com/client9/shlib - portable posix shell functions
   118  Public domain - http://unlicense.org
   119  https://github.com/client9/shlib/blob/master/LICENSE.md
   120  but credit (and pull requests) appreciated.
   121  ------------------------------------------------------------------------
   122  EOF
   123  is_command() {
   124    command -v "$1" >/dev/null
   125  }
   126  echoerr() {
   127    echo "$@" 1>&2
   128  }
   129  log_prefix() {
   130    echo "$0"
   131  }
   132  _logp=6
   133  log_set_priority() {
   134    _logp="$1"
   135  }
   136  log_priority() {
   137    if test -z "$1"; then
   138      echo "$_logp"
   139      return
   140    fi
   141    [ "$1" -le "$_logp" ]
   142  }
   143  log_tag() {
   144    case $1 in
   145      0) echo "emerg" ;;
   146      1) echo "alert" ;;
   147      2) echo "crit" ;;
   148      3) echo "err" ;;
   149      4) echo "warning" ;;
   150      5) echo "notice" ;;
   151      6) echo "info" ;;
   152      7) echo "debug" ;;
   153      *) echo "$1" ;;
   154    esac
   155  }
   156  log_debug() {
   157    log_priority 7 || return 0
   158    echoerr "$(log_prefix)" "$(log_tag 7)" "$@"
   159  }
   160  log_info() {
   161    log_priority 6 || return 0
   162    echoerr "$(log_prefix)" "$(log_tag 6)" "$@"
   163  }
   164  log_err() {
   165    log_priority 3 || return 0
   166    echoerr "$(log_prefix)" "$(log_tag 3)" "$@"
   167  }
   168  log_crit() {
   169    log_priority 2 || return 0
   170    echoerr "$(log_prefix)" "$(log_tag 2)" "$@"
   171  }
   172  uname_os() {
   173    os=$(uname -s | tr '[:upper:]' '[:lower:]')
   174    case "$os" in
   175      cygwin_nt*) os="windows" ;;
   176      mingw*) os="windows" ;;
   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 --no-same-owner -xzf "${tarball}" ;;
   238      *.tar) tar --no-same-owner -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="scaffold"
   346  OWNER=buildtool
   347  REPO="scaffold"
   348  BINARY=scaffold
   349  FORMAT=tar.gz
   350  OS=$(uname_os)
   351  ARCH=$(uname_arch)
   352  PREFIX="$OWNER/$REPO"
   353  
   354  # use in logging routines
   355  log_prefix() {
   356  	echo "$PREFIX"
   357  }
   358  PLATFORM="${OS}/${ARCH}"
   359  GITHUB_DOWNLOAD=https://github.com/${OWNER}/${REPO}/releases/download
   360  
   361  uname_os_check "$OS"
   362  uname_arch_check "$ARCH"
   363  
   364  parse_args "$@"
   365  
   366  get_binaries
   367  
   368  tag_to_version
   369  
   370  adjust_format
   371  
   372  adjust_os
   373  
   374  adjust_arch
   375  
   376  log_info "found version: ${VERSION} for ${TAG}/${OS}/${ARCH}"
   377  
   378  NAME=${PROJECT_NAME}_${VERSION}_${OS}_${ARCH}
   379  TARBALL=${NAME}.${FORMAT}
   380  TARBALL_URL=${GITHUB_DOWNLOAD}/${TAG}/${TARBALL}
   381  CHECKSUM=checksums.txt
   382  CHECKSUM_URL=${GITHUB_DOWNLOAD}/${TAG}/${CHECKSUM}
   383  
   384  
   385  execute