-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathinstall.sh
executable file
·443 lines (362 loc) · 10.1 KB
/
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
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
#!/usr/bin/env bash
# Golang-Install
# Project Home Page:
# https://github.com/jetsung/golang-install
# https://framagit.org/jetsung/golang-install
#
# Author: Jetsung Chan <[email protected]>
set -euo pipefail
exec 3>&1
script_name=$(basename "$0")
script_dir_name="${script_name##*/}"
if [ -t 1 ] && command -v tput >/dev/null; then
ncolors=$(tput colors || echo 0)
if [ -n "$ncolors" ] && [ "$ncolors" -ge 8 ]; then
bold="$(tput bold || echo)"
normal="$(tput sgr0 || echo)"
black="$(tput setaf 0 || echo)"
red="$(tput setaf 1 || echo)"
green="$(tput setaf 2 || echo)"
yellow="$(tput setaf 3 || echo)"
blue="$(tput setaf 4 || echo)"
magenta="$(tput setaf 5 || echo)"
cyan="$(tput setaf 6 || echo)"
white="$(tput setaf 7 || echo)"
fi
fi
say_warning() {
printf "%b\n" "${yellow:-}${script_name}: Warning: $1${normal:-}" >&3
}
say_err() {
printf "%b\n" "${red:-}${script_name}: Error: $1${normal:-}" >&2
exit 1
}
say() {
printf "%b\n" "${cyan:-}${script_name}:${normal:-} $1" >&3
}
# try profile
try_profile() {
if [ -z "${1:-}" ] || [ ! -f "$1" ]; then
return 1
fi
sh_echo "$1"
}
# sh echo
sh_echo() {
command printf %s\\n "$*" 2>/dev/null
}
# Get PROFILE
detect_profile() {
if [ "${PROFILE:-}" = '/dev/null' ]; then
# the user has specifically requested NOT to have nvm touch their profile
return
fi
if [ -n "$PROFILE" ] && [ -f "$PROFILE" ]; then
sh_echo "$PROFILE"
return
fi
local DETECTED_PROFILE
DETECTED_PROFILE=''
if [ -z "${SHELL:-}" ]; then
SHELL="$(grep "^$(whoami):" /etc/passwd | cut -d: -f7)"
fi
if [ "${SHELL#*bash}" != "$SHELL" ]; then
if [ -f "$HOME/.bashrc" ]; then
DETECTED_PROFILE="$HOME/.bashrc"
elif [ -f "$HOME/.bash_profile" ]; then
DETECTED_PROFILE="$HOME/.bash_profile"
fi
elif [ "${SHELL#*zsh}" != "$SHELL" ]; then
if [ -f "$HOME/.zshrc" ]; then
DETECTED_PROFILE="$HOME/.zshrc"
fi
elif [ "${SHELL#*sh}" != "$SHELL" ]; then
if [ -f "$HOME/.profile" ]; then
DETECTED_PROFILE="$HOME/.profile"
fi
fi
if [ -z "$DETECTED_PROFILE" ]; then
for EACH_PROFILE in ".profile" ".bashrc" ".bash_profile" ".zshrc"; do
if DETECTED_PROFILE="$(try_profile "$HOME/$EACH_PROFILE")"; then
break
fi
done
fi
if [ -z "$DETECTED_PROFILE" ]; then
if [ "$(id -u)" -eq 0 ]; then
DETECTED_PROFILE="/etc/profile"
fi
fi
if [ -n "$DETECTED_PROFILE" ]; then
sh_echo "$DETECTED_PROFILE"
fi
}
# fix macos
sedi() {
if [ "$OS" = "darwin" ]; then
sed -i "" "$@"
else
sed -i "$@"
fi
}
# show help message
show_help_message() {
printf "Go install
\e[1;33mUSAGE:\e[m
\e[1;32m%s\e[m [OPTIONS] <SUBCOMMANDS>
\e[1;33mOPTIONS:\e[m
\e[1;32m-h, --help\e[m
Print help information.
\e[1;32m-p, --path\e[m
Set GOPATH. (default: \$HOME/go)
\e[1;32m-r, --root\e[m
Set GOROOT. (default: \$HOME/.go)
\e[1;32m-v, --version\e[m
Set golang version.
\n" "$script_dir_name"
exit
}
# custom version
custom_version() {
if [ -n "$1" ]; then
RELEASE_TAG="go$1"
fi
}
# check in china
check_in_china() {
if ! curl -s -m 3 -IL https://google.com | grep -q "HTTP/2 200"; then
IN_CHINA=1
fi
}
# Get OS bit
init_arch() {
ARCH=$(uname -m)
BIT="$ARCH"
case "$ARCH" in
amd64) ARCH="amd64" ;;
x86_64) ARCH="amd64" ;;
i386) ARCH="386" ;;
armv6l) ARCH="armv6l" ;;
armv7l) ARCH="armv6l" ;;
aarch64) ARCH="arm64" ;;
*)
say_err "Architecture $ARCH is not supported by this installation script\n"
;;
esac
}
# Get OS version
init_os() {
OS=$(uname | tr '[:upper:]' '[:lower:]')
case "$OS" in
darwin) OS='darwin' ;;
linux) OS='linux' ;;
freebsd) OS='freebsd' ;;
# mingw*) OS='windows';;
# msys*) OS='windows';;
*)
say_err "OS $OS is not supported by this installation script\n"
;;
esac
}
# install curl command
install_curl_command() {
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
say_err "You must pre-install the curl tool\n"
fi
fi
}
# if RELEASE_TAG was not provided, assume latest
latest_version() {
if [ -z "$RELEASE_TAG" ]; then
RELEASE_TAG="$(curl -sL --retry 5 --max-time 30 "$RELEASE_URL" | sed -n '/toggleVisible/p' | head -n 1 | cut -d '"' -f 4)"
fi
}
# compare version
compare_version() {
OLD_VERSION="none"
NEW_VERSION="$RELEASE_TAG"
if [ -f "$GOROOT_PATH/bin/go" ]; then
OLD_VERSION=$("$GOROOT_PATH"/bin/go version | awk '{print $3}')
fi
# DELETE current go
# if [[ "$OLD_VERSION"="none" ]]; then
# __CURRENT_GO=$(which go)
# fi
if [ "$OLD_VERSION" = "$NEW_VERSION" ]; then
say_err "You have installed this version: $OLD_VERSION"
fi
printf "
Current version: \e[1;33m %s \e[0m
Target version: \e[1;33m %s \e[0m
" "$OLD_VERSION" "$NEW_VERSION"
}
# create folder
create_folder() {
if [ -n "${1:-}" ]; then
local MYPATH="$1"
local REAL_PATH=${MYPATH/\$HOME/$HOME}
[ -d "$REAL_PATH" ] || mkdir "$REAL_PATH"
__TMP_PATH="$REAL_PATH"
fi
}
# Download file and unpack
download_unpack() {
local downurl="$1"
local savepath="$2"
printf "Fetching %s \n\n" "$downurl"
curl -Lk --connect-timeout 30 --retry 5 --retry-max-time 360 --max-time 300 "$downurl" | gunzip | tar xf - --strip-components=1 -C "$savepath"
}
# compare version size
version_ge() { [[ "$(echo "$@" | tr " " "\n" | sort -rV | head -n 1)" == "$1" ]]; }
# set golang environment
set_environment() {
if ! grep -q 'export\sGOROOT' "$PROFILE"; then
printf "\n## GOLANG\n" >>"$PROFILE"
echo "export GOROOT=\"$__GOROOT\"" >>"$PROFILE"
else
sedi "s@^export GOROOT.*@export GOROOT=\"$__GOROOT\"@" "$PROFILE"
fi
if ! grep -q 'export\sGOPATH' "$PROFILE"; then
echo "export GOPATH=\"$__GOPATH\"" >>"$PROFILE"
else
sedi "s@^export GOPATH.*@export GOPATH=\"$__GOPATH\"@" "$PROFILE"
fi
if ! grep -q 'export\sGOBIN' "$PROFILE"; then
# shellcheck disable=SC2016
echo 'export GOBIN="$GOPATH/bin"' >>"$PROFILE"
else
sedi "s@^export GOBIN.*@export GOBIN=\$GOPATH/bin@" "$PROFILE"
fi
if ! grep -q 'export\sGO111MODULE' "$PROFILE"; then
echo "export GO111MODULE=on" >>"$PROFILE"
fi
if ! grep -q 'export\sASSUME_NO_MOVING_GC_UNSAFE_RISK_IT_WITH' "$PROFILE"; then
if version_ge "$RELEASE_TAG" "go1.17"; then
echo "export ASSUME_NO_MOVING_GC_UNSAFE_RISK_IT_WITH=go1.18" >>"$PROFILE"
fi
fi
if ! grep -q 'export\sGOPROXY' "$PROFILE"; then
echo "export GOPROXY=\"$__GOPROXY_URL,direct\"" >>"$PROFILE"
else
sedi "s@^export GOPROXY.*@export GOPROXY=\"$__GOPROXY_URL,direct\"@" "$PROFILE"
fi
# shellcheck disable=SC2016
if ! grep -q '$GOROOT/bin:$GOBIN' "$PROFILE"; then
# shellcheck disable=SC2016
echo 'export PATH="$PATH:$GOROOT/bin:$GOBIN"' >>"$PROFILE"
fi
}
# show copyright
show_copyright() {
clear
printf "
###############################################################
### Golang Install
###
### Author: Jetsung Chan <[email protected]>
### Link: https://jetsung.com
### Project: %s
###############################################################
\n" "$PROJECT_URL"
}
# show system information
show_system_information() {
printf "
###############################################################
### System: %s
### Bit: %s
### Version: %s
###############################################################
\n" "$OS" "$BIT" "$RELEASE_TAG"
}
# Show success message
show_success_message() {
printf "
###############################################################
# Install success, please execute again \e[1;33msource %s\e[0m
###############################################################
\n" "$PROFILE"
}
# Downlaod URL
DOWNLOAD_URL="https://dl.google.com/go/"
# Release URL
RELEASE_URL="https://go.dev/dl/"
RELEASE_CN_URL="https://golang.google.cn/dl/"
# GOPROXY
__GOPROXY_URL="https://proxy.golang.org"
__GOPROXY_CN_URL="https://goproxy.cn,https://goproxy.io"
# GOPATH
# shellcheck disable=SC2016
__GOPATH='$HOME/go'
# GOROOT
# shellcheck disable=SC2016
__GOROOT='$HOME/.go'
# Project URL
PROJECT_URL="https://github.com/jetsung/golang-install"
PROJECT_CN_URL="https://framagit.org/jetsung/golang-install"
# Profile
PROFILE=""
PROFILE="$(detect_profile)"
if [ -z "$PROFILE" ]; then
say_err "Error: can not find profile"
fi
# Release tag
RELEASE_TAG=""
for ARG in "$@"; do
case "$ARG" in
--help | -h)
show_help_message
;;
--path | -p)
shift
if [ $# -ge 1 ] && [[ "$1" != -* ]]; then
__GOPATH=${1/"$HOME"/\$HOME}
fi
;;
--root | -r)
shift
if [ $# -ge 1 ] && [[ "$1" != -* ]]; then
__GOROOT=${1/"$HOME"/\$HOME}
fi
;;
--version | -v)
shift
if [ $# -ge 1 ] && [[ "$1" != -* ]]; then
custom_version "$1"
fi
;;
*)
shift
;;
esac
done
create_folder "$__GOPATH"
__TMP_PATH=""
create_folder "$__GOROOT"
GOROOT_PATH="$__TMP_PATH"
IN_CHINA=""
check_in_china
if [ -n "$IN_CHINA" ]; then
RELEASE_URL="$RELEASE_CN_URL"
PROJECT_URL="$PROJECT_CN_URL"
__GOPROXY_URL="$__GOPROXY_CN_URL"
fi
show_copyright
init_arch
init_os
install_curl_command
latest_version
compare_version
show_system_information
# Download File
BINARY_URL="$DOWNLOAD_URL$RELEASE_TAG.$OS-$ARCH.tar.gz"
# Download and unpack
download_unpack "$BINARY_URL" "$GOROOT_PATH"
# Set ENV
set_environment
show_success_message