Skip to content

Instantly share code, notes, and snippets.

@cpuguy83
Created April 25, 2024 21:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cpuguy83/0bd267268cbf612d27841d0eb0e06fe2 to your computer and use it in GitHub Desktop.
Save cpuguy83/0bd267268cbf612d27841d0eb0e06fe2 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
set -u
: "${PREFIX:=/usr/local}"
PATH="${PREFIX}/go/bin:${PATH}"
if ! command -v jq > /dev/null; then
echo "Installing jq" >&2
sudo apt-get install -y jq || exit 1
fi
checkLocal() {
if command -v go > /dev/null; then
if [ "$(go version | awk '{ print $3 }')" = "go${VERSION}" ]; then
go version
exit 0
fi
fi
}
if [ -v VERSION ]; then
checkLocal
fi
set -e
tmp="$(mktemp -d)"
cleanup() {
rm -rf "${tmp}"
}
trap 'cleanup' EXIT
data="$(curl -fsSL https://go.dev/dl/?mode=json)"
: "${ARCH="$(dpkg-architecture -q DEB_BUILD_ARCH)"}"
: "${VERSION="$(jq -r --arg arch ${ARCH} '.[0].files | map(select( (.os == "linux") and (.arch == $arch) ))[0].version' <<<${data})"}"
VERSION="${VERSION#go}"
checkLocal
: "${SHA256SUM=$(jq -r --arg arch ${ARCH} --arg ver go${VERSION} '.[] | select(.version==$ver) | .files[] | select((.arch==$arch) and (.os=="linux")) | .sha256' <<<"${data}")}"
cd "${tmp}"
curl -SLf https://go.dev/dl/go${VERSION}.linux-${ARCH}.tar.gz > go.tar.gz
echo "${SHA256SUM} go.tar.gz" > go.tar.gz.sha256sum
cat go.tar.gz.sha256sum | sha256sum -c --quiet
sudo rm -rf ${PREFIX}/go
sudo tar -C ${PREFIX} -zxf go.tar.gz
go version
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment