Skip to content

Instantly share code, notes, and snippets.

@azlan
Created May 11, 2023 14:06
Show Gist options
  • Save azlan/0d8e9e9a094c1bd5ea4dacbf3b678df0 to your computer and use it in GitHub Desktop.
Save azlan/0d8e9e9a094c1bd5ea4dacbf3b678df0 to your computer and use it in GitHub Desktop.
#!/usr/bin/env fish
set TMP_DIR "/tmp"
function get_latest_go_version
set GO_DEV_DL_PAGE "https://go.dev/dl/"
set LATEST_VERSION (curl -sL $GO_DEV_DL_PAGE | grep -oP 'go\d+(\.\d+)+\.linux-amd64\.tar\.gz' | awk -F '.linux-amd64.tar.gz' '{print $1}' | sort -V | tail -n 1 | sed 's/go//')
echo $LATEST_VERSION
end
if test -z $argv[1]
echo "No version supplied, fetching the latest version..."
set GO_VERSION (get_latest_go_version)
else
set GO_VERSION $argv[1]
end
set GO_ARCHIVE "go$GO_VERSION.linux-amd64.tar.gz"
set GO_DOWNLOAD_URL "https://go.dev/dl/$GO_ARCHIVE"
# Download Go archive
echo "Downloading Go version $GO_VERSION..."
curl -L -o "$TMP_DIR/$GO_ARCHIVE" "$GO_DOWNLOAD_URL"
# Verify the download
if test $status -ne 0
echo "Failed to download Go version $GO_VERSION. Please check the URL and try again."
exit 1
end
# Verify the downloaded file is a valid gzip archive
gzip -t "$TMP_DIR/$GO_ARCHIVE"
if test $status -ne 0
echo "The downloaded file is not a valid gzip archive. Please try again."
exit 1
end
# Remove the previous Go installation
echo "Removing previous Go installation..."
sudo rm -rf /usr/local/go
# Extract the downloaded archive to /usr/local
echo "Installing Go version $GO_VERSION..."
sudo tar -C /usr/local -xzf "$TMP_DIR/$GO_ARCHIVE"
# Remove the downloaded archive
rm "$TMP_DIR/$GO_ARCHIVE"
fish_add_path /usr/local/go/bin
# Check the installed version
set INSTALLED_VERSION (go version 2>/dev/null | awk '{print $3}' | tr -d 'go')
if test "$INSTALLED_VERSION" = "$GO_VERSION"
echo "Go version $GO_VERSION successfully installed."
else
echo "Installation failed. Please try again."
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment