Skip to content

Instantly share code, notes, and snippets.

@brianredbeard
Created January 9, 2020 22:48
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 brianredbeard/ec344edc53a0428df32a45465cbca830 to your computer and use it in GitHub Desktop.
Save brianredbeard/ec344edc53a0428df32a45465cbca830 to your computer and use it in GitHub Desktop.
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