-
Notifications
You must be signed in to change notification settings - Fork 229
/
update-golang.sh
executable file
·566 lines (489 loc) · 14 KB
/
update-golang.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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
#!/usr/bin/env bash
#
# update-golang is a script to easily fetch and install new Golang releases
#
# Home: https://github.com/udhos/update-golang
#
# PIPETHIS_AUTHOR udhos
# ignore runtime environment variables
# shellcheck disable=SC2153
version=0.28
set -o pipefail
me=$(basename "$0")
msg() {
echo >&2 "$me": "$*"
}
debug() {
[ -n "$DEBUG" ] && msg debug: "$*"
}
log_stdin() {
while read -r i; do
msg "$i"
done
}
# defaults
release_list=https://go.dev/dl/
source=https://storage.googleapis.com/golang
#source=https://dl.google.com/go
#source=https://go.dev/dl
destination=/usr/local
release=1.22.4 ;# just the default. the script detects the latest available release.
arch_probe="uname -m"
connect_timeout=5
os=$(uname -s | tr "[:upper:]" "[:lower:]")
if [ -d /etc/profile.d ]; then
profiled=/etc/profile.d/golang_path.sh
else
profiled=/etc/profile
fi
[ -n "$ARCH_PROBE" ] && arch_probe="$ARCH_PROBE"
arch=$($arch_probe)
case "$arch" in
i*)
arch=386
;;
x*)
arch=amd64
;;
aarch64)
#arch=armv6l
arch=arm64
;;
armv7l)
# Go project does not provide a binary release for armv71
msg armv7l is not supported, using armv6l
arch=armv6l
;;
esac
show_version() {
msg version $version
}
show_version
# avoid trying 1.12beta because 1.12beta1 is valid while 1.12beta is not
# if you want beta, force RELEASE=1.12beta1
exclude_beta() {
grep -v -E 'go[0-9\.]+(beta|rc)'
}
scan_versions() {
local fetch="$*"
debug scan_versions: from "$release_list"
if has_cmd jq; then
local rl="$release_list?mode=json"
msg "scan_versions: fetch: $fetch $rl"
msg "scan_versions: parsing with jq from $rl"
$fetch "$rl" | jq -r '.[].files[].version' | sort | uniq | exclude_beta | sed -e 's/go//' | sort -V
else
$fetch "$release_list" | exclude_beta | grep -E -o 'go[0-9\.]+' | grep -E -o '[0-9]\.[0-9]+(\.[0-9]+)?' | sort -V | uniq
fi
}
has_cmd() {
#command -v "$1" >/dev/null
hash "$1" 2>/dev/null
}
has_wget() {
[ -z "$SKIP_WGET" ] && has_cmd wget
}
has_curl() {
has_cmd curl
}
tmp='' ;# will be set
save_dir=$PWD
previous_install='' ;# will be set
declutter='' ;# will be set
tar_to_remove='' ;# will be set
cleanup() {
[ -n "$tmp" ] && [ -f "$tmp" ] && msg cleanup: "$tmp" && rm "$tmp"
[ -n "$declutter" ] && [ -n "$tar_to_remove" ] && [ -f "$tar_to_remove" ] && msg cleanup: "$tar_to_remove" && rm "$tar_to_remove"
[ -n "$save_dir" ] && cd "$save_dir" || exit 2
[ -n "$previous_install" ] && msg remember to delete previous install saved as: "$previous_install"
}
die() {
msg "die: $*"
cleanup
exit 3
}
wget_base() {
echo wget --connect-timeout "$connect_timeout" "$FORCE_IPV4"
}
curl_base() {
echo curl --connect-timeout "$connect_timeout" "$FORCE_IPV4"
}
find_latest() {
debug find_latest: built-in version: "$release"
debug find_latest: from "$release_list"
local last=
local fetch=
if has_wget; then
fetch="$(wget_base) -qO-"
elif has_curl; then
fetch="$(curl_base) --silent"
else
die "find_latest: missing both 'wget' and 'curl'"
fi
last=$(scan_versions "$fetch" | tail -1)
if echo "$last" | grep -q -E '[0-9]\.[0-9]+(\.[0-9]+)?'; then
msg find_latest: found last release: "$last"
release=$last
else
msg find_latest: FAILED
fi
}
[ -n "$RELEASE_LIST" ] && release_list=$RELEASE_LIST
if [ -n "$RELEASE" ]; then
msg release forced to RELEASE="$RELEASE"
release="$RELEASE"
else
find_latest
fi
[ -n "$SOURCE" ] && source=$SOURCE
[ -n "$DESTINATION" ] && destination=$DESTINATION
[ -n "$OS" ] && os=$OS
[ -n "$ARCH" ] && arch=$ARCH
cache=$destination
[ -n "$CACHE" ] && cache=$CACHE
[ -n "$PROFILED" ] && profiled=$PROFILED
[ -n "$CONNECT_TIMEOUT" ] && connect_timeout=$CONNECT_TIMEOUT
show_vars() {
echo user: "$(id)"
cat <<EOF
RELEASE_LIST=$release_list
SOURCE=$source
DESTINATION=$destination
RELEASE=$release
OS=$os
ARCH_PROBE=$arch_probe
ARCH=$arch
PROFILED=$profiled
CACHE=$cache
GOPATH=$GOPATH
DEBUG=$DEBUG
FORCE_IPV4=$FORCE_IPV4 ;# set FORCE_IPV4=-4 to force IPv4
CONNECT_TIMEOUT=$connect_timeout
SKIP_WGET=$SKIP_WGET ;# set SKIP_WGET=1 to skip wget
EOF
}
label=go$release.$os-$arch
filename=$label.tar.gz
url=$source/$filename
goroot=$destination/go
filepath=$cache/$filename
new_install=$destination/$label
solve() {
local path=$1
local p=
if echo "$path" | grep -E -q ^/; then
p="$path"
local m=
m=$(file "$p")
debug "solve: $p: $m"
else
p="$save_dir/$path"
fi
echo "$p"
}
abs_filepath=$(solve "$filepath")
abs_url=$(solve "$url")
abs_goroot=$(solve "$goroot")
abs_new_install=$(solve "$new_install")
abs_gobin=$abs_goroot/bin
abs_gotool=$abs_gobin/go
abs_profiled=$(solve "$profiled")
download() {
if echo "$url" | grep -E -q '^https?:'; then
msg "$url" is remote
if [ -f "$abs_filepath" ]; then
msg no need to download - file cached: "$abs_filepath"
else
if has_wget; then
msg download: "$(wget_base)" -O "$abs_filepath" "$url"
$(wget_base) -O "$abs_filepath" "$url" || die could not download using wget from: "$url"
[ -f "$abs_filepath" ] || die missing file downloaded with wget: "$abs_filepath"
elif has_curl; then
msg download: "$(curl_base)" -o "$abs_filepath" "$url"
$(curl_base) -o "$abs_filepath" "$url" || die could not download using curl from: "$url"
[ -f "$abs_filepath" ] || die missing file downloaded with curl: "$abs_filepath"
else
die "download: missing both 'wget' and 'curl'"
fi
fi
else
msg "$abs_url" is local
cp "$abs_url" . || die could not copy from: "$abs_url"
fi
}
symlink_test() {
#file "$1" | grep -q symbolic
readlink "$1" >/dev/null
}
symlink_get() {
#local f=
#local j=
#f=$(file "$1")
#j=$(echo "$f" | awk '{print $NF}')
#debug "symlink_get: $1: [$f]: [$j]"
#echo "$j"
readlink "$1"
}
remove_old_link() {
if symlink_test "$abs_goroot"; then
abs_old_install=$(symlink_get "$abs_goroot")
msg remove_old_link: found symlink for old install: "$abs_old_install"
[ -r "$abs_goroot" ] && rm "$abs_goroot"
else
msg remove_old_link: not found symlink for old install
if [ -r "$abs_goroot" ]; then
local now
now=$(date +%Y%m%d-%H%M%S)
mv "$abs_goroot" "$abs_goroot-$now" || die could not rename existing goland directory: "$abs_goroot"
previous_install="$abs_goroot-$now"
msg previous install renamed to: "$previous_install"
fi
fi
[ -r "$abs_goroot" ] && die could not remove existing golang directory: "$abs_goroot"
}
rm_dir() {
local dir=$1
rm -r "$dir"
}
untar() {
if [ -d "$abs_new_install" ]; then
msg untar: rm_dir "$abs_new_install"
rm_dir "$abs_new_install" || die untar: could not remove: "$abs_new_install"
fi
[ -d "$PWD" ] || die untar: not a directory: "$PWD"
[ -w "$PWD" ] || die untar: unable to write: "$PWD"
local cmd="tar -x -f $abs_filepath"
msg untar: "$cmd"
$cmd || die untar: failed: "$abs_filepath"
tar_to_remove="$abs_filepath"
}
relink() {
mv "$abs_goroot" "$abs_new_install"
ln -s "$abs_new_install" "$abs_goroot"
}
path_mark=update-golang.sh
profile_path_remove() {
if [ -f "$abs_profiled" ]; then
msg profile_path_remove: removing old settings from: "$abs_profiled"
tmp=$(mktemp -t profile-tmpXXXXXXXX) # save for later removal
if [ ! -f "$tmp" ]; then
msg profile_path_remove: could not create temporary file: "$tmp"
return
fi
sed "/# DO NOT EDIT: installed by $path_mark/,/# $path_mark: end/d" "$abs_profiled" > "$tmp"
cp "$tmp" "$abs_profiled"
fi
}
default_goroot=/usr/local/go
profile_path_add() {
profile_path_remove
{ echo; echo "# DO NOT EDIT: installed by $path_mark"; echo ""; } >> "$abs_profiled"
msg profile_path_add: issuing new "$abs_gobin" to "$abs_profiled"
{ echo 'if ! echo "$PATH" | grep -Eq "(^|:)'"$abs_gobin"'($|:)"';
echo "then";
echo " export PATH=$abs_gobin:\$PATH";
echo "fi"; } >> "$abs_profiled"
local user_gobin=
[ -n "$GOPATH" ] && user_gobin=$(echo "$GOPATH" | awk -F: '{print $1}')/bin
# shellcheck disable=SC2016
[ -z "$user_gobin" ] && user_gobin='$HOME/go/bin' ;# we want $HOME literal
msg profile_path_add: issuing "$user_gobin" to "$abs_profiled"
{ echo 'if ! echo "$PATH" | grep -Eq "(^|:)'"$user_gobin"'($|:)"';
echo "then";
echo " export PATH=\$PATH:$user_gobin";
echo "fi"; } >> "$abs_profiled"
if [ "$abs_goroot" != $default_goroot ]; then
msg profile_path_add: setting up custom GOROOT="$abs_goroot" to "$abs_profiled"
echo "export GOROOT=$abs_goroot" >> "$abs_profiled"
fi
echo "# $path_mark: end" >> "$abs_profiled"
chmod 755 "$abs_profiled"
}
running_as_root() {
[ "$EUID" -eq 0 ]
}
perm_build_cache() {
local buildcache
buildcache=$($abs_gotool env GOCACHE)
local own
own=":"
if running_as_root; then
# running as root - try user id from sudo
buildcache=$(sudo -i -u "$SUDO_USER" "$abs_gotool" env GOCACHE)
own="$SUDO_UID:$SUDO_GID"
fi
if [ "$own" == ":" ]; then
# try getting the usual user id
own=$(id -u):$(id -g)
fi
msg recursively forcing build cache ["$buildcache"] ownership to "$own"
chown -R "$own" "$buildcache"
}
unsudo() {
if running_as_root; then
# shellcheck disable=SC2068
msg unsudo: running_as_root:"$SUDO_USER": $@
# shellcheck disable=SC2068
sudo -i -u "$SUDO_USER" $@
else
# shellcheck disable=SC2068
msg unsudo: non_root: $@
# shellcheck disable=SC2068
$@
fi
}
test_runhello() {
local ret=1
local t="$abs_gotool version"
if [ "$abs_goroot" != $default_goroot ]; then
msg testing: GOROOT="$abs_goroot" "$t"
# shellcheck disable=SC2086
GOROOT=$abs_goroot unsudo $t | log_stdin
ret=$?
else
msg testing: "$t"
# shellcheck disable=SC2086
unsudo $t | log_stdin
ret=$?
fi
if [ $ret -eq 0 ]; then
msg "$t": SUCCESS
else
msg "$t" FAIL
fi
local hello_tmp=
hello_tmp=$(unsudo mktemp -t hello-tmpXXXXXXXX)".go"
unsudo tee "$hello_tmp" >/dev/null <<__EOF__
package main
import (
"fmt"
"runtime"
)
func main() {
fmt.Printf("hello, world - %s\n", runtime.Version())
}
__EOF__
local abs_hello=
abs_hello=$(solve "$hello_tmp")
ret=1
t="$abs_gotool run $abs_hello"
if [ "$abs_goroot" != $default_goroot ]; then
msg testing: GOROOT="$abs_goroot" "$t"
# shellcheck disable=SC2086
GOROOT=$abs_goroot unsudo $t | log_stdin
ret=$?
else
msg testing: "$t"
# shellcheck disable=SC2086
unsudo $t | log_stdin
ret=$?
fi
if [ $ret -eq 0 ]; then
msg "$t": SUCCESS
else
msg "$t" FAIL
fi
rm "$hello_tmp"
}
remove_golang() {
if symlink_test "$abs_goroot"; then
local old_install=
old_install=$(symlink_get "$abs_goroot")
msg remove: found symlink for old install: "$old_install"
msg remove: removing symlink: "$abs_goroot"
rm "$abs_goroot"
msg remove: removing dir: "$old_install"
rm_dir "$old_install"
else
msg remove: not found symlink for old install
fi
profile_path_remove
}
remove_old_install() {
if [ -n "$abs_old_install" ]; then
if [ "$abs_old_install" != "$abs_new_install" ]; then
# remove old install only if it actually changed
msg removing old install: "$abs_old_install"
rm_dir "$abs_old_install"
fi
fi
}
check_package() {
if has_cmd dpkg && dpkg -s golang-go 2>/dev/null | grep ^Status | grep -q installed; then
msg
msg WARNING
msg WARNING: golang-go is installed, you should remove it: sudo apt remove golang-go
msg WARNING
msg
fi
if has_cmd rpm && rpm -q golang >/dev/null 2>/dev/null; then
msg
msg WARNING
msg WARNING: golang is installed, you should remove it: sudo yum remove golang
msg WARNING
msg
fi
if unsudo hash brew 2>/dev/null && unsudo brew ls --versions golang >/dev/null; then
msg
msg WARNING
msg WARNING: golang is installed, you should remove it: brew remove golang
msg WARNING
msg
fi
}
# update pre-commit hook
[ -d .git ] && [ ! -h .git/hooks/pre-commit ] && ln -s ../../pre-commit .git/hooks/pre-commit
#
# main section: begin
#
[ -d "$abs_profiled" ] && die "PROFILED=$profiled cannot be a directory"
case "$1" in
-v)
show_version
exit 0
;;
remove)
remove_golang
exit 0
;;
-declutter)
declutter="true"
;;
'')
;;
*)
msg unknown option: "$1"
echo >&2 usage: "$me [-v] [remove] [-declutter]"
exit 1
;;
esac
show_vars | log_stdin
check_package
cd "$destination" || die could not enter destination="$destination"
msg will install golang "$label" as: "$abs_goroot"
download
remove_old_link
untar
relink
remove_old_install
profile_path_add
msg golang "$label" installed at: "$abs_goroot"
test_runhello
if running_as_root; then
msg running_as_root: yes
perm_build_cache ;# must come after test, since testing might create root:root files
else
msg running_as_root: no
fi
cleanup
msg
msg "HINT: If this is the first time you run this script, the env vars"
msg " updated in the profile will only take effect for new shells."
msg " If you want them to affect the current shell, use this:"
msg
msg " source $abs_profiled"
msg
exit 0
#
# main section: end
#