Skip to content

Instantly share code, notes, and snippets.

@locnnil
Last active July 21, 2023 18:19
Show Gist options
  • Save locnnil/fbaa41a40494fd43435b5bd11f7d0315 to your computer and use it in GitHub Desktop.
Save locnnil/fbaa41a40494fd43435b5bd11f7d0315 to your computer and use it in GitHub Desktop.
Golang updater
#!/bin/bash
set -e
help() {
echo -e "${COLOR['RED']}Usage: sudo $(basename "$0") <go version> <arch> ${COLOR['END']}"
echo -e "${COLOR['RED']}Options: ${COLOR['END']}"
echo -e "${COLOR['RED']} <go version>, The version of GO in format: MAJOR.MINOR.PATCH.${COLOR['END']}"
echo -e "${COLOR['RED']} example: sudo $(basename "$0") 1.20.6 ${COLOR['END']}"
echo -e "${COLOR['RED']} <arch>, The CPU arch (default amd64) ${COLOR['END']}"
}
declare -A COLOR
COLOR['END']='\033[0m'
COLOR['BLACK']='\033[0;30m'
COLOR['RED']='\033[0;31m'
COLOR['GREEN']='\033[0;32m'
COLOR['YELLOW']='\033[0;33m'
COLOR['BLUE']='\033[0;34m'
COLOR['MAGENTA']='\033[0;35m'
COLOR['CYAN']='\033[0;36m'
COLOR['WHITE']='\033[0;37m'
if [[ "$EUID" -ne 0 ]]; then
echo -e "${COLOR['RED']}[ERROR] Run as root: ${COLOR['END']}"
help
exit 1
fi
GOVERSION=""
GOARCH=""
# Check the number of arguments
if [ $# -ge 1 ]; then
# If the first argument exists, print "ok"
GOVERSION=$1
else
# If the first argument does not exist, print a helper message
echo -e "${COLOR['RED']}[ERROR] Please provide the GO version in the first argument. ${COLOR['END']}"
help
exit 1
fi
if [ -n "$2" ]; then
GOARCH=$2
echo -e "${COLOR['GREEN']}[INFO] Chossen architecture: $2 ${COLOR['END']}"
else
GOARCH="amd64"
echo -e "${COLOR['YELLOW']}[WARN] No Architechure provided, using the default arch: $GOARCH ${COLOR['END']}"
fi
pushd /tmp > /dev/null
echo -e "${COLOR['GREEN']}[INFO] Selected version: $GOVERSION ${COLOR['END']}"
echo -e "${COLOR['GREEN']}[INFO] Downloading GO $GOVERSION package... ${COLOR['END']}"
wget https://go.dev/dl/go$GOVERSION.linux-$GOARCH.tar.gz > /dev/null
echo -e "${COLOR['GREEN']}[INFO] Removing current version of GO...${COLOR['END']}"
rm -rf /usr/local/go
echo -e "${COLOR['GREEN']}[INFO] Installing GO $GOVERSION ${COLOR['END']}"
tar -C /usr/local -xzf go$GOVERSION.linux-$GOARCH.tar.gz
popd > /dev/null
export PATH=$PATH:/usr/local/go/bin
echo -e "${COLOR['GREEN']}[INFO] Testing GO $GOVERSION installation ${COLOR['END']}"
go version
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment