forked from udhos/update-golang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update-golang.sh
executable file
·443 lines (377 loc) · 9.74 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
#!/bin/bash
#
# update-golang is a script to easily fetch and install new Golang releases
#
# Home: https://github.com/udhos/update-golang
#
# PIPETHIS_AUTHOR udhos
version=0.18
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://golang.org/doc/devel/release.html
source=https://storage.googleapis.com/golang
destination=/usr/local
release=1.11.2 ;# just the default. the script detects the latest available release.
profiled=/etc/profile.d/golang_path.sh
arch_probe="uname -m"
os=$(uname -s | tr "[:upper:]" "[:lower:]")
[ -n "$ARCH_PROBE" ] && arch_probe="$ARCH_PROBE"
arch=$($arch_probe)
case "$arch" in
i*)
arch=386
;;
x*)
arch=amd64
;;
aarch64)
arch=armv6l
;;
armv7l)
arch=armv6l
;;
esac
show_version() {
msg version $version
}
show_version
scan_versions() {
local fetch="$*"
debug scan_versions: from "$release_list"
$fetch "$release_list" | grep -E -o 'go[0-9\.]+' | grep -E -o '[0-9]\.[0-9]+(\.[0-9]+)?' | sort -V | uniq
}
find_latest() {
debug find_latest: from "$release_list"
local last=
local fetch=
if hash wget 2>/dev/null; then
fetch="wget -qO-"
else
fetch="curl --silent"
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
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
[ -n "$PROFILED" ] && profiled=$PROFILED
cache=$destination
[ -n "$CACHE" ] && cache=$CACHE
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
EOF
}
label=go$release.$os-$arch
filename=$label.tar.gz
url=$source/$filename
goroot=$destination/go
filepath=$cache/$filename
new_install=$destination/$label
tmp='' ;# will be set
save_dir=$PWD
previous_install='' ;# will be set
cleanup() {
[ -n "$tmp" ] && [ -f "$tmp" ] && msg cleanup: $tmp && rm $tmp
[ -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
}
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 hash wget 2>/dev/null; then
wget -O "$abs_filepath" "$url" || die could not download using wget from: "$url"
[ -f "$abs_filepath" ] || die missing file downloaded with wget: "$abs_filepath"
else
curl -o "$abs_filepath" "$url" || die could not download using curl from: "$url"
[ -f "$abs_filepath" ] || die missing file downloaded with curl: "$abs_filepath"
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"
}
relink() {
mv "$abs_goroot" "$abs_new_install"
ln -s "$abs_new_install" "$abs_goroot"
}
path_mark=update-golang.sh
path_remove() {
if [ -f "$abs_profiled" ]; then
msg path: removing old settings from: "$abs_profiled"
tmp=$(mktemp -t) # save for later removal
if [ ! -f "$tmp" ]; then
msg path: could not create temporary file: "$tmp"
return
fi
grep -v "$path_mark" "$abs_profiled" > "$tmp"
cp "$tmp" "$abs_profiled"
fi
}
default_goroot=/usr/local/go
path() {
path_remove
msg path: issuing new "$abs_gobin" to "$abs_profiled"
local dont_edit=";# DOT NOT EDIT: installed by $path_mark"
echo "export PATH=\$PATH:$abs_gobin $dont_edit" >> "$abs_profiled"
local user_gobin=
[ -n "$GOPATH" ] && user_gobin=$(echo "$GOPATH" | awk -F: '{print $1}')/bin
[ -z "$user_gobin" ] && user_gobin=$HOME/go/bin
msg path: issuing "$user_gobin" to "$abs_profiled"
echo "export PATH=\$PATH:$user_gobin $dont_edit" >> "$abs_profiled"
if [ "$abs_goroot" != $default_goroot ]; then
msg path: setting up custom GOROOT="$abs_goroot" to "$abs_profiled"
echo "export GOROOT=$abs_goroot $dont_edit" >> "$abs_profiled"
fi
}
running_as_root() {
[ "$EUID" -eq 0 ]
}
perm_build_cache() {
local gocache
gocache=$("$abs_gotool" env | grep GOCACHE) ;# grab GOCACHE=path
local buildcache
buildcache=$(echo "$gocache" | awk -F= '{ print $2 }') ;# grab path
buildcache=$(eval echo "$buildcache") ;# unquote
local own
own=":"
if running_as_root; then
# running as root - try user id from sudo
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"
}
test() {
local ret=1
local t="$abs_gotool version"
if [ "$abs_goroot" != $default_goroot ]; then
msg testing: GOROOT="$abs_goroot" "$t"
GOROOT=$abs_goroot $t | log_stdin
ret=$?
else
msg testing: "$t"
$t | log_stdin
ret=$?
fi
if [ $ret -eq 0 ]; then
msg "$t": SUCCESS
else
msg "$t" FAIL
fi
local hello=
hello=$(mktemp -t hello-tmpXXXXXXXX.go)
cat >"$hello" <<__EOF__
package main
import "fmt"
func main() {
fmt.Printf("hello, world\n")
}
__EOF__
local abs_hello=
abs_hello=$(solve "$hello")
ret=1
t="$abs_gotool run $abs_hello"
if [ "$abs_goroot" != $default_goroot ]; then
msg testing: GOROOT="$abs_goroot" "$t"
GOROOT=$abs_goroot $t | log_stdin
ret=$?
else
msg testing: "$t"
$t | log_stdin
ret=$?
fi
if [ $ret -eq 0 ]; then
msg "$t": SUCCESS
else
msg "$t" FAIL
fi
rm "$hello"
}
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
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 hash dpkg 2>/dev/null && dpkg -s golang-go 2>/dev/null | grep ^Status | grep -q installed; then
msg warning: golang-go is installed, you should remove it: sudo apt remove golang-go
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
;;
'')
;;
*)
msg unknown option: "$1"
echo >&2 usage: "$me [-v] [remove]"
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
path
msg golang "$label" installed at: "$abs_goroot"
test
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
exit 0
#
# main section: end
#