Skip to content

Commit e8803fe

Browse files
committed
perf: 优化 install.sh 脚本
1 parent 3a2b96d commit e8803fe

File tree

4 files changed

+284
-297
lines changed

4 files changed

+284
-297
lines changed

README.md

+11-11
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ gvm version
3232
```bash
3333
# gvm -h
3434

35-
gvm 1.1.0
35+
gvm 1.1.1
3636

3737
Golang Version Manager
3838

@@ -119,24 +119,24 @@ chmod +x install.sh
119119
**Usage**
120120
./install.sh -h
121121

122-
```
122+
```sh
123123
Go install
124124

125125
USAGE:
126-
install.sh [OPTIONS] <SUBCOMMANDS>
126+
install.sh [OPTIONS]
127127

128128
OPTIONS:
129-
-h, --help
130-
Print help information.
129+
-p, --path <GOPATH>
130+
Set GOPATH. (default: $HOME/go)
131131

132-
-p, --path
133-
Set GOPATH. (default: $HOME/go)
132+
-r, --root <GOROOT>
133+
Set GOROOT. (default: $HOME/.go)
134134

135-
-r, --root
136-
Set GOROOT. (default: $HOME/.go)
135+
-v, --version <VERSION>
136+
Set golang version.
137137

138-
-v, --version
139-
Set golang version.
138+
-h, --help
139+
Print help information.
140140
```
141141

142142
## License

README_CN.md

+11-12
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ gvm version
3232
```bash
3333
# gvm -h
3434

35-
gvm 1.1.0
35+
gvm 1.1.1
3636

3737
Golang Version Manager
3838

@@ -118,25 +118,24 @@ chmod +x install.sh
118118

119119
**使用说明**
120120

121-
```
121+
```sh
122122
Go install
123123

124124
USAGE:
125-
install.sh [OPTIONS] <SUBCOMMANDS>
125+
install.sh [OPTIONS]
126126

127127
OPTIONS:
128-
-h, --help
129-
Print help information.
128+
-p, --path <GOPATH>
129+
Set GOPATH. (default: $HOME/go)
130130

131-
-p, --path
132-
Set GOPATH. (default: $HOME/go)
131+
-r, --root <GOROOT>
132+
Set GOROOT. (default: $HOME/.go)
133133

134-
-r, --root
135-
Set GOROOT. (default: $HOME/.go)
136-
137-
-v, --version
138-
Set golang version.
134+
-v, --version <VERSION>
135+
Set golang version.
139136

137+
-h, --help
138+
Print help information.
140139
```
141140

142141
## License

gvm.sh

+58-79
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ if [ -t 1 ] && command -v tput >/dev/null; then
3131
fi
3232

3333
say_warning() {
34-
# shellcheck disable=SC2317
3534
printf "%b\n" "${yellow:-}$script_name: Warning: $1${normal:-}" >&3
3635
}
3736

@@ -59,54 +58,36 @@ sh_echo() {
5958

6059
# Get PROFILE
6160
detect_profile() {
62-
if [ "${PROFILE:-}" = '/dev/null' ]; then
63-
# the user has specifically requested NOT to have nvm touch their profile
64-
return
65-
fi
66-
67-
if [ -n "$PROFILE" ] && [ -f "$PROFILE" ]; then
68-
sh_echo "$PROFILE"
69-
return
70-
fi
71-
72-
local DETECTED_PROFILE
73-
DETECTED_PROFILE=''
61+
local DETECTED_PROFILE=""
7462

7563
if [ -z "${SHELL:-}" ]; then
7664
SHELL="$(grep "^$(whoami):" /etc/passwd | cut -d: -f7)"
7765
fi
7866

79-
if [ "${SHELL#*bash}" != "$SHELL" ]; then
80-
if [ -f "$HOME/.bashrc" ]; then
81-
DETECTED_PROFILE="$HOME/.bashrc"
82-
elif [ -f "$HOME/.bash_profile" ]; then
83-
DETECTED_PROFILE="$HOME/.bash_profile"
84-
fi
85-
elif [ "${SHELL#*zsh}" != "$SHELL" ]; then
86-
if [ -f "$HOME/.zshrc" ]; then
87-
DETECTED_PROFILE="$HOME/.zshrc"
88-
fi
89-
elif [ "${SHELL#*sh}" != "$SHELL" ]; then
90-
if [ -f "$HOME/.profile" ]; then
67+
BASENAME_SHELL=$(basename "$SHELL")
68+
case "$BASENAME_SHELL" in
69+
'sh')
9170
DETECTED_PROFILE="$HOME/.profile"
92-
fi
93-
fi
94-
95-
if [ -z "$DETECTED_PROFILE" ]; then
96-
for EACH_PROFILE in ".profile" ".bashrc" ".bash_profile" ".zshrc"; do
97-
if DETECTED_PROFILE="$(try_profile "$HOME/$EACH_PROFILE")"; then
98-
break
99-
fi
100-
done
101-
fi
71+
;;
72+
'zsh')
73+
DETECTED_PROFILE="$HOME/.zshrc"
74+
;;
75+
'bash')
76+
DETECTED_PROFILE="$HOME/.bashrc"
77+
;;
78+
'fish')
79+
DETECTED_PROFILE="$HOME/.config/fish/config.fish"
80+
;;
81+
*)
82+
return
83+
;;
84+
esac
10285

103-
if [ -z "$DETECTED_PROFILE" ]; then
104-
if [ "$(id -u)" -eq 0 ]; then
105-
DETECTED_PROFILE="/etc/profile"
106-
fi
86+
if [ ! -f "$DETECTED_PROFILE" ]; then
87+
touch "$DETECTED_PROFILE"
10788
fi
10889

109-
if [ -n "$DETECTED_PROFILE" ]; then
90+
if [ -f "$DETECTED_PROFILE" ]; then
11091
sh_echo "$DETECTED_PROFILE"
11192
fi
11293
}
@@ -122,7 +103,7 @@ sedi() {
122103

123104
# check in china
124105
check_in_china() {
125-
if ! curl -s -m 3 -IL https://google.com | grep -q "HTTP/2 200"; then
106+
if [ "$(curl -s -m 3 -o /dev/null -w "%{http_code}" https://www.google.com)" != "200" ]; then
126107
IN_CHINA=1
127108
fi
128109
}
@@ -157,39 +138,39 @@ install_curl_command() {
157138

158139
# set gvm environment
159140
set_environment() {
141+
# shellcheck disable=SC2016
142+
if ! grep -q '$HOME/.gvm/env' "$PROFILE"; then
143+
{
144+
printf '\n## GVM\n. "$HOME/.gvm/env"\n'
145+
} >>"$PROFILE"
146+
else
147+
sedi 's@^. "$HOME/.gvm/.*@. "$HOME/.gvm/env"@' "$PROFILE"
148+
fi
149+
160150
cat > "$GVM_ENV_PATH" <<-'EOF'
161-
#!/usr/bin/env bash
151+
#!/usr/bin/env sh
162152
163153
export GVMPATH="$HOME/.gvm"
164-
export PATH="$PATH:$GVMPATH/bin"
165-
166-
__MY_PATHS=""
167-
# Remove duplicate paths and remove go is not gvm version
168-
# export PATH=$(echo $PATH | sed 's/:/\n/g' | sort | uniq | tr -s '\n' ':' | sed 's/:$//g')
169-
while IFS=$'\n' read -r -d ' ' _V; do
170-
if command -v "$_V/go" >/dev/null 2>&1 && [[ -f "$_V/go" ]]; then
171-
[[ "$_V" == "$GVMPATH/go/bin" ]] || continue
172-
fi
173-
__MY_PATHS="$__MY_PATHS:$_V"
174-
done < <(echo "$PATH" | sed 's/:/\n/g' | sort | uniq | tr -s '\n' ' ')
175-
176-
[[ -z "$__MY_PATHS" ]] || export PATH="${__MY_PATHS#*:}"
154+
155+
case ":${PATH}:" in
156+
*:"$GVMPATH/bin":*) ;;
157+
*) export PATH="$GVMPATH/bin:$PATH" ;;
158+
esac
159+
160+
## GOLANG
161+
export GOROOT="$HOME/.gvm/go"
162+
export GOPATH="$HOME/go"
163+
export GOBIN="$GOPATH/bin"
164+
export GOPROXY="https://goproxy.cn,https://goproxy.io,direct"
165+
export PATH="$PATH:$GOROOT/bin:$GOBIN"
177166
EOF
178167

179-
if [ ! -f "$PROFILE" ]; then
180-
touch "$PROFILE"
168+
if [ -z "$IN_CHINA" ]; then
169+
sedi '/GOPROXY/d' "$GVM_ENV_PATH"
181170
fi
182171

183172
# shellcheck disable=SC2016
184-
if ! grep -q '$HOME/.gvm/env' "$PROFILE"; then
185-
printf "\n## GVM\n" >>"$PROFILE"
186-
echo '. "$HOME/.gvm/env"' >>"$PROFILE"
187-
else
188-
sedi 's@^. "$HOME/.gvm/.*@. "$HOME/.gvm/env"@' "$PROFILE"
189-
fi
190-
191-
# Remove duplicate paths
192-
# export PATH=$(echo $PATH | sed 's/:/\n/g' | sort | uniq | tr -s '\n' ':' | sed 's/:$//g')
173+
sedi '/## GOLANG/,/export PATH="$PATH:$GOROOT"/d' "$PROFILE"
193174
}
194175

195176
# update gvm.sh
@@ -203,8 +184,9 @@ gvm_script() {
203184
if [ -f "$CURRENT_GVM_PATH.sh" ]; then
204185
cp "$CURRENT_GVM_PATH.sh" "$GVM_SCRIPT_PATH"
205186
else # git update
206-
check_in_china
207-
[ -z "$IN_CHINA" ] || PRO_URL="$PRO_CN_URL"
187+
if [ -n "$IN_CHINA" ]; then
188+
PRO_URL="$PRO_CN_URL"
189+
fi
208190
curl -sSL -m 5 -o "$GVM_SCRIPT_PATH" "$PRO_URL/gvm.sh"
209191
fi
210192

@@ -226,7 +208,6 @@ dl_goinstall_script() {
226208
if [ -f "./install.sh" ]; then
227209
cp -f "./install.sh" "$GO_INSTALL_SCRIPT"
228210
else # git update
229-
check_in_china
230211
if [ -n "$IN_CHINA" ]; then
231212
PRO_URL="$PRO_CN_URL"
232213
fi
@@ -279,7 +260,9 @@ go_list_locale() {
279260
go_list_remote() {
280261
check_in_china
281262
local GO_DL_URL="https://go.dev/dl/"
282-
[ -z "$IN_CHINA" ] || GO_DL_URL="https://golang.google.cn/dl/"
263+
if [ -n "$IN_CHINA" ]; then
264+
GO_DL_URL="https://golang.google.cn/dl/"
265+
fi
283266

284267
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')
285268

@@ -376,7 +359,7 @@ uninstall_go() {
376359

377360
# Use custom go version
378361
use_go() {
379-
CURRENT_GO_BINARY="${GO_VERSIONS_PATH}/go${GO_VERSION}/bin/go"
362+
CURRENT_GO_BINARY="$GO_VERSIONS_PATH/go$GO_VERSION/bin/go"
380363
if [ ! -f "$CURRENT_GO_BINARY" ]; then
381364
install_go
382365
else
@@ -387,11 +370,6 @@ use_go() {
387370
rm -rf "$GVM_GO_ROOT"
388371
ln -s "$GO_VERSIONS_PATH/go$GO_VERSION" "$GVM_GO_ROOT"
389372

390-
# use $HOME instead of /home/{USER}/
391-
__GVM_GO_ROOT=${GVM_GO_ROOT/"$HOME"/\$HOME}
392-
sedi "s@^export GOROOT.*@export GOROOT=\"$__GVM_GO_ROOT\"@" "$PROFILE"
393-
394-
# sedi "s@^export GOROOT.*@export GOROOT=\"${GO_VERSIONS_PATH}/go${GO_VERSION}\"@" "$PROFILE"
395373
if ! command -v go >/dev/null 2>&1 || [ "$(go version | awk '{print $3}')" != "go$GO_VERSION" ]; then
396374
printf "\nYou need to execute: \n\e[1;33msource %s\e[m\n" "$PROFILE"
397375
fi
@@ -618,7 +596,7 @@ set_project_url() {
618596
}
619597

620598
main() {
621-
GVM_VERSION="1.1.0"
599+
GVM_VERSION="1.1.1"
622600

623601
GVM_PATH="$HOME/.gvm"
624602
GVM_BIN_PATH="$GVM_PATH/bin"
@@ -630,17 +608,18 @@ main() {
630608

631609
DEBUG=""
632610

633-
PROFILE=""
611+
BASENAME_SHELL=""
634612
PROFILE="$(detect_profile)"
635613

636614
if [ -z "$PROFILE" ]; then
637615
say_err "Error: can not find profile"
638616
fi
639617

640618
IN_CHINA=""
619+
check_in_china
641620

642621
REMOTE_GO_LIST=()
643-
GO_VERSION_LIST=()
622+
GO_VERSION_LIST=()
644623

645624
# 动作
646625
DO_ACTION=""

0 commit comments

Comments
 (0)