Skip to content

Instantly share code, notes, and snippets.

@rammanokar
Created November 16, 2020 09:51
Show Gist options
  • Select an option

  • Save rammanokar/17bba14857c58c7e361aa19aca62240e to your computer and use it in GitHub Desktop.

Select an option

Save rammanokar/17bba14857c58c7e361aa19aca62240e to your computer and use it in GitHub Desktop.
Golang installing and updating Script
#!/usr/bin/env bash
download_go() {
wget "https://dl.google.com/go/${1}.linux-amd64.tar.gz" -P /tmp/go/
sudo tar xf "/tmp/go/${1}.linux-amd64.tar.gz" -C /usr/local/
echo 'export PATH="$PATH:/usr/local/go/bin"' | sudo tee /etc/profile.d/go.sh
source /etc/profile.d/go.sh
}
# main starts here
if [ "$EUID" -ne 0 ]; then
echo "Please run as root"
exit
fi
installed_version_str=$(go version 2>/dev/null | grep -Eo 'go[0-9]+.[0-9]+.[0-9]+')
cloud_version_str=$(curl 'https://golang.org/VERSION?m=text')
cloud_version_num=$(echo cloud_version_str | sed 's/[^0-9]*//g')
if [[ -z $installed_version_str ]]; then
printf ">>>>>>>>>>\n\n\ngo is not installed🧨.\n\n Downloading %s\n\n\n" "$cloud_version_str"
download_go "$cloud_version_str"
else
installed_version_num=$("echo ${installed_version_str} | sed 's/[^0-9]*//g'")
if ((installed_version_num < cloud_version_num)) | [[ -z $installed_version_num ]]; then
printf "%s is currently installed, %s version is available\nUpdating go🗂" "$installed_version_str" "$cloud_version_str"
download_go "$cloud_version_str"
elif ((installed_version_num >= cloud_version_num)); then
echo "$installed_version_str is upto date👍"
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment