Skip to content

Instantly share code, notes, and snippets.

@zekroTJA
Created April 5, 2023 06:50
Show Gist options
  • Save zekroTJA/55714d08e44cf7e803810b98738fd20d to your computer and use it in GitHub Desktop.
Save zekroTJA/55714d08e44cf7e803810b98738fd20d to your computer and use it in GitHub Desktop.
A super simple script to update and backport your local Go installation.
#!/bin/bash
function error {
echo "❌ Error: $1"
exit 1
}
[ "$1" == "-h" ] || [ "$1" == "--help" ] && {
echo "Usage: $0 (version)"
echo ""
echo "Download and install the latest or given go version."
echo "You need to be root to execute this script."
exit 0
}
[ "$(id -u)" != 0 ] && {
error "Please execute this script as root."
}
[ -z $1 ] && {
VERSION=$(curl -s 'https://go.dev/VERSION?m=text')
} || {
VERSION=go${1#go*}
}
case $(uname -s) in
Darwin) OS=darwin ;;
Linux) OS=linux ;;
Windows) error "Sorry, but currently, this script does not support Windows. 😥" ;;
*) error "Could not detect OS type (uname -s)."
esac
case $(uname -m) in
i386) ARCH=386 ;;
x86_64) ARCH=amd64 ;;
arm|arm64) ARCH=arm64 ;;
*) error "Could not detect system architecture (uname -m)" ;;
esac
GODIR=$(which go 2> /dev/null || echo "/usr/local/go")
set -e
[ -f /tmp/goarch.tgz ] && {
rm -f /tmp/goarch.tgz
}
echo "⏱️ Downloading $VERSION ..."
CODE=$(curl -sLo /tmp/goarch.tgz -w "%{http_code}" "https://go.dev/dl/$VERSION.$OS-$ARCH.tar.gz")
case $CODE in
200) ;;
404) error "Version could not be found." ;;
*) error "Download failed unexpectedly ($CODE)" ;;
esac
[ -d $GODIR ] && {
echo "⏱️ Removing current installation ..."
rm -rf $GODIR
}
echo "⏱️ Moving files ..."
tar -C /usr/local -xzf /tmp/goarch.tgz
rm -f /tmp/goarch.tgz
echo "✅ Successfully installed $VERSION!"
printf "✅ $ go version > "
$GODIR/bin/go version
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment