Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Golang Installation script
#!/usr/bin/env bash
# Golang Installation Script (c) Brian 'redbeard' Harrington, 2019
#
# I'm very about how Golang is installed on my system. I normally run through
# this entire process by hand, but realized that it's easier to just have this
# thing to do the needful. Funny enough, 1.13.6 was published in the middle of
# me working on it, so it is already useful.
#
# In the future, I may add a "VERSION" env variable to allow for arbitrary
# versions rather than the latest stable.
#
# NOTE: This is not sorting the JSON returned by the Go website, so it's up to
# the server side. This just takes the first value returned.
#
# requires jq and curl
#
PREFIX=${PREFIX:-/usr/local/golang}
set -eu -o pipefail
function finish {
rm -rf "$scratch"
}
trap finish EXIT
scratch=$(mktemp -d /tmp/.golang-setup-XXXX)
info="$(curl -sL "https://golang.org/dl/?mode=json" | jq -r '[.[].files[] | select(.filename | test("go.+linux-amd64.tar.gz"; "sx"))][0]')"
# extract invidivual values
filename=$(echo ${info} | jq -r .filename )
version=$(echo ${info} | jq -r .version )
version=${version#go*}
sha256=$(echo ${info} | jq -r .sha256 )
function dl_go() {
curl -Lo ${scratch}/${filename} https://golang.org/dl/${filename}
HASH="$(sha256sum -b ${scratch}/${filename} | cut -c-64)"
if [ "${HASH}" != "${sha256}" ]; then
echo "SHA 256 Hash failed, aborting install"
exit 1
fi
echo "SHA 256 Hash verified , installing golang"
}
if [ ! -d ${PREFIX}/${version} ]; then
dl_go
mkdir -v -p ${PREFIX}/$version
tar xvf ${scratch}/${filename} -C ${PREFIX}/${version}
ln -sfr -T ${PREFIX}/${version} ${PREFIX}/latest
else
echo "Latest version already installed at ${PREFIX}/${version}"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment