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