github.com/nektos/act@v0.2.83/install.sh (about)

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