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