github.com/segakazzz/buffalo@v0.16.22-0.20210119082501-1f52048d3feb/install.sh (about)

     1  #!/bin/sh
     2  set -e
     3  # Code generated by godownloader on 2019-02-07T19:17:58Z. DO NOT EDIT.
     4  #
     5  
     6  usage() {
     7    this=$1
     8    cat <<EOF
     9  $this: download go binaries for gobuffalo/buffalo
    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/gobuffalo/buffalo/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=$(mktmpdir)
    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    install -d "${BINDIR}"
    54    for binexe in "buffalo" ; 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  }
    62  is_supported_platform() {
    63    platform=$1
    64    found=1
    65    case "$platform" in
    66      darwin/amd64) found=0 ;;
    67      darwin/386) found=0 ;;
    68      linux/amd64) found=0 ;;
    69      linux/386) found=0 ;;
    70      windows/amd64) found=0 ;;
    71      windows/386) found=0 ;;
    72    esac
    73    return $found
    74  }
    75  check_platform() {
    76    if is_supported_platform "$PLATFORM"; then
    77      # optional logging goes here
    78      true
    79    else
    80      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"
    81      exit 1
    82    fi
    83  }
    84  tag_to_version() {
    85    if [ -z "${TAG}" ]; then
    86      log_info "checking GitHub for latest tag"
    87    else
    88      log_info "checking GitHub for tag '${TAG}'"
    89    fi
    90    REALTAG=$(github_release "$OWNER/$REPO" "${TAG}") && true
    91    if test -z "$REALTAG"; then
    92      log_crit "unable to find '${TAG}' - use 'latest' or see https://github.com/${PREFIX}/releases for details"
    93      exit 1
    94    fi
    95    # if version starts with 'v', remove it
    96    TAG="$REALTAG"
    97    VERSION=${TAG#v}
    98  }
    99  adjust_format() {
   100    # change format (tar.gz or zip) based on ARCH
   101    true
   102  }
   103  adjust_os() {
   104    # adjust archive name based on OS
   105    true
   106  }
   107  adjust_arch() {
   108    # adjust archive name based on ARCH
   109    true
   110  }
   111  
   112  cat /dev/null <<EOF
   113  ------------------------------------------------------------------------
   114  https://github.com/client9/shlib - portable posix shell functions
   115  Public domain - http://unlicense.org
   116  https://github.com/client9/shlib/blob/master/LICENSE.md
   117  but credit (and pull requests) appreciated.
   118  ------------------------------------------------------------------------
   119  EOF
   120  is_command() {
   121    command -v "$1" >/dev/null
   122  }
   123  echoerr() {
   124    echo "$@" 1>&2
   125  }
   126  log_prefix() {
   127    echo "$0"
   128  }
   129  _logp=6
   130  log_set_priority() {
   131    _logp="$1"
   132  }
   133  log_priority() {
   134    if test -z "$1"; then
   135      echo "$_logp"
   136      return
   137    fi
   138    [ "$1" -le "$_logp" ]
   139  }
   140  log_tag() {
   141    case $1 in
   142      0) echo "emerg" ;;
   143      1) echo "alert" ;;
   144      2) echo "crit" ;;
   145      3) echo "err" ;;
   146      4) echo "warning" ;;
   147      5) echo "notice" ;;
   148      6) echo "info" ;;
   149      7) echo "debug" ;;
   150      *) echo "$1" ;;
   151    esac
   152  }
   153  log_debug() {
   154    log_priority 7 || return 0
   155    echoerr "$(log_prefix)" "$(log_tag 7)" "$@"
   156  }
   157  log_info() {
   158    log_priority 6 || return 0
   159    echoerr "$(log_prefix)" "$(log_tag 6)" "$@"
   160  }
   161  log_err() {
   162    log_priority 3 || return 0
   163    echoerr "$(log_prefix)" "$(log_tag 3)" "$@"
   164  }
   165  log_crit() {
   166    log_priority 2 || return 0
   167    echoerr "$(log_prefix)" "$(log_tag 2)" "$@"
   168  }
   169  uname_os() {
   170    os=$(uname -s | tr '[:upper:]' '[:lower:]')
   171    case "$os" in
   172      msys_nt) os="windows" ;;
   173    esac
   174    echo "$os"
   175  }
   176  uname_arch() {
   177    arch=$(uname -m)
   178    case $arch in
   179      x86_64) arch="amd64" ;;
   180      x86) arch="386" ;;
   181      i686) arch="386" ;;
   182      i386) arch="386" ;;
   183      aarch64) arch="arm64" ;;
   184      armv5*) arch="armv5" ;;
   185      armv6*) arch="armv6" ;;
   186      armv7*) arch="armv7" ;;
   187    esac
   188    echo ${arch}
   189  }
   190  uname_os_check() {
   191    os=$(uname_os)
   192    case "$os" in
   193      darwin) return 0 ;;
   194      dragonfly) return 0 ;;
   195      freebsd) return 0 ;;
   196      linux) return 0 ;;
   197      android) return 0 ;;
   198      nacl) return 0 ;;
   199      netbsd) return 0 ;;
   200      openbsd) return 0 ;;
   201      plan9) return 0 ;;
   202      solaris) return 0 ;;
   203      windows) return 0 ;;
   204    esac
   205    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"
   206    return 1
   207  }
   208  uname_arch_check() {
   209    arch=$(uname_arch)
   210    case "$arch" in
   211      386) return 0 ;;
   212      amd64) return 0 ;;
   213      arm64) return 0 ;;
   214      armv5) return 0 ;;
   215      armv6) return 0 ;;
   216      armv7) return 0 ;;
   217      ppc64) return 0 ;;
   218      ppc64le) return 0 ;;
   219      mips) return 0 ;;
   220      mipsle) return 0 ;;
   221      mips64) return 0 ;;
   222      mips64le) return 0 ;;
   223      s390x) return 0 ;;
   224      amd64p32) return 0 ;;
   225    esac
   226    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"
   227    return 1
   228  }
   229  untar() {
   230    tarball=$1
   231    case "${tarball}" in
   232      *.tar.gz | *.tgz) tar -xzf "${tarball}" ;;
   233      *.tar) tar -xf "${tarball}" ;;
   234      *.zip) unzip "${tarball}" ;;
   235      *)
   236        log_err "untar unknown archive format for ${tarball}"
   237        return 1
   238        ;;
   239    esac
   240  }
   241  mktmpdir() {
   242    test -z "$TMPDIR" && TMPDIR="$(mktemp -d)"
   243    mkdir -p "${TMPDIR}"
   244    echo "${TMPDIR}"
   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="buffalo"
   346  OWNER=gobuffalo
   347  REPO="buffalo"
   348  BINARY=buffalo
   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  check_platform
   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