Last active
May 5, 2023 13:35
-
-
Save abiiranathan/fd4aca2e30412a6db3f81c3ec789e5b1 to your computer and use it in GitHub Desktop.
Bash script to install the latest version of go or update Go to the latest version. Caches downloads so you won't do work twice.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# This script installs the latest version of Go on your system. | |
# _________ _ _______ _________ _______ _ _ _______ _______ | |
# \__ __/( ( /|( ____ \\__ __/( ___ )( \ ( \ ( ____ \( ___ ) | |
# ) ( | \ ( || ( \/ ) ( | ( ) || ( | ( | ( \/| ( ) | | |
# | | | \ | || (_____ | | | (___) || | | | | | | | | | | |
# | | | (\ \) |(_____ ) | | | ___ || | | | | | ____ | | | | | |
# | | | | \ | ) | | | | ( ) || | | | | | \_ )| | | | | |
# ___) (___| ) \ |/\____) | | | | ) ( || (____/\| (____/\ | (___) || (___) | | |
# \_______/|/ )_)\_______) )_( |/ \|(_______/(_______/ (_______)(_______) | |
# | |
# Generated with https://patorjk.com/software/taag/#p=display&f=Graffiti&t= | |
# Check if Go is already installed | |
if command -v go &>/dev/null; then | |
# Check the current version of Go | |
CURRENT_VERSION=$(go version | awk '{print $3}') | |
echo "Current version: ${CURRENT_VERSION}" | |
else | |
echo "Go is not installed." | |
CURRENT_VERSION="" | |
fi | |
# Get the latest version of Go | |
GO_VERSION=$(curl -SsL https://golang.org/VERSION?m=text) | |
# If installed version is the latest version, no work to do | |
if [[ "${GO_VERSION}" == "${CURRENT_VERSION}" ]]; then | |
echo "Latest version ${CURRENT_VERSION} is already installed." | |
exit 0 | |
fi | |
if [ -f "${HOME}/.cache/go_${GO_VERSION}.tar.gz" ]; then | |
echo "Found Go ${GO_VERSION} archive in cache. Using it instead of downloading again." | |
GO_ARCHIVE="${HOME}/.cache/go_${GO_VERSION}.tar.gz" | |
else | |
echo "Go ${GO_VERSION} archive not found in cache. Downloading latest version of Go." | |
GO_ARCHIVE="/tmp/go_${GO_VERSION}.tar.gz" | |
wget https://dl.google.com/go/${GO_VERSION}.linux-amd64.tar.gz -O "${GO_ARCHIVE}" | |
# Save the archive to the cache | |
cp "${GO_ARCHIVE}" "${HOME}/.cache/go_${GO_VERSION}.tar.gz" | |
fi | |
# Extract the archive to /usr/local/go | |
sudo tar -C /usr/local -xzf "${GO_ARCHIVE}" | |
if [[ ":$PATH:" == *":/usr/local/go/bin:"* ]]; then | |
echo "Go is already in your PATH." | |
else | |
echo "Go is not in your PATH." | |
echo "Would you like to add it to your PATH? (y/n)" | |
read answer | |
if [[ $answer == "y" ]]; then | |
# Set up the environment variables for Go | |
rc_file="${HOME}/.$(basename "${SHELL}")rc" | |
echo 'export GOPATH=$HOME/go' >> "${rc_file}" | |
echo 'export PATH=$PATH:/usr/local/go/bin' >> "${rc_file}" | |
echo 'export PATH=$PATH:$GOPATH/bin' >> "${rc_file}" | |
source "${rc_file}" | |
echo "Go has been added to your PATH." | |
else | |
echo "Go has not been added to your PATH." | |
echo "To use Go, you will need to add it to your PATH manually." | |
fi | |
fi | |
# Check the version of Go | |
if [ -n "${CURRENT_VERSION}" ]; then | |
INSTALLED_VERSION="${CURRENT_VERSION}" | |
else | |
INSTALLED_VERSION=$(go version | awk '{print $3}') | |
fi | |
echo "Installed version: ${INSTALLED_VERSION}" | |
echo "Latest version: ${GO_VERSION}" | |
echo "Go has been updated to the latest version." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment