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