#!/bin/bash
# Current version
currentGoVersion=$(go version | cut -f3 -d ' ')
if [ -z "$currentGoVersion" ]; then # Check if it's empty
echo "Find current Go version: [FAIL]"
exit 1
else
echo "Find current Go version: [OK]"
fi
# Find latest version
URL=$(curl -ks https://golang.org/dl/ | grep downloadBox | grep linux-amd64.tar.gz | awk -F 'href="' '{ print $2 }' | cut -f1 -d '"')
if [ -z "$URL" ]; then # Check if the variable $URL is empty
echo "Parse latest Go tarball link: [FAIL]"
exit 1
else
echo "Parse latest Go tarball link: [OK]"
fi
# Check if the local version is the same as the latest one
if echo "$URL" | grep "$currentGoVersion" &> /dev/null; then
echo "You have the latest version already installed"
exit 0
fi
if wget --output-document=latest-go.tar.gz "$URL" &> /dev/null; then
echo "Download latest Go: [OK]"
else
echo "Download latest Go: [FAIL]"
exit 1
fi
# Uninstall the current Go
if sudo rm -rf /usr/local/go &> /dev/null; then
echo "Uninstall current Go: [OK]"
else
echo "Uninstall current Go: [FAIL]"
exit 1
fi
# Install
if sudo tar -C /usr/local -xzf latest-go.tar.gz &> /dev/null; then
echo "Install latest Go: [OK]"
else
echo "Install latest Go: [FAIL]"
exit 1
fi
# Cleanup
if rm latest-go.tar.gz &> /dev/null; then
echo "Clean temporary files: [OK]"
else
echo "Clean temporary files: [FAIL]"
exit 1
fi
Comments