github.com/vanstinator/golangci-lint@v0.0.0-20240223191551-cc572f00d9d1/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/vanstinator/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      illumos/amd64) BINARIES="golangci-lint" ;;
    81      linux/386) BINARIES="golangci-lint" ;;
    82      linux/amd64) BINARIES="golangci-lint" ;;
    83      linux/arm64) BINARIES="golangci-lint" ;;
    84      linux/armv6) BINARIES="golangci-lint" ;;
    85      linux/armv7) BINARIES="golangci-lint" ;;
    86      linux/mips64) BINARIES="golangci-lint" ;;
    87      linux/mips64le) BINARIES="golangci-lint" ;;
    88      linux/ppc64le) BINARIES="golangci-lint" ;;
    89      linux/s390x) BINARIES="golangci-lint" ;;
    90      linux/riscv64) BINARIES="golangci-lint" ;;
    91      linux/loong64) BINARIES="golangci-lint" ;;
    92      netbsd/386) BINARIES="golangci-lint" ;;
    93      netbsd/amd64) BINARIES="golangci-lint" ;;
    94      netbsd/armv6) BINARIES="golangci-lint" ;;
    95      netbsd/armv7) BINARIES="golangci-lint" ;;
    96      windows/386) BINARIES="golangci-lint" ;;
    97      windows/amd64) BINARIES="golangci-lint" ;;
    98      windows/arm64) BINARIES="golangci-lint" ;;
    99      windows/armv6) BINARIES="golangci-lint" ;;
   100      windows/armv7) BINARIES="golangci-lint" ;;
   101      windows/mips64) BINARIES="golangci-lint" ;;
   102      windows/mips64le) BINARIES="golangci-lint" ;;
   103      windows/ppc64le) BINARIES="golangci-lint" ;;
   104      windows/s390x) BINARIES="golangci-lint" ;;
   105      *)
   106        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"
   107        exit 1
   108        ;;
   109    esac
   110  }
   111  tag_to_version() {
   112    if [ -z "${TAG}" ]; then
   113      log_info "checking GitHub for latest tag"
   114    else
   115      log_info "checking GitHub for tag '${TAG}'"
   116    fi
   117    REALTAG=$(github_release "$OWNER/$REPO" "${TAG}") && true
   118    if test -z "$REALTAG"; then
   119      log_crit "unable to find '${TAG}' - use 'latest' or see https://github.com/${PREFIX}/releases for details"
   120      exit 1
   121    fi
   122    # if version starts with 'v', remove it
   123    TAG="$REALTAG"
   124    VERSION=${TAG#v}
   125  }
   126  adjust_format() {
   127    # change format (tar.gz or zip) based on OS
   128    case ${OS} in
   129      windows) FORMAT=zip ;;
   130    esac
   131    true
   132  }
   133  adjust_os() {
   134    # adjust archive name based on OS
   135    true
   136  }
   137  adjust_arch() {
   138    # adjust archive name based on ARCH
   139    true
   140  }
   141  
   142  cat /dev/null <<EOF
   143  ------------------------------------------------------------------------
   144  https://github.com/client9/shlib - portable posix shell functions
   145  Public domain - http://unlicense.org
   146  https://github.com/client9/shlib/blob/master/LICENSE.md
   147  but credit (and pull requests) appreciated.
   148  ------------------------------------------------------------------------
   149  EOF
   150  is_command() {
   151    command -v "$1" >/dev/null
   152  }
   153  echoerr() {
   154    echo "$@" 1>&2
   155  }
   156  log_prefix() {
   157    echo "$0"
   158  }
   159  _logp=6
   160  log_set_priority() {
   161    _logp="$1"
   162  }
   163  log_priority() {
   164    if test -z "$1"; then
   165      echo "$_logp"
   166      return
   167    fi
   168    [ "$1" -le "$_logp" ]
   169  }
   170  log_tag() {
   171    case $1 in
   172      0) echo "emerg" ;;
   173      1) echo "alert" ;;
   174      2) echo "crit" ;;
   175      3) echo "err" ;;
   176      4) echo "warning" ;;
   177      5) echo "notice" ;;
   178      6) echo "info" ;;
   179      7) echo "debug" ;;
   180      *) echo "$1" ;;
   181    esac
   182  }
   183  log_debug() {
   184    log_priority 7 || return 0
   185    echoerr "$(log_prefix)" "$(log_tag 7)" "$@"
   186  }
   187  log_info() {
   188    log_priority 6 || return 0
   189    echoerr "$(log_prefix)" "$(log_tag 6)" "$@"
   190  }
   191  log_err() {
   192    log_priority 3 || return 0
   193    echoerr "$(log_prefix)" "$(log_tag 3)" "$@"
   194  }
   195  log_crit() {
   196    log_priority 2 || return 0
   197    echoerr "$(log_prefix)" "$(log_tag 2)" "$@"
   198  }
   199  uname_os() {
   200    os=$(uname -s | tr '[:upper:]' '[:lower:]')
   201    case "$os" in
   202      msys*) os="windows" ;;
   203      mingw*) os="windows" ;;
   204      cygwin*) os="windows" ;;
   205      win*) os="windows" ;;
   206      sunos) [ $(uname -o) == "illumos" ] && os=illumos ;;
   207    esac
   208    echo "$os"
   209  }
   210  uname_arch() {
   211    arch=$(uname -m)
   212    case $arch in
   213      x86_64) arch="amd64" ;;
   214      x86) arch="386" ;;
   215      i686) arch="386" ;;
   216      i386) arch="386" ;;
   217      i86pc) arch="amd64" ;;
   218      aarch64) arch="arm64" ;;
   219      armv5*) arch="armv5" ;;
   220      armv6*) arch="armv6" ;;
   221      armv7*) arch="armv7" ;;
   222      loongarch64) arch="loong64" ;;
   223    esac
   224    echo ${arch}
   225  }
   226  uname_os_check() {
   227    os=$(uname_os)
   228    case "$os" in
   229      darwin) return 0 ;;
   230      dragonfly) return 0 ;;
   231      freebsd) return 0 ;;
   232      illumos) return 0;;
   233      linux) return 0 ;;
   234      android) return 0 ;;
   235      nacl) return 0 ;;
   236      netbsd) return 0 ;;
   237      openbsd) return 0 ;;
   238      plan9) return 0 ;;
   239      solaris) return 0 ;;
   240      windows) return 0 ;;
   241    esac
   242    log_crit "uname_os_check '$(uname -s)' got converted to '$os' which is not a GOOS value."
   243    return 1
   244  }
   245  uname_arch_check() {
   246    arch=$(uname_arch)
   247    case "$arch" in
   248      386) return 0 ;;
   249      amd64) return 0 ;;
   250      arm64) return 0 ;;
   251      armv5) return 0 ;;
   252      armv6) return 0 ;;
   253      armv7) return 0 ;;
   254      ppc64) return 0 ;;
   255      ppc64le) return 0 ;;
   256      mips) return 0 ;;
   257      mipsle) return 0 ;;
   258      mips64) return 0 ;;
   259      mips64le) return 0 ;;
   260      s390x) return 0 ;;
   261      riscv64) return 0 ;;
   262      amd64p32) return 0 ;;
   263      loong64) return 0 ;;
   264    esac
   265    log_crit "uname_arch_check '$(uname -m)' got converted to '$arch' which is not a GOARCH value."
   266    return 1
   267  }
   268  untar() {
   269    tarball=$1
   270    case "${tarball}" in
   271      *.tar.gz | *.tgz) tar --no-same-owner -xzf "${tarball}" ;;
   272      *.tar) tar --no-same-owner -xf "${tarball}" ;;
   273      *.zip) unzip "${tarball}" ;;
   274      *)
   275        log_err "untar unknown archive format for ${tarball}"
   276        return 1
   277        ;;
   278    esac
   279  }
   280  http_download_curl() {
   281    local_file=$1
   282    source_url=$2
   283    header=$3
   284    if [ -z "$header" ]; then
   285      code=$(curl -w '%{http_code}' -sL -o "$local_file" "$source_url")
   286    else
   287      code=$(curl -w '%{http_code}' -sL -H "$header" -o "$local_file" "$source_url")
   288    fi
   289    if [ "$code" != "200" ]; then
   290      log_debug "http_download_curl received HTTP status $code"
   291      return 1
   292    fi
   293    return 0
   294  }
   295  http_download_wget() {
   296    local_file=$1
   297    source_url=$2
   298    header=$3
   299    if [ -z "$header" ]; then
   300      wget -q -O "$local_file" "$source_url"
   301    else
   302      wget -q --header "$header" -O "$local_file" "$source_url"
   303    fi
   304  }
   305  http_download() {
   306    log_debug "http_download $2"
   307    if is_command curl; then
   308      http_download_curl "$@"
   309      return
   310    elif is_command wget; then
   311      http_download_wget "$@"
   312      return
   313    fi
   314    log_crit "http_download unable to find wget or curl"
   315    return 1
   316  }
   317  http_copy() {
   318    tmp=$(mktemp)
   319    http_download "${tmp}" "$1" "$2" || return 1
   320    body=$(cat "$tmp")
   321    rm -f "${tmp}"
   322    echo "$body"
   323  }
   324  github_release() {
   325    owner_repo=$1
   326    version=$2
   327    test -z "$version" && version="latest"
   328    giturl="https://github.com/${owner_repo}/releases/${version}"
   329    json=$(http_copy "$giturl" "Accept:application/json")
   330    test -z "$json" && return 1
   331    version=$(echo "$json" | tr -s '\n' ' ' | sed 's/.*"tag_name":"//' | sed 's/".*//')
   332    test -z "$version" && return 1
   333    echo "$version"
   334  }
   335  hash_sha256() {
   336    TARGET=${1:-/dev/stdin}
   337    if is_command gsha256sum; then
   338      hash=$(gsha256sum "$TARGET") || return 1
   339      echo "$hash" | cut -d ' ' -f 1
   340    elif is_command sha256sum; then
   341      hash=$(sha256sum "$TARGET") || return 1
   342      echo "$hash" | cut -d ' ' -f 1
   343    elif is_command shasum; then
   344      hash=$(shasum -a 256 "$TARGET" 2>/dev/null) || return 1
   345      echo "$hash" | cut -d ' ' -f 1
   346    elif is_command openssl; then
   347      hash=$(openssl -dst openssl dgst -sha256 "$TARGET") || return 1
   348      echo "$hash" | cut -d ' ' -f a
   349    else
   350      log_crit "hash_sha256 unable to find command to compute sha-256 hash"
   351      return 1
   352    fi
   353  }
   354  hash_sha256_verify() {
   355    TARGET=$1
   356    checksums=$2
   357    if [ -z "$checksums" ]; then
   358      log_err "hash_sha256_verify checksum file not specified in arg2"
   359      return 1
   360    fi
   361    BASENAME=${TARGET##*/}
   362    want=$(grep "${BASENAME}" "${checksums}" 2>/dev/null | tr '\t' ' ' | cut -d ' ' -f 1)
   363    if [ -z "$want" ]; then
   364      log_err "hash_sha256_verify unable to find checksum for '${TARGET}' in '${checksums}'"
   365      return 1
   366    fi
   367    got=$(hash_sha256 "$TARGET")
   368    if [ "$want" != "$got" ]; then
   369      log_err "hash_sha256_verify checksum for '$TARGET' did not verify ${want} vs $got"
   370      return 1
   371    fi
   372  }
   373  cat /dev/null <<EOF
   374  ------------------------------------------------------------------------
   375  End of functions from https://github.com/client9/shlib
   376  ------------------------------------------------------------------------
   377  EOF
   378  
   379  PROJECT_NAME="golangci-lint"
   380  OWNER=golangci
   381  REPO="golangci-lint"
   382  BINARY=golangci-lint
   383  FORMAT=tar.gz
   384  OS=$(uname_os)
   385  ARCH=$(uname_arch)
   386  PREFIX="$OWNER/$REPO"
   387  
   388  # use in logging routines
   389  log_prefix() {
   390  	echo "$PREFIX"
   391  }
   392  PLATFORM="${OS}/${ARCH}"
   393  GITHUB_DOWNLOAD=https://github.com/${OWNER}/${REPO}/releases/download
   394  
   395  uname_os_check "$OS"
   396  uname_arch_check "$ARCH"
   397  
   398  parse_args "$@"
   399  
   400  get_binaries
   401  
   402  tag_to_version
   403  
   404  adjust_format
   405  
   406  adjust_os
   407  
   408  adjust_arch
   409  
   410  log_info "found version: ${VERSION} for ${TAG}/${OS}/${ARCH}"
   411  
   412  NAME=${BINARY}-${VERSION}-${OS}-${ARCH}
   413  TARBALL=${NAME}.${FORMAT}
   414  TARBALL_URL=${GITHUB_DOWNLOAD}/${TAG}/${TARBALL}
   415  CHECKSUM=${PROJECT_NAME}-${VERSION}-checksums.txt
   416  CHECKSUM_URL=${GITHUB_DOWNLOAD}/${TAG}/${CHECKSUM}
   417  
   418  
   419  execute