-
-
Save Danukeru/ddad77d1c16ef894f0a2684c3fb1100f to your computer and use it in GitHub Desktop.
Go Updater (go-updater.sh) - bash script that automatically downloads and installs the latest stable version of Golang
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 | |
| # | |
| # Checks if a new stable version of Go is available. If a new version is available, it will download and install it. | |
| set -o pipefail | |
| ###################################### | |
| # Constants # | |
| ###################################### | |
| OS="linux" | |
| ARCH="amd64" | |
| GO_GIT_URL="https://go.googlesource.com/go" | |
| GO_INSTALL_DIR="/usr/local" | |
| DEPENDENCIES="git,tar,sed,sort,grep,cut,tail,wget" | |
| function is_root() { | |
| if [ "$EUID" -ne 0 ]; then | |
| echo "Please run as root" | |
| exit 3 | |
| fi | |
| } | |
| function dependency_check() { | |
| for dependency in $(echo $DEPENDENCIES | tr "," " "); do | |
| if ! command -v "$dependency" &>/dev/null; then | |
| echo "Dependency '$dependency' not found. Please install it and try again." | |
| exit 2 | |
| fi | |
| done | |
| } | |
| function identify_latest_version() { | |
| latest_version=$(git ls-remote --tags $GO_GIT_URL 2>/dev/null | | |
| sed "s/\t/ /g" | | |
| cut -d ' ' -f 2 | | |
| grep -F "refs/tags/go" | | |
| sed "s/^.*\///g" | | |
| grep -vi rc | | |
| sort -V | | |
| tail -1) 2>/dev/null || exit 1 | |
| echo "$latest_version" && return 0 | |
| } | |
| function download_and_install() { | |
| # Download the latest version of Go | |
| download_url="https://go.dev/dl/$1.$OS-$ARCH.tar.gz" | |
| echo "Downloading Go version $latest_version..." | |
| wget -qO "/tmp/$1.$OS-$ARCH.tar.gz" "$download_url" 2>/dev/null | |
| if [[ $? != 0 ]]; then | |
| echo "error occurred downloading go from '$download_url'." | |
| exit 1 | |
| fi | |
| echo "Installing Go version $latest_version..." | |
| # Remove the old existing Go installation | |
| rm -rf "$GO_INSTALL_DIR/go" 2>/dev/null | |
| # Install the latest version of Go | |
| tar -C "$GO_INSTALL_DIR" -xzf "/tmp/$1.$OS-$ARCH.tar.gz" 2>/dev/null | |
| if [[ $? != 0 ]]; then | |
| echo "error occurred extracting '/tmp/$1.$OS-$ARCH.tar.gz' to '$GO_INSTALL_DIR/go'." | |
| exit 1 | |
| fi | |
| # fix permissions | |
| chmod a+x "$GO_INSTALL_DIR"/go/bin/* | |
| # cleaning up | |
| rm "/tmp/$1.$OS-$ARCH.tar.gz" | |
| if echo "$PATH" | grep -q "$GO_INSTALL_DIR/go/bin"; then | |
| echo "Go version $latest_version installed successfully." | |
| else | |
| echo -e "Go version $latest_version installed successfully.\nPlease add '$GO_INSTALL_DIR/go/bin' to your PATH." | |
| fi | |
| } | |
| is_root | |
| dependency_check | |
| latest_version=$(identify_latest_version) || exit 1 | |
| download_and_install $latest_version |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment