Jay Taylor's notes

back to listing index

Golang Linux installer configurator · GitHub

[web search]
Original source (gist.github.com)
Tags: shell golang go installer jay-taylor gist gist.github.com
Clipped on: 2021-11-09

Instantly share code, notes, and snippets.

Golang Linux installer configurator
1 #!/usr/bin/env bash
2
3 ##
4 # @description Golang Linux installer.
5 #
6 # Example usage:
7 #
8 # sudo -E ./install-go.sh
9 #
10 # @date 2021-02-18
11 #
12 # @author Jay Taylor <outtatime@gmail.com>
13 #
14
15 VERSION="${VERSION:-1.17.3}"
16
17 archive_old_go() {
18 if [ -d '/usr/local/go' ]; then
19 local old_ver
20 old_ver="$(
21 /usr/local/go/bin/go version
22 | awk '{print gensub("^go", "", "g", $3)}'
23 )"
24 rm -rf "/usr/local/go-${old_ver}"
25 mv '/usr/local/go' "/usr/local/go-${old_ver}"
26 elif [ -L '/usr/local/go' ]; then
27 unlink '/usr/local/go'
28 fi
29 }
30
31 install_profile_d() {
32 cat << 'EOF' > /etc/profile.d/go.sh
33 export GOROOT='/usr/local/go'
34 export GOPATH="${HOME}/go"
35
36 export PATH="${PATH}:${GOROOT}/bin:${GOPATH}/bin"
37 EOF
38 }
39
40 install_go() {
41 local -r archive="go${VERSION}.linux-amd64.tar.gz"
42
43 cd /tmp
44
45 curl -sSLO "https://golang.org/dl/${archive}"
46
47 rm -rf 'go'
48
49 tar -xzf "${archive}"
50
51 mv 'go' "/usr/local/go-${VERSION}"
52
53 ln -s "/usr/local/go-${VERSION}" '/usr/local/go'
54
55 rm -rf "${archive}" "${HOME}/go/pkg/linux_amd64"
56 }
57
58 main_install_go() {
59 if [ "${UID}" -ne 0 ]; then
60 echo 'ERROR: Must run as root' 1>&2
61 return 1
62 fi
63
64 archive_old_go
65 install_go
66 install_profile_d
67 }
68
69 if [ "${BASH_SOURCE[0]}" = "${0}" ] || [ "${BASH_SOURCE[0]}" = '--' ]; then
70 set -o errexit
71 set -o pipefail
72 set -o nounset
73
74 if [ "${1:-}" = '-v' ]; then
75 echo 'INFO: Verbose output enabled' 1>&2
76 shift
77 set -o xtrace
78 fi
79
80 main_install_go
81 fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment