/
glv
executable file
·203 lines (187 loc) · 5.61 KB
/
glv
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
#!/usr/bin/env bash
#
# Golang Latest Version Manager
# Should work on sh, dash, bash, ksh, zsh
#
# Copyright (C) 2021-present, Implemented by Bin Hua <code@tourcoder.com>
golang_version_remote() {
curl -s https://go.dev/VERSION\?m\=text | awk -F'go' '{print $2}'
}
golang_version_local() {
go version | awk '{print $3}' | sed -e 's/go//'
}
golang_is_installed() {
command -v go > /dev/null
}
system_type() {
case "$(uname)" in
"Darwin")
echo "darwin-amd64"
;;
"Linux")
if [ "$(getconf LONG_BIT)" = "64" ]; then
echo "linux-amd64"
else
echo "linux-386"
fi
;;
*)
echo "System unsupported, please download and install manually. https://go.dev/dl/" && exit 1
;;
esac
}
golang_version_file() {
curl -LO https://go.dev/dl/go$(golang_version_remote).$(system_type).tar.gz
}
export_path_to_current_shell() {
local EXPORTSCRITP="export PATH=\$PATH:/usr/local/go/bin"
local currentshell=$(ps -p $PPID -o comm=)
if [ "${currentshell}" == "-zsh" -o "${currentshell}" == "zsh" ]; then
echo "${EXPORTSCRITP}">>~/.zshrc
elif [ "${currentshell}" == "-bash" -o "${currentshell}" == "bash" ]; then
echo "${EXPORTSCRITP}">>~/.bashrc
else
echo -e "${EXPORTSCRIPT}"
fi
echo "Done, append the following to your shell profile\\nexport PATH=\$PATH:/usr/local/go/bin" && exit 1
}
opt_golang(){
if golang_is_installed; then
if [[ $(golang_version_local) == $(golang_version_remote) ]]; then
echo "The latest version ($(golang_version_local)) is already installed on your system"
else
echo "Local version: $(golang_version_local)"
echo "Latest version: $(golang_version_remote)"
read -p "Do you want to update? (y/N): " answer
if [[ "${answer}" == "Y" ]] || [[ "${answer}" == "y" ]] || [[ "${answer}" == "" ]]; then
echo "Updating..."
cd /tmp
golang_version_file
sudo mv /usr/local/go /usr/local/go$(golang_version_local)
sudo tar -C /usr/local -zxf go$(golang_version_remote).$(system_type).tar.gz
rm -rf go$(golang_version_remote).$(system_type).tar.gz
export_path_to_current_shell
else
echo -e "Golang update cancelled" && exit 1
fi
fi
else
echo "Installing..."
cd /tmp
golang_version_file
sudo tar -C /usr/local -zxf go$(golang_version_remote).$(system_type).tar.gz
rm -rf go$(golang_version_remote).$(system_type).tar.gz
export_path_to_current_shell
fi
}
set_gopath() {
if golang_is_installed; then
local GOPATH=$(go env GOPATH)
echo "Current GOPATH: ${GOPATH}"
read -p "Do you want to change GOPATH? (y/N): " answer
if [[ "${answer}" == "Y" ]] || [[ "${answer}" == "y" ]] || [[ "${answer}" == "" ]]; then
read -p "Enter the new GOPATH: " new_gopath
go env -w GOPATH=$new_gopath
echo "GOPATH has been changed to: ${new_gopath}, run 'go env' to check"
else
echo "GOPATH unchanged" && exit 1
fi
else
echo "Golang is not installed, please run 'glv -i' to install" && exit 1
fi
}
set_gomodcache() {
if golang_is_installed; then
local GOMODCACHE=$(go env GOMODCACHE)
echo "Current GOMODCACHE: ${GOMODCACHE}"
read -p "Do you want to change GOMODCACHE? (y/N): " answer
if [[ "${answer}" == "Y" ]] || [[ "${answer}" == "y" ]] || [[ "${answer}" == "" ]]; then
read -p "Enter the new GOMODCACHE: " new_gomodcache
go env -w GOMODCACHE=$new_gomodcache
echo "GOMODCACHE has been changed to: ${new_gomodcache}, run 'go env' to check"
else
echo "GOMODCACHE unchanged" && exit 1
fi
else
echo "Golang is not installed, please run 'glv -i' to install" && exit 1
fi
}
glv_version() {
echo "v0.1.5"
}
glv_remote_version() {
curl -s "https://api.github.com/repos/glv-go/glv/releases" | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/' | awk 'NR==1'
}
glv_version_local() {
glv --version
}
opt_glv() {
if [[ $(glv_version_local) == $(glv_remote_version) ]]; then
glv_help
else
echo "GLV Local version: $(glv_version_local)"
echo "GLV Latest version: $(glv_remote_version)"
read -p "Do you want to update? (y/N): " answer
if [[ "${answer}" == "Y" ]] || [[ "${answer}" == "y" ]] || [[ "${answer}" == "" ]]; then
echo "Updating..."
sudo curl -sL https://raw.githubusercontent.com/glv-go/glv/master/glv -o /usr/local/bin/glv
sudo chmod +x /usr/local/bin/glv
echo -e "glv updated..." && exit 0
else
echo -e "glv update cancelled" && exit 1
fi
fi
}
glv_help() {
echo
echo "Golang Latest Version Manager ($(glv_version))"
echo
echo "Usage: glv [OPTION]..."
echo "Options:"
echo " -i, --install Install/Update Golang"
echo " -s, --setgopath Set GOPATH"
echo " -sc,--setgomodcache Set GOMODCACHE"
echo " -h, --help Show help information"
echo " -v, --version Show GLV version"
echo " -u, --upgrade Upgrade GLV to latest version"
echo
}
main(){
if [[ $(command -v glv) ]]; then
case $1 in
-i|--install)
opt_golang
exit 0
;;
-s|--setgopath)
set_gopath
exit 0
;;
-sc|--setgomodcache)
set_gomodcache
exit 0
;;
-h|--help)
glv_help
exit 0
;;
-v|--version)
echo $(glv_version)
exit 0
;;
-u|--upgrade)
opt_glv
exit 0
;;
*)
opt_glv
exit 1
;;
esac
else
sudo curl -sL https://raw.githubusercontent.com/glv-go/glv/master/glv -o /usr/local/bin/glv
sudo chmod +x /usr/local/bin/glv
echo "glv installed, just run 'glv --help'" && exit 1
fi
}
main $1