github.com/joey-fossa/fossa-cli@v0.7.34-0.20190708193710-569f1e8679f0/install.sh (about)

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