forked from isislovecruft/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
golang
executable file
·229 lines (210 loc) · 6.02 KB
/
golang
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
#!/bin/bash
##############################################################################
#
# golang
# -------------------
# Install or update Go language.
#
# Download Go source tarball and SHA1 hash from https://code.google.com. If
# hashcheck is okay, install and setup Go in /usr/local/go, and modify the
# system default $PATH to include it by appending to /etc/profile.
#
# For x86_64 and 32-bit Linux, only. Should work on all distros, except for
# RHEL and CentOS.
#
# @author Isis Agora Lovecruft, 0x2cdb8b35
# @date 3 September 2012
# @version 0.0.4
#
##############################################################################
set -e
GO_VERSION="1.0.3"
function usage ()
{
echo
echo "$0 [options]"
echo
echo "install Install Go ${GO_VERSION}"
echo "reinstall Update Go to ${GO_VERSION}"
echo "path Export PATH to Go & GoTools"
echo
}
function install_go ()
{
OUR_ARCH=$(uname -m)
FILE="https://go.googlecode.com/files/go"
DOWN="https://code.google.com/p/go/downloads/detail?name=go"
if [[ "$OUR_ARCH" == "x86_64" ]]; then
ARCH_STRING=".linux-amd64.tar.gz"
elif [[ "$OUR_ARCH" == "i686" ]]; then
ARCH_STRING=".linux-386.tar.gz"
else
echo "Unsupported architecture. Must be 64-bit or 32-bit."
exit 1
fi
tarball=${FILE}${GO_VERSION}${ARCH_STRING}
details=${DOWN}${GO_VERSION}${ARCH_STRING}
GO_SOURCE=${HOME}/go${GO_VERSION}.tar.gz
HTML_PAGE=${HOME}/go${GO_VERSION}.tar.gz.html
if ! test -f "$GO_SOURCE" ; then
if test -n "$tarball"; then
echo "Getting GoTools from:
${tarball} "
echo "Downloading GoTools, which includes gc (go compiler)..."
wget --no-check-certificate -q -O $GO_SOURCE ${tarball}
else
echo "Error determining correct download...exiting..."
exit 1
fi
else
echo "We've already got the latest source code, using that..."
fi
if ! test -f "$HTML_PAGE" ; then
echo "Getting reported SHA1 from:
${details} "
wget --no-check-certificate -q -O $HTML_PAGE ${details}
echo
else
echo "We've already got the reported SHA1 from code.google.com..."
echo
fi
theirs=/tmp/go_reported_hash
ours=/tmp/go_our_hash
echo "Checking SHA1 hash of downloaded source..."
grep "sha1" $HTML_PAGE | cut -d ">" -f 2 | cut -d "<" -f 1 | cat > $theirs
sha1sum $GO_SOURCE | cut -d " " -f 1 > $ours
echo "Reported SHA1 from Google Code: $(cat $theirs )"
echo "Computed SHA1 from download: $(cat $ours )"
HASHDIFF=`diff ${theirs} ${ours}`
if [[ "$HASHDIFF" == "" ]]; then
echo
echo "Installing GoTools ..."
sudo tar -C /usr/local -xzf $GO_SOURCE && echo "Installation successful."
else
echo "WARNING: SHA1 sums do not match. Exiting..." & exit 1
fi
}
function clean_up ()
{
JUNK="$theirs $ours $HTML_PAGE $GO_SOURCE "
echo
echo "Cleaning up..."
for file in $JUNK; do
if test -f $file ; then
echo "Removing ${file}..."
sudo rm $file
fi
done
}
function export_paths ()
{
if [ $(id -u) -eq 0 ]; then
echo
echo "Modifying system \$PATH to include Go..."
sudo cat >> /etc/profile<<EOF
## Path to GoTools
export GOROOT=/usr/local/go
export PATH=$PATH:$GOROOT/bin
EOF
echo "Successfully updated \$PATH in /etc/profile."
else
echo
echo "Modifying user \$PATH..."
sudo cat >> $HOME/.profile<<FIN
## Path to GoTools
export GOROOT=/usr/local/go
export PATH=$PATH:$GOROOT/bin
FIN
echo "Successfully updated \$PATH in ${HOME}/.profile..."
fi
}
function root_check ()
{
if [ $(id -u) -ne 0 ]; then
echo "Must be run as root."
exit 1
fi
}
function os_check ()
{
if [ $(uname) != "Linux" ]; then
echo "Sorry, this only works for Linux."
exit 1
fi
}
function go_check ()
{
if test -n $(which go) ; then
current=$(go version | cut -d " " -f 3 | cut -b 3- )
echo "Found previous Go installation. Version: ${current} "
if test -d /usr/local/go ; then
if [[ "$current" == "${GO_VERSION}" ]] ; then
echo "Already up to date!"
exit 0
elif [[ "$current" > "${GO_VERSION}" ]] ; then
echo "Oops. You've got Go ${current}, while we've got Go ${GO_VERSION}."
echo "Please mailto:[email protected] and tell Isis to update me."
echo
else
echo "Updating previous Go installation to ${GO_VERSION}..."
fi
fi
fi
}
function mercurial_check ()
{
if test -z $(which hg) ; then
echo "Go requires mercurial.Please use your distro's package manager
to obtain it.
Exiting..."
fi
}
if [[ "$#" == "0" ]]; then
usage
exit 1
else
while [[ "x$1" != "x" ]]; do
case $1 in
install )
root_check
mercurial_check
os_check
go_check
install_go
clean_up
export_paths
echo
echo "Installation successful!"
echo
echo "To get started learning go, try doing:"
echo " \$ go get code.google.com/p/go-tour/gotour "
echo
unset tarball
unset details
exit 0
;;
reinstall )
root_check
mercurial_check
os_check
go_check
install_go
clean_up
echo
echo "Upgrade successful!"
echo
unset tarball
unset details
exit 0
;;
path )
os_check
export_paths
exit 0
;;
* )
usage
;;
esac
done
fi