#!/bin/sh -e CURRENT_GO_PATH=$(which go) if [ -z "$CURRENT_GO_PATH" ]; then echo "Go is not installed!" CURRENT_GO_PATH="/usr/local/go" echo "Installing Go in $CURRENT_GO_PATH" else echo "Found Go in $CURRENT_GO_PATH" echo "Updating Go in $CURRENT_GO_PATH" fi BASE_GO_PATH=$(echo $CURRENT_GO_PATH | sed 's/\/bin\/go//g') ROOT_GO_PATH=$(echo $BASE_GO_PATH | sed 's/\/go//g') # echo "Current Go path: $CURRENT_GO_PATH | Base Go path: $BASE_GO_PATH | Root Go path: $ROOT_GO_PATH" #UPDATE_GO_VERSION=${1:-} FETCH_FROM_GO_VERSION=$(curl -s https://go.dev/VERSION?m=text | awk 'NR==1 {sub(/^go/, ""); print}') # Output: # go1.21.0 # time 2023-08-04T20:14:06Z UPDATE_GO_VERSION=$(echo $FETCH_FROM_GO_VERSION) echo "Latest fetched from: https://go.dev/VERSION => $UPDATE_GO_VERSION" # UPDATE_GO_VERSION=$(echo $FETCH_FROM_GO_VERSION | sed 's/^go//') if [ -z "$UPDATE_GO_VERSION" ]; then echo "Usage: $0 | Example: $0 1.20.0" exit 1 fi CURRENT_GO_VERSION=$(go version | awk '{print $3}') echo "Checking version $UPDATE_GO_VERSION in local..." if [ "go$UPDATE_GO_VERSION" = "$CURRENT_GO_VERSION" ]; then echo "Version $UPDATE_GO_VERSION is already installed!" exit 0 else echo "New version $UPDATE_GO_VERSION is available!" fi GO_FILE="go${UPDATE_GO_VERSION}.linux-amd64.tar.gz" GO_DL_URL=https://go.dev/dl/$GO_FILE echo "Downloading last version from $GO_DL_URL" sudo wget $GO_DL_URL -O /tmp/$GO_FILE echo "Remove old version: $CURRENT_GO_VERSION from $BASE_GO_PATH" sudo rm -rf $BASE_GO_PATH echo "Extracting new version: $UPDATE_GO_VERSION" sudo tar -C $ROOT_GO_PATH -xzf /tmp/$GO_FILE echo "Removing downloaded file: $GO_FILE" sudo rm -rf /tmp/$GO_FILE echo "Verifying new version: $UPDATE_GO_VERSION" if [ "go$UPDATE_GO_VERSION" = "$(go version | awk '{print $3}')" ]; then echo "New version $UPDATE_GO_VERSION is installed!" else echo "New version $UPDATE_GO_VERSION is not installed!" fi echo "Thanks for using go-update!"