PageRenderTime 62ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/godownloader.sh

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