forked from Jrohy/go-install
-
Notifications
You must be signed in to change notification settings - Fork 1
/
install.sh
208 lines (187 loc) · 5.96 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
#!/bin/bash
# Author: Jrohy
# Github: https://github.com/Jrohy/go-install
# cancel centos alias
[[ -f /etc/redhat-release ]] && unalias -a
can_google=1
force_mode=0
sudo=""
os="Linux"
install_version=""
proxy_url="https://goproxy.cn"
#######color code########
red="31m"
green="32m"
yellow="33m"
blue="36m"
fuchsia="35m"
color_echo(){
echo -e "\033[$1${@:2}\033[0m"
}
#######get params#########
while [[ $# > 0 ]];do
case "$1" in
-v|--version)
install_version="$2"
echo -e "准备安装$(color_echo ${blue} $install_version)版本golang..\n"
shift
;;
-f)
force_mode=1
echo -e "强制更新golang..\n"
;;
*)
# unknown option
;;
esac
shift # past argument or value
done
#############################
ip_is_connect(){
ping -c2 -i0.3 -W1 $1 &>/dev/null
if [ $? -eq 0 ];then
return 0
else
return 1
fi
}
setup_env(){
if [[ $sudo == "" ]];then
profile_path="/etc/profile"
elif [[ -e ~/.zshrc ]];then
profile_path="$HOME/.zprofile"
fi
if [[ $sudo == "" && -z `echo $GOPATH` ]];then
while :
do
read -p "默认GOPATH路径: `color_echo $blue /home/go`, 回车直接使用或者输入自定义绝对路径: " GOPATH
if [[ $GOPATH ]];then
if [[ ${GOPATH:0:1} != "/" ]];then
color_echo $yellow "请输入绝对路径!"
continue
fi
else
GOPATH="/home/go"
fi
break
done
echo "GOPATH值为: `color_echo $blue $GOPATH`"
echo "export GOPATH=$GOPATH" >> $profile_path
echo 'export PATH=$PATH:$GOPATH/bin' >> $profile_path
mkdir -p $GOPATH
fi
if [[ -z `echo $PATH|grep /usr/local/go/bin` ]];then
echo 'export PATH=$PATH:/usr/local/go/bin' >> $profile_path
fi
source $profile_path
}
check_network(){
ip_is_connect "golang.org"
[[ ! $? -eq 0 ]] && can_google=0
}
setup_proxy(){
if [[ $can_google == 0 && `go env|grep proxy.golang.org` ]]; then
go env -w GO111MODULE=on
go env -w GOPROXY=$proxy_url,direct
color_echo $green "当前网络环境为国内环境, 成功设置goproxy代理!"
fi
}
sys_arch(){
arch=$(uname -m)
if [[ `uname -s` == "Darwin" ]];then
os="Darwin"
if [[ "$arch" == "arm64" ]];then
vdis="darwin-arm64"
else
vdis="darwin-amd64"
fi
else
if [[ "$arch" == "i686" ]] || [[ "$arch" == "i386" ]]; then
vdis="linux-386"
elif [[ "$arch" == *"armv7"* ]] || [[ "$arch" == "armv6l" ]]; then
vdis="linux-armv6l"
elif [[ "$arch" == *"armv8"* ]] || [[ "$arch" == "aarch64" ]]; then
vdis="linux-arm64"
elif [[ "$arch" == *"s390x"* ]]; then
vdis="linux-s390x"
elif [[ "$arch" == "ppc64le" ]]; then
vdis="linux-ppc64le"
elif [[ "$arch" == "x86_64" ]]; then
vdis="linux-amd64"
fi
fi
[ $(id -u) != "0" ] && sudo="sudo"
}
install_go(){
if [[ -z $install_version ]];then
echo "正在获取最新版golang..."
count=0
while :
do
install_version=""
if [[ $can_google == 0 ]];then
install_version=`curl -s --connect-timeout 15 -H 'Cache-Control: no-cache' https://go.dev/dl/|grep -w downloadBox|grep src|grep -oE '[0-9]+\.[0-9]+\.?[0-9]*'|head -n 1`
else
install_version=`curl -s --connect-timeout 15 -H 'Cache-Control: no-cache' https://github.com/golang/go/tags|grep releases/tag|grep -v rc|grep -v beta|grep -oE '[0-9]+\.[0-9]+\.?[0-9]*'|head -n 1`
fi
[[ ${install_version: -1} == '.' ]] && install_version=${install_version%?}
if [[ -z $install_version ]];then
if [[ $count < 3 ]];then
color_echo $yellow "获取go版本号超时, 正在重试..."
else
color_echo $red "\n获取go版本号失败!"
exit 1
fi
else
break
fi
count=$(($count+1))
done
echo "最新版golang: `color_echo $blue $install_version`"
fi
if [[ $force_mode == 0 && `command -v go` ]];then
if [[ `go version|awk '{print $3}'|grep -Eo "[0-9.]+"` == $install_version ]];then
return
fi
fi
file_name="go${install_version}.$vdis.tar.gz"
local temp_path=`mktemp -d`
curl -H 'Cache-Control: no-cache' -L https://dl.google.com/go/$file_name -o $file_name
tar -C $temp_path -xzf $file_name
if [[ $? != 0 ]];then
color_echo $yellow "\n解压失败! 正在重新下载..."
rm -rf $file_name
curl -H 'Cache-Control: no-cache' -L https://dl.google.com/go/$file_name -o $file_name
tar -C $temp_path -xzf $file_name
[[ $? != 0 ]] && { color_echo $yellow "\n解压失败!"; rm -rf $temp_path $file_name; exit 1; }
fi
[[ -e /usr/local/go ]] && $sudo rm -rf /usr/local/go
$sudo mv $temp_path/go /usr/local/
rm -rf $temp_path $file_name
}
install_updater(){
if [[ $os == "Linux" ]];then
if [[ ! -e /usr/local/bin/goupdate || -z `cat /usr/local/bin/goupdate|grep '$@'` ]];then
echo 'source <(curl -L https://go-install.netlify.app/install.sh) $@' > /usr/local/bin/goupdate
chmod +x /usr/local/bin/goupdate
fi
elif [[ $os == "Darwin" ]];then
if [[ ! -e $HOME/go/bin/goupdate || -z `cat $HOME/go/bin/goupdate|grep '$@'` ]];then
cat > $HOME/go/bin/goupdate << 'EOF'
#!/bin/zsh
source <(curl -L https://go-install.netlify.app/install.sh) $@
EOF
chmod +x $HOME/go/bin/goupdate
fi
fi
}
main(){
sys_arch
check_network
install_go
setup_env
setup_proxy
install_updater
echo -e "golang `color_echo $blue $install_version` 安装成功!"
}
main