-
Notifications
You must be signed in to change notification settings - Fork 0
/
golang_install.sh
executable file
·305 lines (250 loc) · 6.87 KB
/
golang_install.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
lang-Install
# Project Home Page:
# https://github.com/skiy/golang-install
#
# Author: Skiychan <dev@skiy.net>
# Link: https://www.skiy.net
# Script file name
SCRIPT_NAME=$0
# Release link
RELEASE_URL="https://golang.org/dl/"
# Downlaod link
DOWNLOAD_URL="https://dl.google.com/go/"
# DOWNLOAD_URL="http://127.0.0.1/"
# GOPROXY
GOPROXY_TEXT="https://proxy.golang.org,https://athens.azurefd.net"
# Set environmental for golang
PROFILE="/etc/profile"
# Set GOPATH PATH
GO_PATH="/data/go"
# Is GWF
IN_CHINA=0
# Check if user is root
checkRoot() {
ROOT=$(id -u)
case $ROOT in
0) ROOT='root';;
*) printf "\e[1;31mError: You must be root to run this script\e[0m\n"; exit 1;;
esac
}
# Chenck GWF
checkGWF() {
urlstatus=$(curl -s -m 5 -IL $RELEASE_URL | grep 200)
if [ "$urlstatus" == "" ]; then
IN_CHINA=1
RELEASE_URL="https://golang.google.cn/dl/"
GOPROXY_TEXT="https://goproxy.cn,https://goproxy.io"
fi
}
# Get OS bit
initArch() {
ARCH=$(uname -m)
BIT=$ARCH
case $ARCH in
amd64) ARCH="amd64";;
x86_64) ARCH="amd64";;
i386) ARCH="386";;
armv6l) ARCH="armv6l";;
armv7l) ARCH="armv6l";;
*) printf "\e[1;31mArchitecture %s is not supported by this installation script\e[0m\n" $ARCH; exit 1;;
esac
echo "ARCH = ${ARCH}"
}
# Get OS version
initOS() {
OS=$(uname | tr '[:upper:]' '[:lower:]')
case $OS in
darwin) OS='darwin';;
linux) OS='linux';;
freebsd) OS='freebsd';;
# mingw*) OS='windows';;
# msys*) OS='windows';;
*) printf "\e[1;31mOS %s is not supported by this installation script\e[0m\n" $OS; exit 1;;
esac
echo "OS = ${OS}"
}
initArgs() {
key=""
for arg in "$@"
do
if test "-h" = $arg; then
showHelpMessage
fi
if test -z $key; then
key=$arg
else
if test "-v" = $key; then
customVersion $arg
elif test "-d" = $key; then
GO_PATH=$arg
createGOPATHFolder
fi
key=""
fi
done
}
# DIY version
customVersion() {
if [ -n "${1}" ] ;then
RELEASE_TAG="go${1}"
echo "Custom Version = ${RELEASE_TAG}"
fi
}
# if RELEASE_TAG was not provided, assume latest
latestVersion() {
if [ -z "${RELEASE_TAG}" ]; then
RELEASE_TAG="$(curl -sL ${RELEASE_URL} | sed -n '/toggleVisible/p' | head -n 1 | cut -d '"' -f 4)"
echo "Latest Version = ${RELEASE_TAG}"
fi
}
# Compare version
compareVersion() {
OLD_VERSION="none"
NEW_VERSION="${RELEASE_TAG}"
if test -x "$(command -v go)"; then
OLD_VERSION="$(go version | awk '{print $3}')"
fi
if [ "$OLD_VERSION" = "$NEW_VERSION" ]; then
printf "\n\e[1;31mYou have installed this version: %s\e[0m\n" $OLD_VERSION; exit 1;
fi
printf "
Current version: \e[1;33m %s \e[0m
Target version: \e[1;33m %s \e[0m
" $OLD_VERSION $NEW_VERSION
}
# Compare version size
versionGE() { test "$(echo "$@" | tr " " "\n" | sort -rV | head -n 1)" == "$1"; }
# Install curl command
installCURLCommand() {
if !(test -x "$(command -v curl)"); then
if test -x "$(command -v yum)"; then
yum install -y curl
elif test -x "$(command -v apt)"; then
apt install -y curl
else
printf "\e[1;31mYou must pre-install the curl tool\e[0m\n"
exit 1
fi
fi
}
# Download go file
downloadFile() {
url="${1}"
destination="${2}"
printf "Fetching ${url} \n\n"
if test -x "$(command -v curl)"; then
code=$(curl --connect-timeout 15 -w '%{http_code}' -L "${url}" -o "${destination}")
elif test -x "$(command -v wget)"; then
code=$(wget -t2 -T15 -O "${destination}" --server-response "${url}" 2>&1 | awk '/^ HTTP/{print $2}' | tail -1)
else
printf "\e[1;31mNeither curl nor wget was available to perform http requests.\e[0m\n"
exit 1
fi
if [ "${code}" != 200 ]; then
printf "\e[1;31mRequest failed with code %s\e[0m\n" $code
exit 1
else
printf "\n\e[1;33mDownload succeeded\e[0m\n"
fi
}
# Set golang environment
setEnvironment() {
profile="${1}"
if [ -z "`grep 'export\sGOROOT' ${profile}`" ];then
echo -e "\n## GOLANG" >> $profile
echo "export GOROOT=/usr/local/go" >> $profile
fi
if [ -z "`grep 'export\sGOPATH' ${profile}`" ];then
echo "export GOPATH=${GO_PATH}" >> $profile
fi
if [ -z "`grep 'export\sGOBIN' ${profile}`" ];then
echo "export GOBIN=\$GOPATH/bin" >> $profile
fi
if [ "${IN_CHINA}" == "1" ]; then
if [ -z "`grep 'export\sGOSUMDB' ${profile}`" ];then
echo "export GOSUMDB=off" >> $profile
fi
fi
if [ -z "`grep 'export\sGOPROXY' ${profile}`" ];then
if versionGE $RELEASE_TAG "go1.13"; then
GOPROXY_TEXT="${GOPROXY_TEXT},direct"
fi
echo "export GOPROXY=${GOPROXY_TEXT}" >> $profile
fi
if [ -z "`grep '\$GOROOT/bin:\$GOBIN' ${profile}`" ];then
echo "export PATH=\$GOROOT/bin:\$GOBIN:\$PATH" >> $profile
fi
}
# Create GOPATH folder
createGOPATHFolder() {
mkdir -p $GO_PATH
}
# Show copyright
showCopyright() {
clear
printf "
###############################################################
### Golang Install
###
### Author: Skiychan<dev@skiy.net>
### Link: https://www.skiy.net
### Project: https://github.com/skiy/golang-install
###############################################################
\n"
}
# Show system information
showSystemInformation() {
printf "
###############################################################
### System: %s
### Bit: %s
### Version: %s
###############################################################
\n" $OS $BIT $RELEASE_TAG
}
# Show success message
showSuccessMessage() {
printf "
###############################################################
# Install success, please execute again \e[1;33msource %s\e[0m
###############################################################
\n" $PROFILE
}
# Show help message
showHelpMessage() {
printf "
Go install
Usage: %s [-h] [-v version] [-d gopath]
Options:
-h : this help
-v : set go version (default: latest version)
-d : set go path (default: /data/go)
\n" $SCRIPT_NAME
exit 1
}
# identify platform based on uname output
initArgs $@
showCopyright
checkRoot
checkGWF
set -e
initArch
initOS
installCURLCommand
latestVersion
compareVersion
showSystemInformation
# Download File
BINARY_URL="${DOWNLOAD_URL}${RELEASE_TAG}.${OS}-${ARCH}.tar.gz"
DOWNLOAD_FILE="$(mktemp).tar.gz"
downloadFile $BINARY_URL $DOWNLOAD_FILE
# Tar file and move file
rm -rf /usr/local/go
tar -C /usr/local/ -zxf $DOWNLOAD_FILE && \
rm -rf $DOWNLOAD_FILE
setEnvironment $PROFILE
# Make environmental is enable
. $PROFILE
go env
go version
showSuccessMessage