fix update bug

This commit is contained in:
Joe Kaufeld 2022-07-29 11:43:40 -04:00
parent c6180948c0
commit e4b2d984ad

View File

@ -6,6 +6,7 @@ import uuid
import click import click
import httpx import httpx
from shiv.bootstrap import current_zipfile
import src import src
from src.helpers import flip_char from src.helpers import flip_char
@ -57,26 +58,27 @@ def beautify(words: list[str]):
@main.command() @main.command()
def update(): def update():
"""Get the newest release from GitHub and install it.""" """Get the newest release from GitHub and install it."""
release_data = httpx.get( response = httpx.get(
"https://api.github.com/repos/itsthejoker/utils/releases/latest" "https://api.github.com/repos/itsthejoker/utils/releases/latest"
) )
if release_data.status_code != 200: if response.status_code != 200:
print( print(
f"Something went wrong when talking to github; got a" f"Something went wrong when talking to github; got a"
f" {release_data.status_code} with the following content:\n" f" {response.status_code} with the following content:\n"
f"{release_data.content}" f"{response.content}"
) )
return return
json_data = release_data.json() release_data = response.json()
if json_data["name"] == src.__version__: if release_data["name"] == src.__version__:
print("Server version is the same as current version; nothing to update.") print("Server version is the same as current version; nothing to update.")
return return
url = json_data["assets"][0]["browser_download_url"] url = release_data["assets"][0]["browser_download_url"]
with open("utils", "wb") as f, httpx.stream("GET", url, follow_redirects=True) as r: with current_zipfile() as archive:
for line in r.iter_bytes(): with open(archive.filename, "wb") as f, httpx.stream("GET", url, follow_redirects=True) as r:
f.write(line) for line in r.iter_bytes():
print(f"Updated to {json_data['name']}! 🎉") f.write(line)
print(f"Updated to {release_data['name']}! 🎉")
if __name__ == "__main__": if __name__ == "__main__":