-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathgvm.sh
executable file
·673 lines (548 loc) · 16.1 KB
/
gvm.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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
#!/usr/bin/env bash
# Golang Version Manager
# Project Home Page:
# https://github.com/jetsung/golang-install
# https://framagit.org/jetsung/golang-install
#
# Author: Jetsung Chan <jetsungchan@gmail.com>
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() {
# shellcheck disable=SC2317
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
}
# 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 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
}
# set gvm environment
set_environment() {
cat > "$GVM_ENV_PATH" <<-'EOF'
#!/usr/bin/env bash
export GVMPATH="$HOME/.gvm"
export PATH="$PATH:$GVMPATH/bin"
__MY_PATHS=""
# Remove duplicate paths and remove go is not gvm version
# export PATH=$(echo $PATH | sed 's/:/\n/g' | sort | uniq | tr -s '\n' ':' | sed 's/:$//g')
while IFS=$'\n' read -r -d ' ' _V; do
if command -v "$_V/go" >/dev/null 2>&1 && [[ -f "$_V/go" ]]; then
[[ "$_V" == "$GVMPATH/go/bin" ]] || continue
fi
__MY_PATHS="$__MY_PATHS:$_V"
done < <(echo "$PATH" | sed 's/:/\n/g' | sort | uniq | tr -s '\n' ' ')
[[ -z "$__MY_PATHS" ]] || export PATH="${__MY_PATHS#*:}"
EOF
if [ ! -f "$PROFILE" ]; then
touch "$PROFILE"
fi
# shellcheck disable=SC2016
if ! grep -q '$HOME/.gvm/env' "$PROFILE"; then
printf "\n## GVM\n" >>"$PROFILE"
echo '. "$HOME/.gvm/env"' >>"$PROFILE"
else
sedi 's@^. "$HOME/.gvm/.*@. "$HOME/.gvm/env"@' "$PROFILE"
fi
# Remove duplicate paths
# export PATH=$(echo $PATH | sed 's/:/\n/g' | sort | uniq | tr -s '\n' ':' | sed 's/:$//g')
}
# update gvm.sh
gvm_script() {
[ -d "$GVM_BIN_PATH" ] || mkdir -p "$GVM_BIN_PATH"
CURRENT_GVM_PATH="$(pwd)/gvm"
local GVM_SCRIPT_PATH="$GVM_BIN_PATH/gvm"
# locally update
if [ -f "$CURRENT_GVM_PATH.sh" ]; then
cp "$CURRENT_GVM_PATH.sh" "$GVM_SCRIPT_PATH"
else # git update
check_in_china
[ -z "$IN_CHINA" ] || PRO_URL="$PRO_CN_URL"
curl -sSL -m 5 -o "$GVM_SCRIPT_PATH" "$PRO_URL/gvm.sh"
fi
if [ ! -f "$GVM_SCRIPT_PATH" ]; then
say_err "GVM(gvm.sh) download failed\n"
fi
chmod +x "$GVM_SCRIPT_PATH"
}
# download go install.sh
dl_goinstall_script() {
# force update
if [ -n "${1:-}" ]; then
rm -rf "$GO_INSTALL_SCRIPT"
fi
if [ ! -f "$GO_INSTALL_SCRIPT" ]; then
# locally update
if [ -f "./install.sh" ]; then
cp -f "./install.sh" "$GO_INSTALL_SCRIPT"
else # git update
check_in_china
if [ -n "$IN_CHINA" ]; then
PRO_URL="$PRO_CN_URL"
fi
curl -fsSL -m 5 -o "$GO_INSTALL_SCRIPT" "$PRO_URL/install.sh"
fi
fi
if [ ! -f "$GO_INSTALL_SCRIPT" ]; then
say_err "GVM go-install.sh download failed\n"
fi
chmod +x "$GO_INSTALL_SCRIPT"
}
# gvm script install
install_script() {
gvm_script
dl_goinstall_script
say "GVM successfully installed\n"
if ! command -v gvm >/dev/null; then
printf "You need to execute: \n\e[1;33msource %s\e[m\n" "$PROFILE"
fi
}
# gvm script upgrade
update_script() {
gvm_script
dl_goinstall_script "force"
say "GVM successfully updated\n"
}
# Get a list of locally installed Go versions.
go_list_locale() {
if ! find "$GO_VERSIONS_PATH"/go*/bin/go -maxdepth 1 -type f >/dev/null 2>&1; then
if [ -z "${1:-}" ]; then
say_err "No go version list found\n"
fi
return
fi
# GO_VERSION_LIST=$(find "${GO_VERSIONS_PATH}" -maxdepth 1 -name "go*" -type d | cut -d '/' -f 6 | sed 's/..//')
while IFS= read -r _V; do
GO_VERSION_LIST[${#GO_VERSION_LIST[@]}]=$(${_V} version | awk '{print $3}' | sed 's/..//')
done < <(find "$GO_VERSIONS_PATH"/go*/bin/go -maxdepth 1 -type f)
}
# Obtain a list of versions from the Go official website.
go_list_remote() {
check_in_china
local GO_DL_URL="https://go.dev/dl/"
[ -z "$IN_CHINA" ] || GO_DL_URL="https://golang.google.cn/dl/"
RELEASE_TAGS=$(curl -sL --retry 5 --max-time 10 "$GO_DL_URL" | sed -n '/toggle/p' | cut -d '"' -f 4 | grep go | grep -Ev 'rc|beta')
while IFS=$'\n' read -r -d ' ' _V; do
REMOTE_GO_LIST[${#REMOTE_GO_LIST[@]}]="$_V"
done < <(echo "$RELEASE_TAGS" | tr -s '\n' ' ')
}
# Install Go
install_go() {
go_list_locale "install"
for _V in "${GO_VERSION_LIST[@]:-}"; do
if [ "$_V" = "$GO_VERSION" ]; then
say_err "Go $GO_VERSION already exists\n"
fi
done
go_list_remote
local REMOTE_HAS_VERSION=""
for _V in "${REMOTE_GO_LIST[@]:-}"; do
if [ "$_V" = "go$GO_VERSION" ]; then
REMOTE_HAS_VERSION=1
break
fi
done
if [ -z "$REMOTE_HAS_VERSION" ]; then
say_err "There is no such version(go$GO_VERSION)\n"
fi
printf "\e[1;33mInstalling go %s\e[m\n" "$GO_VERSION"
PARAMS="--version $GO_VERSION --root $GO_VERSIONS_PATH/go$GO_VERSION"
# echo "${GO_INSTALL_SCRIPT} ${PARAMS}"
# shellcheck disable=SC2086
$SHELL $GO_INSTALL_SCRIPT $PARAMS >"$HOME/.gvm.log" 2>&1
# shellcheck disable=SC1090,SC2086
CURRENT_GO_BINARY="$GO_VERSIONS_PATH/go$GO_VERSION/bin/go"
if [ -f "$CURRENT_GO_BINARY" ]; then
$CURRENT_GO_BINARY version
else
say_err "Go $GO_VERSION installation failed\n"
fi
}
# List all locally installed go versions
show_list() {
go_list_locale
for _V in "${GO_VERSION_LIST[@]:-}"; do
if [ -z "$_V" ]; then
continue
fi
printf "go%s\n" "$_V"
done
}
# List all remote go versions
show_remote_list() {
go_list_remote
local SHOW_ALL=""
SHOW_ALL="${1:-}"
local __INDEX=1
for _V in "${REMOTE_GO_LIST[@]:-}"; do
if [ $__INDEX -ge 10 ] && [ -z "$SHOW_ALL" ]; then
break
fi
printf "%s\n" "$_V"
((__INDEX++))
done
}
# Remove custom go version from locally
uninstall_go() {
if command -v go > /dev/null; then
if [ "$(go version | awk '{print $3}')" = "go$GO_VERSION" ]; then
say_err "The current version(go${GO_VERSION}) is in use\n"
fi
fi
if [ -d "$GO_VERSIONS_PATH/go$GO_VERSION" ]; then
rm -rf "$GO_VERSIONS_PATH/go$GO_VERSION"
say "go$GO_VERSION uninstalled successfully\n"
else
say "go$GO_VERSION does not exist\n"
fi
}
# Use custom go version
use_go() {
CURRENT_GO_BINARY="${GO_VERSIONS_PATH}/go${GO_VERSION}/bin/go"
if [ ! -f "$CURRENT_GO_BINARY" ]; then
install_go
else
$CURRENT_GO_BINARY version
fi
if [ -f "$CURRENT_GO_BINARY" ]; then
rm -rf "$GVM_GO_ROOT"
ln -s "$GO_VERSIONS_PATH/go$GO_VERSION" "$GVM_GO_ROOT"
# use $HOME instead of /home/{USER}/
__GVM_GO_ROOT=${GVM_GO_ROOT/"$HOME"/\$HOME}
sedi "s@^export GOROOT.*@export GOROOT=\"$__GVM_GO_ROOT\"@" "$PROFILE"
# sedi "s@^export GOROOT.*@export GOROOT=\"${GO_VERSIONS_PATH}/go${GO_VERSION}\"@" "$PROFILE"
if ! command -v go >/dev/null 2>&1 || [ "$(go version | awk '{print $3}')" != "go$GO_VERSION" ]; then
printf "\nYou need to execute: \n\e[1;33msource %s\e[m\n" "$PROFILE"
fi
fi
}
# Get go latest version
get_latest_version() {
_VERSION="${1:-}"
if [ "$_VERSION" = "latest" ]; then
_VERSION=$(show_remote_list | sort -rV | head -n 1)
fi
}
# show help message
show_help_message() {
printf "\e[1;32mgvm\e[m %s
Golang Version Manager
\e[1;33mUsage:\e[m %s [OPTIONS] [COMMAND]
\e[1;33mCommands:\e[m
\e[1;32muse\e[m [VERSION|latest] Change Go version
\e[1;32minstall\e[m [VERSION|latest] Install a new go version
\e[1;32muninstall\e[m Uninstall a Go version
\e[1;32mlist\e[m List all locally installed go versions
\e[1;32mremote\e[m <more> List all remote go versions
\e[1;32mversion\e[m Print the current go version
\e[1;33mOptions:\e[m
\e[1;32m-i, --install\e[m
Install Golang Version Manager
\e[1;32m-u, --update\e[m
Update Golang Version Manager
\e[1;32m-h, --help\e[m
Print help information
\e[1;32m-v, --version\e[m
Print Gvm version information
\n" "$GVM_VERSION" "$script_dir_name"
exit 0
}
# 处理参数信息
judgment_parameters() {
# 参数带帮助
if [ $# -eq 0 ]; then
if [ "$0" == "bash" ] || [ "$0" == "-bash" ]; then # 无参数,远程,安装
DO_ACTION="--install"
else # 无参数,本地
DO_ACTION="--help"
fi
return
fi
while [ $# -gt 0 ]; do
case "$1" in
# 切换 Go
'use')
if [ -z "${2:-}" ]; then
say_err "Missing go version argument. \n Example: $script_name use [VERSION]\n"
fi
DO_ACTION="use"
DO_PARAMETER="${2:-}"
shift
;;
# 安装 Go
'install')
if [ -z "${2:-}" ]; then
say_err "Missing go version argument. \n Example: $script_name install [VERSION]\n"
fi
DO_ACTION="install"
DO_PARAMETER="${2:-}"
shift
;;
'uninstall')
if [ -z "${2:-}" ]; then
say_err "Missing go version argument. \n Example: $script_name uninstall [VERSION]\n"
fi
DO_ACTION="uninstall"
DO_PARAMETER="${2:-}"
shift
;;
# 显示本地已安装的 Go 版本
'list')
DO_ACTION="list"
;;
# 显示远程已安装的 Go 版本
'remote')
DO_ACTION="remote"
DO_PARAMETER="${2:-}"
if [ -n "$DO_PARAMETER" ]; then
shift
fi
;;
# 显示 Go 版本
'version')
DO_ACTION="version"
;;
# 安装此项目
'-i' | '--install')
DO_ACTION="--install"
;;
# 更新此项目
'-u' | '--update')
DO_ACTION="--update"
;;
# 显示脚本版本
'-v' | '--version')
DO_ACTION="--version"
;;
# 帮助
'-h' | '--help')
DO_ACTION="--help"
;;
# 未知参数
*)
echo "$script_dir_name: unknown option -- $1"
exit 1
;;
esac
shift
done
}
do_action() {
case "${DO_ACTION:-}" in
# 切换 Go
'use')
get_latest_version "${DO_PARAMETER:-}"
GO_VERSION="${_VERSION#go}"
use_go
;;
# 安装 Go
'install')
get_latest_version "${DO_PARAMETER:-}"
GO_VERSION="${_VERSION#go}"
install_go
;;
'uninstall')
_VERSION="${DO_PARAMETER:-}"
GO_VERSION="${_VERSION#go}"
uninstall_go
;;
# 显示本地已安装的 Go 版本
'list')
show_list
;;
# 显示远程已安装的 Go 版本
'remote')
_MODE="${DO_PARAMETER:-}"
show_remote_list "$_MODE"
;;
# 显示 Go 版本
'version')
if ! command -v go >/dev/null 2>&1; then
_msg=$(printf "Go is not installed\nPlease execute: \e[1;33m%s install latest\e[0m\n" "$script_dir_name")
say_err "$_msg"
else
go version
fi
;;
# 安装此项目
'-i' | '--install')
install_script
;;
# 更新此项目
'-u' | '--update')
update_script
;;
# 显示脚本版本
'-v' | '--version')
printf "gvm version: %s\n" "$GVM_VERSION"
;;
# 帮助
'-h' | '--help')
show_help_message
;;
# 未知参数
*)
;;
esac
}
set_project_url() {
PRO_URL="https://raw.githubusercontent.com/jetsung/golang-install/main/"
PRO_CN_URL="https://framagit.org/jetsung/golang-install/-/raw/main/"
# DEBUG
if [ -n "$DEBUG" ]; then
PRO_CN_URL="https://git.envs.net/jetsung/learn/raw/branch/dev/"
fi
}
main() {
GVM_VERSION="1.1.0"
GVM_PATH="$HOME/.gvm"
GVM_BIN_PATH="$GVM_PATH/bin"
GVM_ENV_PATH="$GVM_PATH/env"
GVM_GO_ROOT="$GVM_PATH/go"
GO_INSTALL_SCRIPT="$GVM_PATH/install.sh"
GO_VERSIONS_PATH="$GVM_PATH/packages"
DEBUG=""
PROFILE=""
PROFILE="$(detect_profile)"
if [ -z "$PROFILE" ]; then
say_err "Error: can not find profile"
fi
IN_CHINA=""
REMOTE_GO_LIST=()
GO_VERSION_LIST=()
# 动作
DO_ACTION=""
# 参数
DO_PARAMETER=""
if [ ! -d "$GO_VERSIONS_PATH" ]; then
mkdir -p "$GO_VERSIONS_PATH"
fi
# 检测系统
init_os
# 安装必要的依赖
install_curl_command
# 设置环境变量
set_environment
# 设置项目地址
set_project_url
# 提取参数和值
judgment_parameters "$@"
# 执行命令
do_action
}
main "$@" || exit 1