Skip to content

Commit b482b3b

Browse files
authored
feat: add version checker (#14)
2 parents e6e41c3 + 1109dfc commit b482b3b

2 files changed

Lines changed: 40 additions & 0 deletions

File tree

labctl/core/api.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,27 @@ def put(self, path: str, data: dict = {}, json: dict = {}, additional_headers: d
4747
if json:
4848
return requests.put(self.api_url + path, headers=headers, json=json)
4949
return requests.put(self.api_url + path, headers=headers)
50+
51+
@staticmethod
52+
def get_latest_version() -> str | None:
53+
"""
54+
Get the latest version of labctl from pypi or github
55+
Returns None if both github and pypi requests fail
56+
"""
57+
print("Checking for updates")
58+
try:
59+
return get_latest_version_from_github()
60+
except Exception:
61+
print("Failed to get version from github")
62+
pass
63+
try:
64+
return get_latest_version_from_pypi()
65+
except Exception:
66+
print("Failed to get version from pypi")
67+
return None
68+
69+
def get_latest_version_from_pypi() -> str:
70+
return requests.get("https://pypi.org/pypi/labctl/json", timeout=5).json()["info"]["version"]
71+
72+
def get_latest_version_from_github() -> str:
73+
return requests.get("https://api.github.com/repos/laboinfra/labctl/releases/latest", timeout=5).json()["tag_name"]

labctl/main.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,26 @@ def version():
3030
"""
3131
Print the version
3232
"""
33+
latest_version = APIDriver.get_latest_version()
3334
version = __version__
35+
dev_version = False
3436
if version == "0.0.0":
3537
version = "dev or installed from source"
38+
dev_version = True
3639
console.print(f"labctl version: {version} :rocket:")
40+
if not latest_version:
41+
latest_version = "Could not fetch latest version"
42+
console.print(f"Latest version: {latest_version}")
43+
return
44+
# remove v prefix
45+
if version != latest_version.replace("v", "") and not dev_version:
46+
console.print(f"[red]:warning: Your version is outdated :warning:[/red]")
47+
console.print(f"Latest version: [green]{latest_version.replace('v', '')}[/green]")
48+
console.print(f"Your version: [red]{version}[/red]")
49+
console.print(f"Please update your client to avoid issues")
50+
if dev_version:
51+
console.print(f"Dev version has no check intgration for updates please check manually")
52+
console.print(f"https://github.com/laboinfra/labctl/releases")
3753

3854
@app.command()
3955
@cli_ready

0 commit comments

Comments
 (0)