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