blob: ef0840777e1989d66d405a8ede636697d7dc66c7 [file] [log] [blame]
Joey Armstrong3451f622023-01-11 16:58:56 -05001#!/bin/sh
2set -e
3# Code generated by godownloader on 2019-05-28T19:49:53Z. DO NOT EDIT.
4#
5
6usage() {
7 this=$1
8 cat <<EOF
9$this: download go binaries for boz/kail
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/boz/kail/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
49 # ---------------------------------------------------------
50 # [JOEY] - strange, curl fails to retrieve but wget is fine.
51 # ---------------------------------------------------------
52 # http_download "${tmpdir}/${TARBALL}" "${TARBALL_URL}"
53 http_download_wget "${tmpdir}/${TARBALL}" "${TARBALL_URL}"
54
55 http_download "${tmpdir}/${CHECKSUM}" "${CHECKSUM_URL}"
56 hash_sha256_verify "${tmpdir}/${TARBALL}" "${tmpdir}/${CHECKSUM}"
57 srcdir="${tmpdir}"
58 (cd "${tmpdir}" && untar "${TARBALL}")
59 test ! -d "${BINDIR}" && install -d "${BINDIR}"
60 for binexe in "kail" ; do
61 if [ "$OS" = "windows" ]; then
62 binexe="${binexe}.exe"
63 fi
64 install "${srcdir}/${binexe}" "${BINDIR}/"
65 log_info "installed ${BINDIR}/${binexe}"
66 done
67 rm -rf "${tmpdir}"
68}
69is_supported_platform() {
70 platform=$1
71 found=1
72 case "$platform" in
73 darwin/amd64) found=0 ;;
74 linux/amd64) found=0 ;;
75 esac
76 return $found
77}
78check_platform() {
79 if is_supported_platform "$PLATFORM"; then
80 # optional logging goes here
81 true
82 else
83 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"
84 exit 1
85 fi
86}
87tag_to_version() {
88 if [ -z "${TAG}" ]; then
89 log_info "checking GitHub for latest tag"
90 else
91 log_info "checking GitHub for tag '${TAG}'"
92 fi
93 REALTAG=$(github_release "$OWNER/$REPO" "${TAG}") && true
94 if test -z "$REALTAG"; then
95 log_crit "unable to find '${TAG}' - use 'latest' or see https://github.com/${PREFIX}/releases for details"
96 exit 1
97 fi
98 # if version starts with 'v', remove it
99 TAG="$REALTAG"
100 VERSION=${TAG#v}
101}
102adjust_format() {
103 # change format (tar.gz or zip) based on OS
104 true
105}
106adjust_os() {
107 # adjust archive name based on OS
108 true
109}
110adjust_arch() {
111 # adjust archive name based on ARCH
112 true
113}
114
115cat /dev/null <<EOF
116------------------------------------------------------------------------
117https://github.com/client9/shlib - portable posix shell functions
118Public domain - http://unlicense.org
119https://github.com/client9/shlib/blob/master/LICENSE.md
120but credit (and pull requests) appreciated.
121------------------------------------------------------------------------
122EOF
123is_command() {
124 command -v "$1" >/dev/null
125}
126echoerr() {
127 echo "$@" 1>&2
128}
129log_prefix() {
130 echo "$0"
131}
132_logp=6
133log_set_priority() {
134 _logp="$1"
135}
136log_priority() {
137 if test -z "$1"; then
138 echo "$_logp"
139 return
140 fi
141 [ "$1" -le "$_logp" ]
142}
143log_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}
156log_debug() {
157 log_priority 7 || return 0
158 echoerr "$(log_prefix)" "$(log_tag 7)" "$@"
159}
160log_info() {
161 log_priority 6 || return 0
162 echoerr "$(log_prefix)" "$(log_tag 6)" "$@"
163}
164log_err() {
165 log_priority 3 || return 0
166 echoerr "$(log_prefix)" "$(log_tag 3)" "$@"
167}
168log_crit() {
169 log_priority 2 || return 0
170 echoerr "$(log_prefix)" "$(log_tag 2)" "$@"
171}
172uname_os() {
173 os=$(uname -s | tr '[:upper:]' '[:lower:]')
174 case "$os" in
175 msys_nt) os="windows" ;;
176 esac
177 echo "$os"
178}
179uname_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}
193uname_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}
211uname_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}
232untar() {
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}
244http_download_curl() {
245 local_file=$1
246 source_url=$2
247 header=$3
248 if [ -z "$header" ]; then
249 code=$(curl -w '%{http_code}' -sL -o "$local_file" "$source_url")
250 else
251 code=$(curl -w '%{http_code}' -sL -H "$header" -o "$local_file" "$source_url")
252 fi
253 if [ "$code" != "200" ]; then
254 log_debug "http_download_curl received HTTP status $code"
255 return 1
256 fi
257 return 0
258}
259http_download_wget() {
260 local_file=$1
261 source_url=$2
262 header=$3
263 if [ -z "$header" ]; then
264 wget -q -O "$local_file" "$source_url"
265 else
266 wget -q --header "$header" -O "$local_file" "$source_url"
267 fi
268}
269http_download() {
270 log_debug "http_download $2"
271 if is_command curl; then
272 http_download_curl "$@"
273 return
274 elif is_command wget; then
275 http_download_wget "$@"
276 return
277 fi
278 log_crit "http_download unable to find wget or curl"
279 return 1
280}
281http_copy() {
282 tmp=$(mktemp)
283 http_download "${tmp}" "$1" "$2" || return 1
284 body=$(cat "$tmp")
285 rm -f "${tmp}"
286 echo "$body"
287}
288github_release() {
289 owner_repo=$1
290 version=$2
291 test -z "$version" && version="latest"
292 giturl="https://github.com/${owner_repo}/releases/${version}"
293 json=$(http_copy "$giturl" "Accept:application/json")
294 test -z "$json" && return 1
295 version=$(echo "$json" | tr -s '\n' ' ' | sed 's/.*"tag_name":"//' | sed 's/".*//')
296 test -z "$version" && return 1
297 echo "$version"
298}
299hash_sha256() {
300 TARGET=${1:-/dev/stdin}
301 if is_command gsha256sum; then
302 hash=$(gsha256sum "$TARGET") || return 1
303 echo "$hash" | cut -d ' ' -f 1
304 elif is_command sha256sum; then
305 hash=$(sha256sum "$TARGET") || return 1
306 echo "$hash" | cut -d ' ' -f 1
307 elif is_command shasum; then
308 hash=$(shasum -a 256 "$TARGET" 2>/dev/null) || return 1
309 echo "$hash" | cut -d ' ' -f 1
310 elif is_command openssl; then
311 hash=$(openssl -dst openssl dgst -sha256 "$TARGET") || return 1
312 echo "$hash" | cut -d ' ' -f a
313 else
314 log_crit "hash_sha256 unable to find command to compute sha-256 hash"
315 return 1
316 fi
317}
318hash_sha256_verify() {
319 TARGET=$1
320 checksums=$2
321 if [ -z "$checksums" ]; then
322 log_err "hash_sha256_verify checksum file not specified in arg2"
323 return 1
324 fi
325 BASENAME=${TARGET##*/}
326 want=$(grep "${BASENAME}" "${checksums}" 2>/dev/null | tr '\t' ' ' | cut -d ' ' -f 1)
327 if [ -z "$want" ]; then
328 log_err "hash_sha256_verify unable to find checksum for '${TARGET}' in '${checksums}'"
329 return 1
330 fi
331 got=$(hash_sha256 "$TARGET")
332 if [ "$want" != "$got" ]; then
333 log_err "hash_sha256_verify checksum for '$TARGET' did not verify ${want} vs $got"
334 return 1
335 fi
336}
337cat /dev/null <<EOF
338------------------------------------------------------------------------
339End of functions from https://github.com/client9/shlib
340------------------------------------------------------------------------
341EOF
342
343PROJECT_NAME="kail"
344OWNER=boz
345REPO="kail"
346BINARY=kail
347FORMAT=tar.gz
348OS=$(uname_os)
349ARCH=$(uname_arch)
350PREFIX="$OWNER/$REPO"
351
352# use in logging routines
353log_prefix() {
354 echo "$PREFIX"
355}
356PLATFORM="${OS}/${ARCH}"
357GITHUB_DOWNLOAD=https://github.com/${OWNER}/${REPO}/releases/download
358
359uname_os_check "$OS"
360uname_arch_check "$ARCH"
361
362parse_args "$@"
363
364check_platform
365
366tag_to_version
367
368adjust_format
369
370adjust_os
371
372adjust_arch
373
374log_info "found version: ${VERSION} for ${TAG}/${OS}/${ARCH}"
375
376# ---------------------------------------------------------
377# [JOEY] - as of v0.16.1, download name contains an embedded 'v'
378# ---------------------------------------------------------
379# NAME=${PROJECT_NAME}_${VERSION}_${OS}_${ARCH}
380NAME=${PROJECT_NAME}_v${VERSION}_${OS}_${ARCH}
381
382TARBALL=${NAME}.${FORMAT}
383TARBALL_URL=${GITHUB_DOWNLOAD}/${TAG}/${TARBALL}
384CHECKSUM=checksums.txt
385CHECKSUM_URL=${GITHUB_DOWNLOAD}/${TAG}/${CHECKSUM}
386
387execute