Skip to content

Commit b707cc7

Browse files
committed
Add install.sh
1 parent eb36373 commit b707cc7

File tree

1 file changed

+374
-0
lines changed

1 file changed

+374
-0
lines changed

install.sh

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

0 commit comments

Comments
 (0)