2023-01-27 09:07:32 +07:00
|
|
|
#!/bin/sh -e
|
|
|
|
|
|
|
|
CURRENT_GO_PATH=$(which go)
|
|
|
|
|
|
|
|
if [ -z "$CURRENT_GO_PATH" ]; then
|
|
|
|
echo "Go is not installed!"
|
2023-08-14 10:32:39 +07:00
|
|
|
CURRENT_GO_PATH=$(echo $GOROOT)
|
|
|
|
if [ -z "$CURRENT_GO_PATH" ]; then
|
|
|
|
CURRENT_GO_PATH="/usr/local/go"
|
|
|
|
else
|
|
|
|
CURRENT_GO_PATH="$CURRENT_GO_PATH"
|
|
|
|
fi
|
2023-01-27 09:07:32 +07:00
|
|
|
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"
|
|
|
|
|
2023-05-05 08:26:19 +07:00
|
|
|
#UPDATE_GO_VERSION=${1:-}
|
2023-08-14 10:18:32 +07:00
|
|
|
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"
|
2023-05-05 08:26:19 +07:00
|
|
|
|
2023-08-14 10:18:32 +07:00
|
|
|
# UPDATE_GO_VERSION=$(echo $FETCH_FROM_GO_VERSION | sed 's/^go//')
|
2023-01-27 09:07:32 +07:00
|
|
|
|
|
|
|
if [ -z "$UPDATE_GO_VERSION" ]; then
|
2023-08-14 10:18:32 +07:00
|
|
|
echo "Usage: $0 <go-version> | Example: $0 1.20.0"
|
2023-01-27 09:07:32 +07:00
|
|
|
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
|
|
|
|
|
2023-08-14 10:18:32 +07:00
|
|
|
echo "Thanks for using go-update!"
|