Skip to content

Instantly share code, notes, and snippets.

@Sevlak
Last active August 21, 2023 17:40
Show Gist options
  • Save Sevlak/f5261c2fdd0ef67cfba81cdfe56d463b to your computer and use it in GitHub Desktop.
Save Sevlak/f5261c2fdd0ef67cfba81cdfe56d463b to your computer and use it in GitHub Desktop.
Simple Golang update script
from bs4 import BeautifulSoup
import requests
import regex as re
GO_URL = "https://go.dev/dl"
print("Checking latest Go version")
response = requests.get(GO_URL)
soup = BeautifulSoup(response.text, 'html.parser')
last_version = soup.find(id=re.compile("go\d[\.\d]{2,}"))['id']
print(f'Latest version is {last_version}')
filename = last_version + ".linux-amd64.tar.gz"
print("Requesting .tar.gz file")
with open(filename, "wb") as file:
with requests.get(f'{GO_URL + "/" + filename}', stream=True, allow_redirects=True) as resp:
for chunk in resp.iter_content(chunk_size = 512):
if chunk:
file.write(chunk)
print("Finished downloading. Saving file...")
beautifulsoup4==4.12.2
regex==2023.8.8
requests==2.31.0
pip install -r requirements.txt
if [ $? -ne 0 ]; then
echo "pip not installed. Please, install pip and try again"
exit 1
fi
python3 download_go.py
echo "Removing old Go installation and unpacking Go .tar.gz file"
sudo rm -rf /usr/local/go
sudo tar -C /usr/local -xzf $(ls | grep go[1-9] | tr -d '\r')
if ! grep -o -q /usr/local/go/bin <<< $PATH ; then
echo "Go not on PATH, adding to it"
echo export PATH='$PATH':/usr/local/go/bin >> $HOME/.profile
source $HOME/.profile
fi
echo "Installed!"
go version
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment