From 1109dfc569bd0e6adf33844d682705603dd96954 Mon Sep 17 00:00:00 2001 From: Gabin L Date: Sun, 2 Mar 2025 12:03:49 +0100 Subject: [PATCH] feat: add version checker Signed-off-by: Gabin L --- labctl/core/api.py | 24 ++++++++++++++++++++++++ labctl/main.py | 16 ++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/labctl/core/api.py b/labctl/core/api.py index d22c558..80e9b1c 100644 --- a/labctl/core/api.py +++ b/labctl/core/api.py @@ -47,3 +47,27 @@ def put(self, path: str, data: dict = {}, json: dict = {}, additional_headers: d if json: return requests.put(self.api_url + path, headers=headers, json=json) return requests.put(self.api_url + path, headers=headers) + + @staticmethod + def get_latest_version() -> str | None: + """ + Get the latest version of labctl from pypi or github + Returns None if both github and pypi requests fail + """ + print("Checking for updates") + try: + return get_latest_version_from_github() + except Exception: + print("Failed to get version from github") + pass + try: + return get_latest_version_from_pypi() + except Exception: + print("Failed to get version from pypi") + return None + +def get_latest_version_from_pypi() -> str: + return requests.get("https://pypi.org/pypi/labctl/json", timeout=5).json()["info"]["version"] + +def get_latest_version_from_github() -> str: + return requests.get("https://api.github.com/repos/laboinfra/labctl/releases/latest", timeout=5).json()["tag_name"] diff --git a/labctl/main.py b/labctl/main.py index 2444869..5453af8 100644 --- a/labctl/main.py +++ b/labctl/main.py @@ -30,10 +30,26 @@ def version(): """ Print the version """ + latest_version = APIDriver.get_latest_version() version = __version__ + dev_version = False if version == "0.0.0": version = "dev or installed from source" + dev_version = True console.print(f"labctl version: {version} :rocket:") + if not latest_version: + latest_version = "Could not fetch latest version" + console.print(f"Latest version: {latest_version}") + return + # remove v prefix + if version != latest_version.replace("v", "") and not dev_version: + console.print(f"[red]:warning: Your version is outdated :warning:[/red]") + console.print(f"Latest version: [green]{latest_version.replace('v', '')}[/green]") + console.print(f"Your version: [red]{version}[/red]") + console.print(f"Please update your client to avoid issues") + if dev_version: + console.print(f"Dev version has no check intgration for updates please check manually") + console.print(f"https://github.com/laboinfra/labctl/releases") @app.command() @cli_ready