-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup-go.sh
149 lines (125 loc) · 2.78 KB
/
setup-go.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
init_global_vars() {
DEFAULT_VERSION=1.19.4
VERSION=$DEFAULT_VERSION
INSTALLATION_BASE_DIR=$HOME/opt
OPTIND=1
}
print_usage() {
cat <<EOM
${0} [-v VERSION]
-v VERSION Version of Go to install.
Default: $DEFAULT_VERSION
EOM
}
set_vars_from_opts() {
while getopts v: opt; do
case $opt in
v) VERSION=$OPTARG
;;
esac
done
}
installation_path() {
# There might be different installation paths, depending on target OS
case "$(uname -s)" in
CYGWIN*|MINGW*|MSYS*)
echo $INSTALLATION_BASE_DIR/go$VERSION/bin
;;
*)
echo $INSTALLATION_BASE_DIR/go$VERSION/bin
;;
esac
}
export_path_vars() {
echo "Adding $(installation_path) to PATH"
SETUP_GO_ORIGINAL_PATH="${PATH}"
export PATH="$(installation_path):${PATH}"
}
reset_path_vars() {
if [ -v SETUP_GO_ORIGINAL_PATH ]; then
export PATH="${SETUP_GO_ORIGINAL_PATH}"
unset SETUP_GO_ORIGINAL_PATH
fi
}
reset_global_vars() {
unset DEFAULT_VERSION
unset VERSION
unset INSTALLATION_BASE_DIR
OPTIND=1
}
abort() {
reset_path_vars
reset_global_vars
return 0
}
is_installed() {
go version 2>/dev/null &&
(go version 2>&1 | grep $VERSION)
}
installation_file() {
case "$(uname -s)" in
CYGWIN*|MINGW*|MSYS*)
echo go$VERSION.windows-amd64.zip
;;
*)
echo go$VERSION.linux-amd64.tar.gz
;;
esac
}
local_installation_file() {
echo /tmp/$(installation_file)
}
local_installation_file_exists() {
test -f $(local_installation_file)
}
download_url() {
case "$(uname -s)" in
CYGWIN*|MINGW*|MSYS*)
echo https://dl.google.com/go/go$VERSION.windows-amd64.zip
;;
*)
echo https://dl.google.com/go/go$VERSION.linux-amd64.tar.gz
;;
esac
}
remote_installation_file_exists() {
curl -sIf $(download_url) >/dev/null
}
download_installation_file() {
echo "Download installation file"
curl $(download_url) -o $(local_installation_file)
}
install_installation_file() {
local trgt_dir=$INSTALLATION_BASE_DIR/go$VERSION
case "$(uname -s)" in
CYGWIN*|MINGW*|MSYS*)
unzip -q $(local_installation_file) -d $INSTALLATION_BASE_DIR
;;
*)
tar zxf $(local_installation_file) -C $INSTALLATION_BASE_DIR
;;
esac
mv $INSTALLATION_BASE_DIR/go $trgt_dir
}
install() {
echo "Install version: $VERSION"
if ! local_installation_file_exists; then
echo "Local installation file not found: $(installation_file). Try, download new one"
if remote_installation_file_exists; then
download_installation_file
else
echo "ERROR: No installation file found. Abort"
abort
fi
fi
install_installation_file
}
init_global_vars
reset_path_vars
set_vars_from_opts ${@}
export_path_vars
if ! is_installed; then
install || abort
fi
go version
reset_global_vars