-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgopher
executable file
·209 lines (167 loc) · 5.9 KB
/
gopher
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
#!/usr/bin/env bash
# Author: Stanislav <git.io/monesonn>
# Description: Shell script for fetching and updating Golang.
# Licence: ISC
# Credit: https://gist.github.com/nikhita/432436d570b89cab172dcf2894465753
set -euo pipefail
IFS=$'\n\t'
_version="1.1.1"
_shell=`getent passwd $LOGNAME | cut -d: -f7 | awk -F "/" '{print $NF}'`
_destination="$HOME/.gopher"
fuzzy=false
blue="\033[34m"
yellow="\033[1;33m"
red="\033[0;31m"
noc="\033[0;m"
banner () {
cat << "EOF"
,_---~~~~~----._
_,,_,*^____ _____``*g*\"*,
/ __/ /' ^. / \ ^@q f
[ @f | @)) | | @)) l 0 _/
\`/ \~____ / __ \_____/ \
| _l__l_ I
} [______] I
] | | | |
] ~_~ | __
___ ____ ___ / / ___ ____ ___ / /
/ _ `/ _ \/ _ \/ _ \/ -_) __/ (_-</ _ \
\_, /\___/ .__/_//_/\__/_/ (_)___/_//_/
/___/ /_/
EOF
}
help () {
printf %b\\n\\n "${blue}`basename $0` v.$_version ${noc}"
cat << EOF
Description:
Shell script for fetching and updating Golang.
Usage:
`basename $0` [option]
Options:
--go go Startpoint
--check check Check out new releases
--fuzzy fuzzy Same as above option, but uses fzf for searching specific versions
Use one of them
--version version Show current Golang and script version
--help help Show help text
Examples:
gopher go
gopher fuzzy
gopher go='specific version' like 1.16.5
EOF
}
err () {
printf %b\\n "${red}[!] ${yellow}$@${noc}"
}
gopher_say () {
printf %b\\n "${blue}[gopher]${noc} $1"
}
gopher_say_sleepy () {
printf %b\\n "${blue}[gopher]${noc} $1"
sleep 1
}
version () {
[[ `command -v go` ]] && (
gopher_say "script version $_version"
gopher_say `go version`
) || (
gopher_say $_version
)
}
check () {
release=`curl --silent https://golang.org/doc/devel/release | grep -Eo 'go[0-9]+(\.[0-9]+)+' | sort -V | uniq | tail -1`
gopher_say "$release is available."
[[ `command -v go` ]] && \
current_version=`go version | grep -Eo 'go[0-9]+(\.[0-9]+)+'` && \
[[ $current_version == $release ]] && { gopher_say_sleepy "All is up to date."; exit 0; }
}
find () {
release=`curl --silent https://golang.org/doc/devel/release | grep -Eo "go$1" | sort -V | uniq `
gopher_say "$release is available."
}
go! () {
gopher_say_sleepy "Starting."
[ ${#} -eq 0 ] && release_pattern='go[0-9]+(\.[0-9]+)+' || release_pattern="go$1"
[ $fuzzy = true ] && \
release=`curl --silent https://golang.org/doc/devel/release | grep -Eo $release_pattern | sort -rV | uniq | fzf` || \
release=`curl --silent https://golang.org/doc/devel/release | grep -Eo $release_pattern | sort -rV | uniq | head -1`
os=`uname -s | tr '[:upper:]' '[:lower:]'`
arch=`case "$(uname -m)" in i*) printf '386' ;; x*) printf 'amd64' ;; *) printf 'armv61'; esac`
[ `command -v go` ] && \
current_version=`go version | grep -Eo 'go[0-9]+(\.[0-9]+)+'` && \
[[ $current_version == $release ]] && { gopher_say_sleepy "All is up to date."; exit 0; }
gopher_say_sleepy "$release $os/$arch will installed. Is it correct? [y/n]"
read -n 1 -s -e -p '[gopher] ' answer
if [ $answer != "${answer#[Yy]}" ]; then
_temp=`mktemp -d` && gopher_say_sleepy "temp => $_temp"
[ -d $_destination/go ] || mkdir -p $_destination/go && gopher_say_sleepy "$_destination/go is created."
gopher_say_sleepy "$release.$os-$arch is installing."
gopher_say_sleepy "Progress:"
curl -# https://storage.googleapis.com/golang/$release.$os-$arch.tar.gz -o $_temp/$release.$os-$arch.tar.gz
gopher_say_sleepy "Extracting $_temp/$release.$os-$arch => $_destination/go"
tar -C $_destination/go --strip-components 1 -xzf $_temp/$release.$os-$arch.tar.gz
gopher_say_sleepy "Extracted."
gopher_say_sleepy "$release is installed."
rm -f $_temp/$release.$os-$arch.tar.gz && gopher_say_sleepy "$_temp/$release.$os-$arch.tar.gz is deleted."
else
gopher_say_sleepy "See ya!"
exit 0
fi
if [[ ":$PATH:" != *":$_destination/go/bin:"* ]]; then
gopher_say_sleepy "Adding Golang to PATH."
if [[ $_shell = "zsh" ]]; then
profile=".zshenv"
elif [[ $_shell = "bash" ]]; then
profile=".bash_profile"
else
profile=".profile"
fi
[ -a $HOME/$profile ] || touch $PWD/$profile
gopher_say_sleepy "Default shell: $_shell"
printf %s\\n "export GOPHER_PATH=$_destination" >> $HOME/$profile
printf %s\\n 'export GOPATH=$HOME/.go' >> $HOME/$profile
printf %s\\n 'export GOBIN="$GOPATH/bin"' >> $HOME/$profile
printf %s\\n 'export PATH=${PATH}:${GOPHER_PATH}/go/bin:$GOPATH/bin' >> $HOME/$profile
gopher_say_sleepy "Added to $profile"
. $HOME/$profile
gopher_say_sleepy "source ~/$profile"
fi
gopher_say_sleepy "Have fun!"
}
__main__ () {
while [ "$#" -gt 0 ]; do
argument="$1"
case $argument in
--go | go)
go!
shift
exit 0 ;;
--go=* | go=*)
go! "${argument#*=}"
shift
exit 0 ;;
--fuzzy | fuzzy)
fuzzy=true
go!
shift
exit 0 ;;
--check | check )
check
shift
exit 0 ;;
--version | version)
version
shift
exit 0 ;;
--help | help)
help
shift
exit 0 ;;
* | -* | --*)
err "No such option: $argument.\nType `basename $0` [--help|help] to see a list of all options."
exit 1 ;;
esac
done
err "Something went wrong...";
}
[ ${#} -eq 0 ] && { banner; help; } || __main__ "$@"