From 56da01d1572484512ee07bb7dddfde4714970759 Mon Sep 17 00:00:00 2001 From: Patrick Collins <54278053+PatrickAlphaC@users.noreply.github.com> Date: Wed, 11 Sep 2024 22:44:53 -0400 Subject: [PATCH] refactored finding the version (#30) --- moccasin/__main__.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/moccasin/__main__.py b/moccasin/__main__.py index 8c8773d..5d08dd0 100644 --- a/moccasin/__main__.py +++ b/moccasin/__main__.py @@ -7,6 +7,8 @@ import sys from moccasin.constants.vars import CONFIG_NAME +from importlib import metadata + GAB_CLI_VERSION_STRING = "Moccasin CLI v{}" @@ -401,11 +403,15 @@ def add_network_args_to_parser(parser: argparse.ArgumentParser): def get_version() -> str: - with open( - Path(__file__).resolve().parent.parent.joinpath("pyproject.toml"), "rb" - ) as f: - moccasin_cli_data = tomllib.load(f) - return GAB_CLI_VERSION_STRING.format(moccasin_cli_data["project"]["version"]) + version = metadata.version("moccasin") + # Attempt to parse from `pyproject.toml` if not found + if not version: + with open( + Path(__file__).resolve().parent.parent.joinpath("pyproject.toml"), "rb" + ) as f: + moccasin_cli_data = tomllib.load(f) + version = moccasin_cli_data["project"]["version"] + return GAB_CLI_VERSION_STRING.format(version) def validate_generate_args(args):