-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgo-up.sh
executable file
·113 lines (88 loc) · 2.48 KB
/
go-up.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
#!/bin/bash
SUDOCMD=""
WGETCMD="wget -qO-"
CURLCMD="curl -sL"
EXTRACTCMD="tar -xzf - --strip-components=1 -C"
GOBIN=$(which go 2> /dev/null)
if [[ -z "$GOBIN" ]]
then
echo "❌ Go not installed, nothing to update"
exit 1
fi
GOROOT=$($GOBIN env GOROOT 2> /dev/null)
GOPATH=$($GOBIN env GOPATH 2> /dev/null)
if [[ -z "$GOROOT" ]]
then
echo "❌ Could't find \$GOROOT environment variable"
exit 1
fi
GOVERSION=$($GOBIN env GOVERSION 2> /dev/null)
GOVERSIONPRETTY=${GOVERSION#"go"}
if [[ -z "$GOVERSION" ]]
then
echo "❌ Could't find \$GOVERSION environment variable"
exit 1
fi
if [[ -z "$(which wget 2> /dev/null)" ]] && [[ -z "$(which curl 2> /dev/null)" ]]
then
echo "❌ Unable to update go to latest version, please install wget or curl"
exit 1
fi
GOLATESTURL="https://go.dev/VERSION?m=text"
GOLATEST=$($WGETCMD "$GOLATESTURL" 2> /dev/null || $CURLCMD "$GOLATESTURL" 2> /dev/null)
GOLATESTPRETTY=${GOLATEST#"go"}
if [[ -z "$GOLATEST" ]]
then
echo "❌ Could't fetch latest golang version"
exit 1
fi
if [[ "$GOVERSION" == "$GOLATEST" ]]
then
echo "👍 You already have the latest go with version $GOVERSIONPRETTY, no update required"
exit 0
fi
case $(uname) in
Linux) GOROOTOWNER=$(stat -c "%U" $GOROOT 2> /dev/null) ;;
Darwin) GOROOTOWNER=$(stat -f "%Su" $GOROOT 2> /dev/null) ;;
esac
if [[ -z "$GOROOTOWNER" ]]
then
echo "❌ Could't find owner of the \$GOROOT directory"
exit 1
fi
if [[ "$USER" != "$GOROOTOWNER" ]]
then
echo "🔥 Requires "$GOROOTOWNER" user access rights ..."
SUDOCMD="sudo -u $GOROOTOWNER"
$SUDOCMD echo -n ""
fi
GOARCH=$($GOBIN env GOARCH 2> /dev/null)
if [[ -z "$GOARCH" ]]
then
echo "❌ Could't find \$GOARCH environment variable"
exit 1
fi
GOOS=$($GOBIN env GOOS 2> /dev/null)
if [[ -z "$GOOS" ]]
then
echo "❌ Could't find \$GOOS environment variable"
exit 1
fi
GOTARBALL="https://go.dev/dl/$GOLATEST.$GOOS-$GOARCH.tar.gz"
$SUDOCMD rm -rf "$GOROOT"/*
$WGETCMD $GOTARBALL 2> /dev/null | $SUDOCMD $EXTRACTCMD $GOROOT 2> /dev/null
if [[ "$?" != "0" ]]
then
$SUDOCMD rm -rf "$GOROOT"/*
$CURLCMD $GOTARBALL 2> /dev/null | $SUDOCMD $EXTRACTCMD $GOROOT 2> /dev/null
if [[ "$?" != "0" ]]
then
echo "❌ Couldn't download and unpack go with version $GOLATESTPRETTY"
exit 1
fi
fi
if [[ -n "$GOPATH" ]] && [[ "$GOPATH" == "$GOROOT"* ]]
then
$SUDOCMD mkdir -p $GOPATH
fi
echo "👍 Go successfully upgraded from version $GOVERSIONPRETTY to version $GOLATESTPRETTY"