Skip to content

Commit 6774873

Browse files
committed
Add install.sh
1 parent e7c929b commit 6774873

File tree

1 file changed

+375
-0
lines changed

1 file changed

+375
-0
lines changed

install.sh

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

0 commit comments

Comments
 (0)