github.com/nozzle/golangci-lint@v1.49.0-nz3/install.sh (about)

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