This repository has been archived by the owner on Jan 14, 2022. It is now read-only.
/
godownloader-goreleaser.sh
145 lines (128 loc) · 3.55 KB
/
godownloader-goreleaser.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/bin/sh
set -e
# Code generated by godownloader. DO NOT EDIT.
OWNER=goreleaser
REPO=goreleaser
BINARY=goreleaser
FORMAT=tar.gz
BINDIR=${BINDIR:-./bin}
VERSION=$1
if [ -z "${VERSION}" ]; then
echo ""
echo "$0: download go binaries for ${OWNER}/${REPO}"
echo ""
echo "Usage: $0 [version]"
echo " where [version] is 'latest' or a version number from"
echo " https://github.com/${OWNER}/${REPO}/releases"
echo ""
echo "Generated by godownloader"
echo " https://github.com/goreleaser/godownloader"
echo ""
exit 1
fi
# portable sha256sum
checksum() {
TARGET=$1
if which gsha256sum > /dev/null; then
# mac homebrew, others
gsha256sum $TARGET | cut -d ' ' -f 1
elif which sha256sum > /dev/null; then
# gnu, busybox
sha256sum $TARGET | cut -d ' ' -f 1
elif which shasum > /dev/null; then
# darwin, freebsd?
shasum -a 256 $TARGET | cut -d ' ' -f 1
elif which openssl > /dev/null; then
openssl -dst openssl dgst -sha256 $TARGET | cut -d ' ' -f a
else
echo "Unable to compute hash. exiting"
exit 1
fi
}
# download dest source
# if dest is "-", then output to stdout
# if source is api.github.com add auth token
download() {
DEST=$1
SOURCE=$2
HEADER=""
case $SOURCE in
https://api.github.com*)
test -z "$GITHUB_TOKEN" || HEADER="Authorization: token $GITHUB_TOKEN"
;;
esac
if which curl > /dev/null; then
WGET="curl --fail -sSL"
test -z "$GITHUB_TOKEN" || WGET="${WGET} -H \"${HEADER}\""
if [ "${DEST}" != "-" ]; then
WGET="$WGET -o $DEST"
fi
elif which wget > /dev/null; then
WGET="wget -q -O $DEST"
test -z "$GITHUB_TOKEN" || WGET="${WGET} --header \"${HEADER}\""
else
echo "Unable to find wget or curl. Exit"
exit 1
fi
${WGET} ${SOURCE}
}
if [ "${VERSION}" = "latest" ]; then
echo "Checking GitHub for latest version of ${OWNER}/${REPO}"
VERSION=$(download - https://api.github.com/repos/${OWNER}/${REPO}/releases/latest | grep -m 1 "\"name\":" | cut -d ":" -f 2 | tr -d ' ",')
if [ -z "${VERSION}" ]; then
echo "Unable to determine latest release for ${OWNER}/${REPO}"
exit 1
fi
fi
# if version starts with 'v', remove it
VERSION=${VERSION#v}
OS=$(uname -s)
ARCH=$(uname -m)
# change format (tar.gz or zip) based on ARCH
case ${ARCH} in
Windows) FORMAT=zip ;;
esac
# adjust archive name based on OS
if [ ! -z "${ARM}" ]; then ARM="v$ARM"; fi
NAME=${BINARY}_${OS}_${ARCH}${ARM}
TARBALL=${NAME}.${FORMAT}
TARBALL_URL=https://github.com/${OWNER}/${REPO}/releases/download/v${VERSION}/${TARBALL}
CHECKSUM=${REPO}_checksums.txt
CHECKSUM_URL=https://github.com/${OWNER}/${REPO}/releases/download/v${VERSION}/${CHECKSUM}
# Destructive operations start here
#
#
test -z "$TMPDIR" && TMPDIR="$(mktemp -d)"
mkdir -p ${TMPDIR}
rm -f ${TMPDIR}/${TARBALL}
download ${TMPDIR}/${TARBALL} ${TARBALL_URL}
# checksum goes here
if [ 1 -eq 1 ]; then
rm -f ${TMPDIR}/${CHECKSUM}
download ${TMPDIR}/${CHECKSUM} ${CHECKSUM_URL}
# remove tabs: old version of goreleaser used them
# https://github.com/goreleaser/goreleaser/issues/233
# fixed 2017-05-11
WANT=$(grep ${TARBALL} ${TMPDIR}/${CHECKSUM} | tr '\t' ' ' | cut -d ' ' -f 1)
GOT=$(checksum $TMPDIR/$TARBALL)
if [ "$GOT" != "$WANT" ]; then
echo "Checksum for $TMPDIR/$TARBALL did not verify"
echo "WANT: ${WANT}"
echo "GOT : ${GOT}"
exit 1
fi
fi
case ${FORMAT} in
tar.gz)
tar -C ${TMPDIR} -xzf ${TMPDIR}/${TARBALL}
;;
zip)
(cd ${TMPDIR} && unzip ${TARBALL})
;;
*)
echo "unknown format '${FORMAT}' - exiting"
exit 1
;;
esac
install -d ${BINDIR}
install ${TMPDIR}/${BINARY} ${BINDIR}/