-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall.sh
242 lines (214 loc) · 5.84 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
#!/bin/bash
# Pretty Go Version Manager
#
# https://github.com/voocel/sv
#
# voocel, [email protected]
set -eu
GOPATH=${GOPATH:-$HOME/go}
GOROOT=${GOROOT:-$HOME/.sv/go}
if [ "$(echo "$GOROOT" | cut -c1)" != "/" ]; then
print_error "$GOROOT must be an absolute path but it is set to $GOROOT"
fi
print_banner() {
cat <<-'EOF'
=================================================
___
/ /\ ___
/ /::\ / /\
/__/:/\:\ / /:/
_\_ \:\ \:\ / /:/
/__/\ \:\ \:\ /__/:/ ___
\ \:\ \:\_\/ | |:| / /\
\ \:\_\:\ | |:|/ /:/
\ \:\/:/ |__|:|__/:/
\ \::/ \__\::::/
\__\/ ````
___ _ _ _
|_ _|_ __ ___| |_ __ _| | | ___ _ __
| || '_ \/ __| __/ _` | | |/ _ \ '__|
| || | | \__ \ || (_| | | | __/ |
|___|_| |_|___/\__\__,_|_|_|\___|_|
==================================================
EOF
}
setup_color() {
RESET=$(printf '\033[0m')
BOLD=$(printf '\033[1m')
RED=$(printf '\033[31m')
GREEN=$(printf '\033[32m')
YELLOW=$(printf '\033[33m')
BLUE=$(printf '\033[34m')
PINK=$(printf '\033[35m')
CYAN=$(printf '\033[36m')
GRAY=$(printf '\033[37m')
}
print_error() {
printf '%sError: %s%s\n' "${BOLD}${RED}" "$*" "${RESET}" >&2
exit 1
}
get_os() {
uname_out=""
if command -v uname >/dev/null 2>&1; then
uname_out="$(uname)"
if [ "${uname_out}" = "" ]; then
return 1
else
echo "${uname_out}"
return 0
fi
else
return 20
fi
}
get_arch() {
arch=""
# silent=${1:-}
arch_check=${ASDF_GOLANG_OVERWRITE_ARCH:-"$(uname -m)"}
case "${arch_check}" in
x86_64|amd64) arch="amd64"; ;;
i686|i386|386) arch="386"; ;;
armv6l|armv7l) arch="armv6l"; ;;
aarch64|arm64) arch="arm64"; ;;
ppc64le) arch="ppc64le"; ;;
*)
print_error "Arch '${arch_check}' not supported!"
;;
esac
printf "%s" "$arch"
}
get_shell_profile() {
shell_profile=""
# shellcheck disable=SC2039
if [[ "${SHELL}" == *"sh"* ]]; then
if [[ -f "$HOME/.bashrc" ]]; then
shell_profile="$HOME/.bashrc"
elif [[ -f "$HOME/.bash_profile" ]]; then
shell_profile="$HOME/.bash_profile"
fi
elif [[ "${SHELL}" == *"zsh"* ]]; then
shell_profile="$HOME/.zshrc"
fi
if [ -z "$shell_profile" ]; then
print_error "Get shell profile error, please set .bashrc"
fi
}
gen_env() {
cat>"$HOME/.sv/env"<<EOF
#!/bin/sh
# sv shell setup
case ":${PATH}:" in
*:"$HOME/.sv/go/bin:$HOME/.sv/bin":*)
;;
*)
export GO111MODULE=auto
export SVHOME=$HOME/.sv
export GOROOT=$HOME/.sv/go
export GOPROXY=https://goproxy.cn,direct
export PATH="$HOME/.sv/go/bin:$HOME/.sv/bin:$PATH"
;;
esac
EOF
}
set_env() {
envStr='. "$HOME/.sv/env"'
if grep -q "$envStr" "${shell_profile}"; then
echo "${BLUE}SV env has exists in $shell_profile${RESET}"
else
echo $envStr >> "${shell_profile}"
fi
# . ${shell_profile}
# source $shell_profile
}
## Detect the curl
check_curl() {
if ! (test -x "$(command -v curl)"); then
print_error "You must pre-install the curl tool"
fi
}
get_svbin() {
svbin=''
THISOS=$(uname -s)
ARCH=$(uname -m)
case $THISOS in
Linux*)
case $ARCH in
arm64)
svbin="sv-linux-arm-64"
;;
aarch64)
svbin="sv-linux-arm-64"
;;
*)
svbin="sv-linux-amd-64"
;;
esac
;;
Darwin*)
case $ARCH in
arm64)
svbin="sv-darwin-arm-64"
;;
*)
svbin="sv-darwin-64"
;;
esac
;;
Windows*)
svbin="sv-windows-64.exe"
;;
esac
}
get_latest_tag() {
local release=$(curl -s "https://api.github.com/repos/voocel/sv/releases/latest" | grep '"tag_name":' | cut -d'"' -f4)
echo "$release"
}
main() {
setup_color
echo "${YELLOW}[1/3] Get sv latest version${RESET}"
local release="$(get_latest_tag)"
if [ -z "$release" ]; then
print_error "Get sv latest version error, please try again"
fi
printf "${GREEN}The sv latest version is %s${RESET}\n" $release
# local os="$(uname -s | awk '{print tolower($0)}')"
os=`get_os|tr "[A-Z]" "[a-z]"`
print_banner
if [ -f "$HOME/.bash_profile" ]; then
. "$HOME/.bash_profile"
fi
echo "${YELLOW}[2/3] Downloading sv to the /usr/local/bin${RESET}"
check_curl
get_svbin
if [ ! -d "$HOME/.sv/bin" ];then
mkdir -p "$HOME/.sv/bin"
fi
download_url=https://github.com/voocel/sv/releases/download/$release/$svbin
echo "$download_url"
# http_code=$(curl -I -w '%{http_code}' -s -o /dev/null "$download_url")
# if [ "$http_code" -eq 404 ] || [ "$http_code" -eq 403 ]; then
# print_error "URL: ${download_url} returned status ${http_code}"
# fi
# curl -kLs download_url -o "$HOME/.sv/bin/sv"
wget ${download_url} -O "$HOME/.sv/bin/sv"
chmod +x "$HOME/.sv/bin/sv"
echo "${GREEN}Installed successfully to: $HOME/.sv/bin/sv${RESET}"
echo "${YELLOW}[3/3] Setting environment variables${RESET}"
gen_env
get_shell_profile
set_env
echo "${GREEN}Set env successfully${RESET}"
# [ -z "$GOROOT" ] && GOROOT="$HOME/.go"
# [ -z "$GOPATH" ] && GOPATH="$HOME/go"
# mkdir -p "$GOPATH"/{src,pkg,bin} "$GOROOT"
}
set +u
if [ ! -d "${SVHOME}" ]; then
set -u
main "$@" || exit 1
else
echo "${YELLOW}Already installed!${RESET}"
echo "${YELLOW}You can delete the directory($HOME/.sv) and reinstall${RESET}"
fi
echo "${GREEN}end.${RESET}"
set -u