Skip to content

Instantly share code, notes, and snippets.

@jaytaylor
Last active November 15, 2021 21:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaytaylor/5a6d52fdfe69582347115cb9f46e13d3 to your computer and use it in GitHub Desktop.
Save jaytaylor/5a6d52fdfe69582347115cb9f46e13d3 to your computer and use it in GitHub Desktop.
Golang Linux installer configurator
#!/usr/bin/env bash
##
# @description Golang Linux installer.
#
# Example usage:
#
# VERSION=1.17.3 sudo -E bash install-go.sh
#
# @date 2021-02-18
#
# @author Jay Taylor <outtatime@gmail.com>
#
VERSION="${VERSION:-1.17.3}"
archive_old_go() {
if [ -d '/usr/local/go' ]; then
local old_ver
old_ver="$( \
/usr/local/go/bin/go version \
| awk '{print gensub("^go", "", "g", $3)}' \
)"
rm -rf "/usr/local/go-${old_ver}"
mv '/usr/local/go' "/usr/local/go-${old_ver}"
elif [ -L '/usr/local/go' ]; then
unlink '/usr/local/go'
fi
}
install_profile_d() {
cat << 'EOF' > /etc/profile.d/go.sh
export GOROOT='/usr/local/go'
export GOPATH="${HOME}/go"
export PATH="${PATH}:${GOROOT}/bin:${GOPATH}/bin"
EOF
}
install_go() {
local -r archive="go${VERSION}.linux-amd64.tar.gz"
cd /tmp
curl -fSLO "https://golang.org/dl/${archive}"
rm -rf 'go'
tar -xzf "${archive}"
mv 'go' "/usr/local/go-${VERSION}"
ln -s "/usr/local/go-${VERSION}" '/usr/local/go'
rm -rf "${archive}" "${HOME}/go/pkg/linux_amd64"
}
install_go_main() {
if [ "${UID}" -ne 0 ]; then
echo 'ERROR: Must run as root' 1>&2
return 1
fi
archive_old_go
install_go
install_profile_d
}
if [ "${BASH_SOURCE[0]}" = "${0}" ] || [ "${BASH_SOURCE[0]}" = '--' ]; then
set -o errexit
set -o pipefail
set -o nounset
if [ "${1:-}" = '-v' ]; then
echo 'INFO: Verbose output enabled' 1>&2
shift
set -o xtrace
fi
install_go_main
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment