github.com/yuqengo/golangci-lint@v0.0.2/install.sh (about)

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