-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgolang.sh
More file actions
executable file
·359 lines (312 loc) · 8.64 KB
/
Copy pathgolang.sh
File metadata and controls
executable file
·359 lines (312 loc) · 8.64 KB
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
#!/usr/bin/env bash
#
# Copyright (c) 2021-2022 brian
#
# Short: https://git.io/csh-golang.sh
# File name: golang.sh
# Description: Install latest version golang
# System Required: GNU/Linux
# Version: 1.0
##############################################################
## Project Vars
# Name
PROJECT_NAME="golang.sh"
# Release link
RELEASE_URL="https://golang.org/dl/"
# Version link
VERSION_URL="${RELEASE_URL}?mode=json"
# Download archive name
DOWNLOAD_FILE=""
# Set Default GOPATH PATH
INSTALL_PATH="/usr/local"
INSTALL_AUTO="n"
##############################################################
## Global Vars
ARCH=""
PLATFORM=""
DISTRIBUTION=""
PACKAGING=""
# Shell
USR_SHELL="bash"
BASHRC="${HOME}/.bashrc"
ZSHRC="${HOME}/.zshrc"
# root required
SUDO=""
# Common
set -o errexit
set -o errtrace
set -o pipefail
export LC_ALL=C
export LANG=C
export LANGUAGE=en_US.UTF-8
# fonts color
ERRO(){
echo -e "\033[31m\033[01m$1\033[0m"
}
INFO(){
echo -e "\033[32m\033[01m$1\033[0m"
}
WARN(){
echo -e "\033[33m\033[01m$1\033[0m"
}
SUCC(){
echo -e "\033[34m\033[01m$1\033[0m"
}
BOLD(){
echo -e "\033[1m\033[01m$1\033[0m"
}
##############################################################
## Detect
# Get OS arch
detect_arch() {
ARCH=$(uname -m)
case $ARCH in
amd64) ARCH="amd64";;
x86_64) ARCH="amd64";;
i386) ARCH="386";;
armv6l) ARCH="armv6l";;
armv7l) ARCH="armv6l";;
aarch64) ARCH="arm64";;
*) ERRO "Unsupported architecture: ${ARCH}"; exit 1;;
esac
echo -e "ARCH = ${ARCH}"
}
# Get OS version
detect_platform() {
# check os name
PLATFORM=$(uname -s | tr '[:upper:]' '[:lower:]')
case $PLATFORM in
darwin) PLATFORM='darwin';;
linux) PLATFORM='linux';;
freebsd) PLATFORM='freebsd';;
*) ERRO "Unsupported platform: ${PLATFORM}"; exit 1;;
esac
echo -e "PLATFORM = ${PLATFORM}"
}
detect_distribution() {
# check os release
DISTRIBUTION=""
if [[ -f /etc/redhat-release ]]; then
DISTRIBUTION="centos"
PACKAGING="yum"
elif [ -f /etc/issue ]; then
if grep -Eqi "debian|raspbian" /etc/issue; then
DISTRIBUTION="debian"
PACKAGING="apt-get"
elif grep -Eqi "ubuntu" /etc/issue; then
DISTRIBUTION="ubuntu"
PACKAGING="apt-get"
elif grep -Eqi "centos|red hat|redhat" /etc/issue; then
DISTRIBUTION="centos"
PACKAGING="yum"
fi
elif [ -f /proc/version ]; then
if grep -Eqi "debian|raspbian" /proc/version; then
DISTRIBUTION="debian"
PACKAGING="apt-get"
elif grep -Eqi "ubuntu" /proc/version; then
DISTRIBUTION="ubuntu"
PACKAGING="apt-get"
elif grep -Eqi "centos|red hat|redhat" /proc/version; then
DISTRIBUTION="centos"
PACKAGING="yum"
fi
elif which sw_vers >/dev/null 2>&1; then
DISTRIBUTION=$(sw_vers -productName)
PACKAGING="brew"
SUDO=""
else
ERRO "Unsupported os distribution: ${DISTRIBUTION}"; exit 1;
fi
echo -e "DISTRIB = ${DISTRIBUTION}"
}
detect_shell() {
USR_SHELL=$(expr "$SHELL" : '.*/\(.*\)')
echo -e "Current shell is ${USR_SHELL}"
}
detect_network() {
country_code=$(curl -SsL https://api.ip.sb/geoip | sed 's/,/\n/g' | grep country_code | awk -F '"' '{print $(NF-1)}')
if [ "$country_code" = "CN" ]; then
RELEASE_URL="https://mirrors.ustc.edu.cn/golang/"
VERSION_URL="https://golang.google.cn/dl/?mode=json"
WARN "Can't download from origin, change to: ${RELEASE_URL}"
fi
}
# detect env
detect_environment() {
INFO "Detect system environment ..."
detect_arch
detect_platform
detect_distribution
detect_shell
detect_network
}
##############################################################
## Depend
# install depend list
install_depend() {
if [[ $(id -u) -ne 0 ]]; then
SUDO="sudo"
fi
depend_list=""
if ! which curl >/dev/null 2>&1; then
depend_list="${depend_list} curl"
fi
if ! which jq >/dev/null 2>&1; then
depend_list="${depend_list} jq"
fi
if [ -n "$depend_list" ]; then
installed=$(${SUDO} ${PACKAGING} install -y ${depend_list})
echo -e "Installing: \n${installed}"
fi
}
# download archive
download_archive() {
src="$1"
dst="$2"
INFO "Downloading $src to $dst"
if which curl >/dev/null 2>&1; then
curl -L -o "$dst" "$src"
elif which wget >/dev/null 2>&1; then
wget "$src" -O "$dst"
else
ERRO "No curl or wget found, how to download the archive?"
exit 1
fi
}
##############################################################
## Project Code
CUSTOM_VERSION=""
## Show help
show_help() {
echo "usage: ./${PROJECT_NAME} [-h] [-v version] [-d gopath] [-a]"
echo ' -d : set go path (default: %s/go)'
echo ' -v : set go version (default: latest version)'
echo ' -a, --auto : auto select latest or custom'
echo ' -h, --help : Show help'
echo ''
echo '> bash <(curl -fsSL git.io/csh-golang.sh)'
exit 0
}
# init install path
install_path() {
if [ ! -d ${INSTALL_PATH} ]; then
mkdir -p ${INSTALL_PATH}
fi
INFO "Install ${PROJECT_NAME} path: ${INSTALL_PATH}"
}
# Pick latest version
pick_version() {
INFO "Pick version for ${PROJECT_NAME}, custom version: ${CUSTOM_VERSION} ..."
# fetch latest verion
LATEST=$(curl -fsSL "${VERSION_URL}")
OLD_VERSION="none"
NEW_VERSION=$(echo "${LATEST}" | jq -r '.[0] .version')
OPT_VERSION=$(echo "${LATEST}" | jq -r '.[1] .version')
# check installed version
if which go >/dev/null 2>&1; then
OLD_VERSION="$(go version | awk '{print $3}')"
BOLD "Current version: ${OLD_VERSION}"
fi
if [ "$OLD_VERSION" = "${NEW_VERSION}" ]; then
INFO "You have installed this version: ${OLD_VERSION}";
fi
BOLD "Latest version: ${NEW_VERSION}"
# select version
INFO "------ Select install version ------"
echo -e "1) ${NEW_VERSION}"
echo -e "2) ${OPT_VERSION}"
if [ -n "$CUSTOM_VERSION" ]; then
echo -e "3) go${CUSTOM_VERSION} [Custom]"
fi
echo -e "0) Exit"
# auto select
if [[ ${INSTALL_AUTO} = "y" ]]; then
if [ -n "$CUSTOM_VERSION" ]; then
menu_num=3
else
menu_num=1
fi
INFO "Auto select: ${menu_num}"
else
echo && read -n1 -p "Please select: " menu_num
fi
# check select
case $menu_num in
1 | 2)
menu_num=$((menu_num-1))
DOWNLOAD_FILE=$(echo "${LATEST}" | jq -r ".[${menu_num}] .files[] | select(.arch == \"${ARCH}\" and .os == \"${PLATFORM}\" and .kind == \"archive\") | .filename")
install_binary
;;
3)
DOWNLOAD_FILE="go${CUSTOM_VERSION}.${PLATFORM}-${ARCH}.tar.gz"
install_binary
;;
0)
exit 1
;;
*)
echo && read -n1 -p "Please select: " menu_num
;;
esac
}
# install binary
install_binary() {
INFO "\nInstalling ${PROJECT_NAME} ..."
if [[ -f ${DOWNLOAD_FILE} ]] ; then
echo -e "the archive has been downloaded. ${DOWNLOAD_FILE}"
else
download_archive "${RELEASE_URL}${DOWNLOAD_FILE}" ${DOWNLOAD_FILE}
fi
rm -rf ${INSTALL_PATH}/go && tar xzf ${DOWNLOAD_FILE} -C ${INSTALL_PATH}
}
# config
install_config() {
INFO "Configure ${PROJECT_NAME} ..."
export_data="export PATH=\$PATH:${INSTALL_PATH}/go/bin"
export_file=""
if [[ "$USR_SHELL" = "zsh" ]]; then
export_file=${ZSHRC}
else
export_data="export PATH=\$PATH:${INSTALL_PATH}/go/bin"
export_file=${BASHRC}
fi
if grep "PATH:${INSTALL_PATH}/go/bin" "${export_file}" ; then
BOLD "You has the following lines to ${export_file}"
BOLD " ${export_data} >> ${export_file}"
else
echo -e "${export_data}" >> "${export_file}"
INFO "You must use: source ${export_file}"
INFO "Finished install ${DOWNLOAD_FILE}"
$SHELL ${export_file}
fi
}
# install result
install_result() {
if which go >/dev/null 2>&1; then
SUCC "Current version : $(go version) !"
fi
}
# init args
init_args() {
while [[ "$#" -gt 0 ]]; do
case $1 in
-v) CUSTOM_VERSION="$2"; shift ;;
-d) INSTALL_PATH="$2"; shift ;;
-a | --auto) INSTALL_AUTO="y" ;;
-h | --help) show_help ;;
*) ERRO "Unknown parameter passed: $1"; exit 1 ;;
esac
shift
done
}
main() {
init_args "$@"
detect_environment
install_depend
install_path
pick_version
install_config
install_result
}
main "$@" || exit 1