From 0662b930c4d82ff542484ca010f9b9290b283281 Mon Sep 17 00:00:00 2001 From: Sambo Chea Date: Fri, 27 Jan 2023 09:07:32 +0700 Subject: [PATCH] Add 'go-update' --- go-update | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 go-update diff --git a/go-update b/go-update new file mode 100644 index 0000000..05d0072 --- /dev/null +++ b/go-update @@ -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 | 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!" \ No newline at end of file