-
Notifications
You must be signed in to change notification settings - Fork 4
/
installer.sh
executable file
·326 lines (274 loc) · 8.05 KB
/
installer.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#!/bin/sh
# Github release installer
#
# Heavily inspired by:
# - https://github.com/goreleaser/godownloader
#
# Shoutout to:
# - https://www.codetinkerer.com/is-shell-portable/
# - https://google.github.io/styleguide/shellguide.html although the guide is for BASH not SH
# run in subshell
(
# config
BINARY=bns
BREW_TAP=bunnyshell/tap/bunnyshell-cli
REPOSITORY=bunnyshell/cli
# install options
RELEASE_VERSION=${RELEASE_VERSION:-'latest'}
PREFER_BREW=${PREFER_BREW:-false}
INSTALL_PATH=${INSTALL_PATH:-.}
SUDO_INSTALL=${SUDO_INSTALL:-false}
if [ "${DEBUG_INSTALLER}" = true ]; then
set -x
fi
# main functions
main() {
if has_brew; then
install_with_brew
elif is_command "${BINARY}"; then
if has_correct_binary; then
upgrade;
else
throw "There is another binary called '${BINARY}' in your PATH"
fi
else
install
fi
}
has_brew() {
if [ "${PREFER_BREW}" != true ]; then
return 1
fi
is_command brew;
}
install_with_brew() {
log_info 'Using detected brew installation'
set -x
brew install "${BREW_TAP}"
}
upgrade() {
INSTALLED_VERSION=$(show_binary_version "${BINARY}")
log_info "Already installed. ${INSTALLED_VERSION}"
tmp_install=$(fetch_binary);EXIT_CODE=$?
if [ "${EXIT_CODE}" -ne 0 ]; then
issue 'Something went wrong with the installation'
fi
UPGRADED_VERSION=$(
show_binary_version "${tmp_install}"
);EXIT_CODE=$?
if [ "${EXIT_CODE}" -ne 0 ]; then
log_error 'Something went wrong with the installation'
issue "Consider manually checking ${tmp_install}"
fi
if [ "${INSTALLED_VERSION}" = "${UPGRADED_VERSION}" ]; then
log_info 'You are using the latest version'
rm -r "${tmp_install}"
exit 0
fi
release "${tmp_install}"
}
install() {
log_info 'Fresh install required...'
tmp_install=$(fetch_binary);EXIT_CODE=$?
if [ "${EXIT_CODE}" -ne 0 ]; then
issue 'Something went wrong with the installation'
fi
release "${tmp_install}"
}
fetch_binary() {
if ! is_command tar; then
throw 'You need tar binary to unpack github archive'
fi
version=$(github_release_version);EXIT_CODE=$?
if [ "${EXIT_CODE}" -ne 0 ]; then
throw "Installer could not determine version for ${RELEASE_VERSION}."
fi
os=$(goreleaser_os);EXIT_CODE=$?
if [ "${EXIT_CODE}" -ne 0 ]; then
issue "Installer cannot handle OS ${os}"
fi
arch=$(goreleaser_arch);EXIT_CODE=$?
if [ "${EXIT_CODE}" -ne 0 ]; then
issue "Installer cannot handle OS ${os} and ARCH ${arch}"
fi
source="https://github.com/${REPOSITORY}/releases/download/v${version}/${BINARY}_${version}_${os}_${arch}.tar.gz"
github_download "$source"
}
release() {
tmp_install=$1
maybe_sudo=''
if [ "${SUDO_INSTALL}" = true ]; then
maybe_sudo='sudo'
log_info 'Detected SUDO_INSTALL. Password might be required...'
fi
install_file="${INSTALL_PATH}/${BINARY}"
RESULT=$(
${maybe_sudo} mv "${tmp_install}" "${install_file}" \
\
2>&1 \
;
);EXIT_CODE=$?
if [ "${EXIT_CODE}" -ne 0 ]; then
throw "${RESULT}"
fi
log_info "Installed '${BINARY}' in ${INSTALL_PATH}"
log_info "$(show_binary_version "${install_file}")"
if ! is_command "${BINARY}"; then
absolute_install_path=$(realpath "${INSTALL_PATH}")
update_path=$(
printf 'You will need to update your %sPATH variable or run "mv %s %s"' '$' "${absolute_install_path}/${BINARY}" '/usr/local/bin'
)
log_info "${update_path}"
fi
}
# binary functions
has_correct_binary() {
HEADER=$(
${BINARY} --help | head -1
);EXIT_CODE=$?
if [ "${EXIT_CODE}" -ne 0 ]; then
return 1
fi
case $HEADER in
("Bunnyshell CLI"*) return 0 ;;
*) return 1 ;;
esac
}
show_binary_version() {
$1 version --client=true
}
# goreleaser binary naming from .goreleaser.yaml
goreleaser_os() {
os=$(uname -s | tr '[:upper:]' '[:lower:]')
case $os in
darwin) echo "Darwin" ;;
linux) echo "Linux" ;;
*) echo "${os}"; return 1;
esac
}
goreleaser_arch() {
arch=$(uname -m)
case $arch in
x86_64) echo 'x86_64' ;;
i386) echo 'i386' ;;
aarch64) echo 'arm64' ;;
arm64) echo 'arm64' ;;
*) echo "${arch}"; return 1;
esac
}
# fetcher logic
fetch() {
source_url=$1
tmp=$(tmp_file)
echo "${tmp}"
download "$source_url" "${tmp}"
}
fetch_response() {
tmp=$(fetch "$1");
cat "${tmp}"
rm -rf "${tmp}"
}
download() {
if is_command curl; then
download_with_curl "$1" "$2"
elif is_command wget; then
download_with_wget "$1" "$2"
else
throw 'You need either curl or wget to download remote files'
fi
}
download_with_curl() {
source_url=$1
local_file=$2
RESPONSE=$(
curl "$source_url" --silent --location --fail \
--output "$local_file"\
--header 'Accept: application/json' \
;
);EXIT_CODE=$?
if [ "${EXIT_CODE}" -ne 0 ]; then
throw "Could not download ${source_url} to ${local_file}: ${RESPONSE}"
fi
return 0
}
download_with_wget() {
source_url=$1
local_file=$2
RESPONSE=$(
wget "$source_url" --quiet \
--output "$local_file" \
--header 'Accept: application/json' \
;
);EXIT_CODE=$?
if [ "${EXIT_CODE}" -ne 0 ]; then
throw "Could not download ${source_url} to ${local_file}: ${RESPONSE}"
fi
return 0
}
# github operations
github_extract_binary() {
archive=$1
extract_dir=$(tmp_dir)
result=$(
tar --extract --gzip \
--directory "${extract_dir}" \
--file "${archive}" \
${BINARY} \
\
2>&1 \
;
);EXIT_CODE=$?
if [ "${EXIT_CODE}" -ne 0 ]; then
issue "Could not extract ${BINARY}: ${result}"
fi
tmp_binary=$(tmp_file)
mv "${extract_dir}/${BINARY}" "${tmp_binary}"
rm -r "${extract_dir}"
echo "${tmp_binary}"
}
github_download() {
tmp_archive=$(fetch "$1") || throw 'Could not download archive'
tmp_binary=$(github_extract_binary "${tmp_archive}")
rm -rf "${tmp_archive}"
echo "${tmp_binary}"
}
github_release_version() {
json=$(fetch_response "https://github.com/${REPOSITORY}/releases/${RELEASE_VERSION}")
test -z "${json}" && return 1
version=$(
echo "${json}" \
| tr -s '\n' ' ' \
| sed 's/.*"tag_name":"v//' \
| sed 's/".*//' \
;
)
test -z "${version}" && return 1
echo "${version}"
}
# helpers
tmp_dir() {
mktemp --quiet --directory
}
tmp_file() {
mktemp --quiet
}
issue() {
log_error "$@"
log_error "Let us know: https://github.com/${REPOSITORY}/issues"
exit 2
}
throw() {
log_error "$@"
exit 1
}
log_error() {
echo '[ERROR]' "$@" >&2
}
log_info() {
echo '[INFO]' "$@"
}
is_command() {
command -v "$1" > /dev/null
}
main "$@"
)