github.com/pankona/gometalinter@v2.0.11+incompatible/scripts/install.sh (about)

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