From aa1fde8360d0a9786e42eb35a48c9d006abb4a6e Mon Sep 17 00:00:00 2001 From: Guillaume Mulocher Date: Fri, 30 Aug 2024 17:09:16 +0200 Subject: [PATCH] feat(anta.cli): Remove --tags from debug commands (#727) --- anta/cli/debug/commands.py | 7 ++++-- anta/cli/debug/utils.py | 5 ++--- anta/cli/utils.py | 46 +++++++++++++++++++++++++++----------- docs/cli/debug.md | 9 +++----- 4 files changed, 43 insertions(+), 24 deletions(-) diff --git a/anta/cli/debug/commands.py b/anta/cli/debug/commands.py index 14f168ba4..1304758a4 100644 --- a/anta/cli/debug/commands.py +++ b/anta/cli/debug/commands.py @@ -72,13 +72,16 @@ def run_template( revision: int, ) -> None: # pylint: disable=too-many-arguments + # Using \b for click + # ruff: noqa: D301 """Run arbitrary templated command to an ANTA device. Takes a list of arguments (keys followed by a value) to build a dictionary used as template parameters. - Example: + \b + Example ------- - anta debug run-template -d leaf1a -t 'show vlan {vlan_id}' vlan_id 1 + anta debug run-template -d leaf1a -t 'show vlan {vlan_id}' vlan_id 1 """ template_params = dict(zip(params[::2], params[1::2])) diff --git a/anta/cli/debug/utils.py b/anta/cli/debug/utils.py index 04a7a38b1..4e20c5a74 100644 --- a/anta/cli/debug/utils.py +++ b/anta/cli/debug/utils.py @@ -11,7 +11,7 @@ import click -from anta.cli.utils import ExitCode, inventory_options +from anta.cli.utils import ExitCode, core_options if TYPE_CHECKING: from anta.inventory import AntaInventory @@ -22,7 +22,7 @@ def debug_options(f: Callable[..., Any]) -> Callable[..., Any]: """Click common options required to execute a command on a specific device.""" - @inventory_options + @core_options @click.option( "--ofmt", type=click.Choice(["json", "text"]), @@ -44,7 +44,6 @@ def wrapper( ctx: click.Context, *args: tuple[Any], inventory: AntaInventory, - tags: set[str] | None, device: str, **kwargs: Any, ) -> Any: diff --git a/anta/cli/utils.py b/anta/cli/utils.py index 6d31e55ae..2f6e7d302 100644 --- a/anta/cli/utils.py +++ b/anta/cli/utils.py @@ -112,7 +112,7 @@ def resolve_command(self, ctx: click.Context, args: Any) -> Any: return cmd.name, cmd, args -def inventory_options(f: Callable[..., Any]) -> Callable[..., Any]: +def core_options(f: Callable[..., Any]) -> Callable[..., Any]: """Click common options when requiring an inventory to interact with devices.""" @click.option( @@ -190,22 +190,12 @@ def inventory_options(f: Callable[..., Any]) -> Callable[..., Any]: required=True, type=click.Path(file_okay=True, dir_okay=False, exists=True, readable=True, path_type=Path), ) - @click.option( - "--tags", - help="List of tags using comma as separator: tag1,tag2,tag3.", - show_envvar=True, - envvar="ANTA_TAGS", - type=str, - required=False, - callback=parse_tags, - ) @click.pass_context @functools.wraps(f) def wrapper( ctx: click.Context, *args: tuple[Any], inventory: Path, - tags: set[str] | None, username: str, password: str | None, enable_password: str | None, @@ -219,7 +209,7 @@ def wrapper( # pylint: disable=too-many-arguments # If help is invoke somewhere, do not parse inventory if ctx.obj.get("_anta_help"): - return f(*args, inventory=None, tags=tags, **kwargs) + return f(*args, inventory=None, **kwargs) if prompt: # User asked for a password prompt if password is None: @@ -255,7 +245,37 @@ def wrapper( ) except (TypeError, ValueError, YAMLError, OSError, InventoryIncorrectSchemaError, InventoryRootKeyError): ctx.exit(ExitCode.USAGE_ERROR) - return f(*args, inventory=i, tags=tags, **kwargs) + return f(*args, inventory=i, **kwargs) + + return wrapper + + +def inventory_options(f: Callable[..., Any]) -> Callable[..., Any]: + """Click common options when requiring an inventory to interact with devices.""" + + @core_options + @click.option( + "--tags", + help="List of tags using comma as separator: tag1,tag2,tag3.", + show_envvar=True, + envvar="ANTA_TAGS", + type=str, + required=False, + callback=parse_tags, + ) + @click.pass_context + @functools.wraps(f) + def wrapper( + ctx: click.Context, + *args: tuple[Any], + tags: set[str] | None, + **kwargs: dict[str, Any], + ) -> Any: + # pylint: disable=too-many-arguments + # If help is invoke somewhere, do not parse inventory + if ctx.obj.get("_anta_help"): + return f(*args, tags=tags, **kwargs) + return f(*args, tags=tags, **kwargs) return wrapper diff --git a/docs/cli/debug.md b/docs/cli/debug.md index db5f4961d..376dffb14 100644 --- a/docs/cli/debug.md +++ b/docs/cli/debug.md @@ -52,8 +52,6 @@ Options: ANTA_DISABLE_CACHE] -i, --inventory FILE Path to the inventory YAML file. [env var: ANTA_INVENTORY; required] - --tags TEXT List of tags using comma as separator: - tag1,tag2,tag3. [env var: ANTA_TAGS] --ofmt [json|text] EOS eAPI format to use. can be text or json -v, --version [1|latest] EOS eAPI version -r, --revision INTEGER eAPI command revision @@ -97,8 +95,9 @@ Usage: anta debug run-template [OPTIONS] PARAMS... Takes a list of arguments (keys followed by a value) to build a dictionary used as template parameters. - Example: ------- anta debug run-template -d leaf1a -t 'show vlan {vlan_id}' - vlan_id 1 + Example + ------- + anta debug run-template -d leaf1a -t 'show vlan {vlan_id}' vlan_id 1 Options: -u, --username TEXT Username to connect to EOS [env var: @@ -125,8 +124,6 @@ Options: ANTA_DISABLE_CACHE] -i, --inventory FILE Path to the inventory YAML file. [env var: ANTA_INVENTORY; required] - --tags TEXT List of tags using comma as separator: - tag1,tag2,tag3. [env var: ANTA_TAGS] --ofmt [json|text] EOS eAPI format to use. can be text or json -v, --version [1|latest] EOS eAPI version -r, --revision INTEGER eAPI command revision