diff --git a/src/scribe_data/cli/main.py b/src/scribe_data/cli/main.py index 05310f96f..f9bd5b66a 100644 --- a/src/scribe_data/cli/main.py +++ b/src/scribe_data/cli/main.py @@ -28,6 +28,7 @@ from scribe_data.cli.interactive import start_interactive_mode from scribe_data.cli.list import list_wrapper from scribe_data.cli.total import get_total_lexemes +from scribe_data.cli.version import get_version_message LIST_DESCRIPTION = "List languages, data types and combinations of each that Scribe-Data can be used for." GET_DESCRIPTION = ( @@ -50,7 +51,11 @@ def main() -> None: parser._actions[0].help = "Show this help message and exit." parser.add_argument( - "-v", "--version", help="Show the version of the Scribe-Data CLI." + "-v", + "--version", + action="version", + version=f"{get_version_message()}", + help="Show the local and latest versions of the Scribe-Data CLI.", ) parser.add_argument("-u", "--upgrade", help="Upgrade the Scribe-Data CLI.") diff --git a/src/scribe_data/cli/version.py b/src/scribe_data/cli/version.py new file mode 100644 index 000000000..d315a4695 --- /dev/null +++ b/src/scribe_data/cli/version.py @@ -0,0 +1,62 @@ +""" +Functions for checking current version of the Scribe-Data CLI. + +.. raw:: html + +""" + +import pkg_resources +import requests + + +def get_local_version(): + try: + return pkg_resources.get_distribution("scribe-data").version + except pkg_resources.DistributionNotFound: + return "Unknown (Not installed via pip)" + + +def get_latest_version(): + try: + response = requests.get( + "https://api.github.com/repos/scribe-org/Scribe-Data/releases/latest" + ) + version = response.json()["name"] + return version + except Exception: + return "Unknown (Unable to fetch version)" + + +def get_version_message(): + local_version = "Scribe-Data v" + get_local_version() + latest_version = get_latest_version() + + if ( + local_version == "Unknown (Not installed via pip)" + or latest_version == "Unknown (Unable to fetch version)" + ): + return f"{local_version}" + + if local_version == latest_version: + return f"{local_version}" + else: + update_message = ( + f"Scribe-Data v{local_version} (Update available: v{latest_version})\n" + ) + update_message += "To update, run: pip scribe-data --upgrade" + return update_message