Add 'go-update'

This commit is contained in:
Sambo Chea 2023-01-27 09:07:32 +07:00
parent 5ee15c3fba
commit 0662b930c4

57
go-update Normal file
View File

@ -0,0 +1,57 @@
#!/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:-}
if [ -z "$UPDATE_GO_VERSION" ]; then
echo "Usage: $0 <go-version> | Example: $0 1.19.5"
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!"