Skip to content

Instantly share code, notes, and snippets.

@cmndrsp0ck
Created January 7, 2023 05:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cmndrsp0ck/997d121f6426da49e41721db89c90e92 to your computer and use it in GitHub Desktop.
Save cmndrsp0ck/997d121f6426da49e41721db89c90e92 to your computer and use it in GitHub Desktop.
golang update function
function go-update() {
DSTORE=${HOME}/Programs;
CURRENT_INSTALL=$(go version | cut -d" " -f3 | cut -do -f2)
LATEST_STABLE=$(curl -Ss https://go.dev/dl/ | grep -Po '>go.*linux-amd64\.tar\.gz' | cut -d'>' -f2 | head -1);
LATEST_VERSION=$(echo $LATEST_STABLE | grep -Po '[0-9]+\.[0-9]+(\.[0-9]+)?')
if [[ ${CURRENT_INSTALL} == ${LATEST_VERSION} ]]; then
printf "\nLatest stable version already installed. ${LATEST_VERSION}\n"
else
printf "\nCurrently installed version: ${CURRENT_INSTALL}.\nNewer version ${LATEST_VERSION} is available.\n"
read -p "Would you like to install latest version (${LATEST_VERSION})? y/n: "
case "$REPLY" in
Y|y)
mkdir -p ${DSTORE};
curl -Lo ${DSTORE}/${LATEST_STABLE} "https://golang.org/dl/${LATEST_STABLE}";
if [ -d "/usr/local/go" ]; then
sudo rm -rf /usr/local/go;
sudo tar -xzvf ${DSTORE}/${LATEST_STABLE} -C /usr/local/;
else
sudo tar -xzvf ${DSTORE}/${LATEST_STABLE} -C /usr/local/;
fi;
unset DSTORE LATEST_STABLE PKG_FILE
;;
N|n)
printf "\nNow exiting.";
sleep 1;
;;
*)
echo "$REPLY Didn't match anything"
esac
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment