install-task.sh 9.4 KB

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