-
-
Notifications
You must be signed in to change notification settings - Fork 101
/
Copy pathdryup.sh
executable file
·416 lines (326 loc) · 8.35 KB
/
dryup.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
#!/bin/sh
set -u # Undefined variables are errors
main() {
assert_cmds
set_globals
handle_command_line_args "$@"
}
set_globals() {
#Environment sanity checks
assert_nz "$HOME" "\$HOME is undefined"
assert_nz "$0" "\$0 is undefined"
#script version
version=0.0.1
#location of the distribution server
dist_server="https://github.com/moncho/dry/releases/download"
version_file_url="https://raw.githubusercontent.com/moncho/dry/master/APPVERSION"
#Install prefix
default_prefix="${DRY_PREFIX-/usr/local/bin}"
#Downloads go here
dl_dir="/tmp"
flag_verbose=false
if [ -n "${VERBOSE-}" ]; then
flag_verbose=true
fi
}
handle_command_line_args() {
local _prefix="$default_prefix"
local _help=false
for arg in "$@"; do
case "$arg" in
--help )
_help=true
;;
--verbose)
# verbose is a global flag
flag_verbose=true
;;
--version)
echo "dry.sh $version"
exit 0
;;
esac
if is_value_arg "$arg" "prefix"; then
_prefix="$(get_value_arg "$arg")"
fi
done
if [ "$_help" = true ]; then
print_help
exit 0
fi
download_install_dry "$_prefix"
}
is_value_arg() {
local _arg="$1"
local _name="$2"
echo "$arg" | grep -q -- "--$_name="
return $?
}
get_value_arg() {
local _arg="$1"
echo "$_arg" | cut -f2 -d=
}
# Returns 0 on success, 1 on error
download_install_dry() {
local _prefix="$1"
local _errors=0
# Dry version
get_latest_dry_version
dry_version="$RETVAL"
#dry_version="v0.3-beta.1"
assert_nz "$dry_version" "dry_version from repository"
determine_binary || return 1
local _dry_binary="$RETVAL"
local _dry_binary_file=""
determine_remote_dry "$_dry_binary" || return 1
local _remote_dry_binary="$RETVAL"
assert_nz "$_remote_dry_binary" "remote dry binary"
verbose_say "remote dry binary location: $_remote_dry_binary"
# Download and install dry
say "downloading dry binary"
download_and_check "$_remote_dry_binary" false
if [ $? != 0 ]; then
say "failed to download binary"
_errors=1
else
local _dry_binary_file="$RETVAL"
assert_nz "$_dry_binary_file" "dry_binary_file"
install_dry "$_dry_binary_file" "$_prefix"
if [ $? != 0 ]; then
say_err "failed to install dry"
_errors=1
fi
fi
if [ "$_errors" -ne 0 ]; then
say_err "there were errors during the installation"
if [ -f "$_dry_binary_file" ]; then
run rm "$_dry_binary_file"
fi
return 1
fi
say "dry binary was copied to $_prefix, now you should 'sudo chmod 755 $_prefix/dry'"
}
install_dry() {
local _dry_file="$1"
local _prefix="$2"
say "Moving dry binary to its destination"
verbose_say "moving $_dry_file to $_prefix/dry"
run mv "$_dry_file" "$_prefix/dry"
if [ $? != 0 ]; then
say "Failed to move binary to $_prefix/dry"
return 1
fi
verbose_say "moved dry to its destination"
return 0
}
determine_remote_dry() {
local _binary=$1
verbose_say "figuring out remote binary "
local _remote_dry="$dist_server/$dry_version/$_binary"
verbose_say "binary is $_remote_dry"
RETVAL="$_remote_dry"
}
determine_binary() {
verbose_say "figuring out dry binary "
get_architecture || return 1
local _arch="$RETVAL"
assert_nz "$_arch" "arch"
local _bin="dry-$_arch"
verbose_say "binary is $_bin"
RETVAL="$_bin"
}
get_architecture() {
verbose_say "detecting architecture"
local _ostype="$(uname -s)"
local _cputype="$(uname -m)"
local _isarm
verbose_say "uname -s reports: $_ostype"
verbose_say "uname -m reports: $_cputype"
if [ "$_ostype" = Darwin -a "$_cputype" = i386 ]; then
# Darwin `uname -s` lies
if sysctl hw.optional.x86_64 | grep -q ': 1'; then
local _cputype=x86_64
fi
fi
case "$_ostype" in
Linux)
local _ostype=linux
;;
FreeBSD)
local _ostype=freebsd
;;
Darwin)
local _ostype=darwin
;;
MINGW* | MSYS*)
local _ostype=windows
;;
*)
err "unrecognized OS type: $_ostype"
;;
esac
case "$_cputype" in
i386 | i486 | i686 | i786 | x86)
local _cputype=386
;;
x86_64 | x86-64 | x64 | amd64)
local _cputype=amd64
;;
aarch64 | arm64)
local _cputype=arm64
;;
arm*)
local _cputype=arm
;;
*)
err "unknown CPU type: $CFG_CPUTYPE"
esac
# Detect 64-bit linux with 32-bit userland
if [ $_ostype = unknown-linux-gnu -a $_cputype = x86_64 ]; then
# $SHELL does not exist in standard 'sh', so probably only exists
# if configure is running in an interactive bash shell. /usr/bin/env
# exists *everywhere*.
local _bin_to_probe="${SHELL-bogus_shell}"
if [ ! -e "$_bin_to_probe" -a -e "/usr/bin/env" ]; then
_bin_to_probe="/usr/bin/env"
fi
if [ -e "$_bin_to_probe" ]; then
file -L "$_bin_to_probe" | grep -q "x86[_-]64"
if [ $? != 0 ]; then
local _cputype=386
fi
fi
fi
local _arch="$_ostype-$_cputype"
verbose_say "architecture is $_arch"
RETVAL="$_arch"
}
get_latest_dry_version() {
verbose_say "getting latest dry version from $version_file_url"
RETVAL="v$(curl $version_file_url)"
}
# Downloads a remote file, returns 0 on success.
# Returns the path to the downloaded file in RETVAL.
download_and_check() {
local _remote_name="$1"
local _quiet="$2"
download_file "$_remote_name" "$dl_dir" "$_quiet"
if [ $? != 0 ]; then
return 1
fi
#TODO Check download
local _download_file="$RETVAL"
assert_nz "$_download_file" "downloaded file"
verbose_say "downloaded dry binary location: $_download_file"
RETVAL="$_download_file"
}
download_file() {
local _remote_name="$1"
local _local_dirname="$2"
local _quiet="$3"
local _remote_basename="$(basename "$_remote_name")"
assert_nz "$_remote_basename" "remote basename"
local _local_name="$_local_dirname/$_remote_basename"
verbose_say "downloading '$_remote_name' to '$_local_name'"
# Invoke curl in a way that will resume if necessary
if [ "$_quiet" = false ]; then
(run cd "$_local_dirname" && run curl -# -L -C - -f -O "$_remote_name")
else
(run cd "$_local_dirname" && run curl -s -L -C - -f -O "$_remote_name")
fi
if [ $? != 0 ]; then
say_err "couldn't download '$_remote_name'"
return 1
fi
RETVAL="$_local_name"
}
# Help
print_help() {
echo '
Usage: dryup.sh [--verbose]
Options:
--prefix=<path> Install to a specific location (default /usr/local/bin)
'
}
# Standard utilities
say() {
echo "dryup: $1"
}
say_err() {
say "$1" >&2
}
verbose_say() {
if [ "$flag_verbose" = true ]; then
say "$1"
fi
}
err() {
say "$1" >&2
exit 1
}
need_cmd() {
if ! command -v "$1" > /dev/null 2>&1
then err "need '$1' (command not found)"
fi
}
need_ok() {
if [ $? != 0 ]; then err "$1"; fi
}
assert_nz() {
if [ -z "$1" ]; then err "assert_nz $2"; fi
}
# Run a command that should never fail. If the command fails execution
# will immediately terminate with an error showing the failing
# command.
ensure() {
"$@"
need_ok "command failed: $*"
}
# This is just for indicating that commands' results are being
# intentionally ignored. Usually, because it's being executed
# as part of error handling.
ignore() {
run "$@"
}
# Runs a command and prints it to stderr if it fails.
run() {
"$@"
local _retval=$?
if [ $_retval != 0 ]; then
say_err "command failed: $*"
fi
return $_retval
}
# Prints the absolute path of a directory to stdout
abs_path() {
local _path="$1"
# Unset CDPATH because it causes havok: it makes the destination unpredictable
# and triggers 'cd' to print the path to stdout. Route `cd`'s output to /dev/null
# for good measure.
(unset CDPATH && cd "$_path" > /dev/null && pwd)
}
assert_cmds() {
need_cmd dirname
need_cmd basename
need_cmd mkdir
need_cmd cat
need_cmd curl
need_cmd mktemp
need_cmd rm
need_cmd egrep
need_cmd grep
need_cmd file
need_cmd uname
need_cmd tar
need_cmd sed
need_cmd sh
need_cmd mv
need_cmd cut
need_cmd sort
need_cmd date
need_cmd head
need_cmd printf
need_cmd touch
need_cmd id
}
main "$@"