Skip to content

Commit

Permalink
feat(anta.cli): Remove --tags from debug commands (#727)
Browse files Browse the repository at this point in the history
  • Loading branch information
gmuloc authored Aug 30, 2024
1 parent 722b3e1 commit aa1fde8
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 24 deletions.
7 changes: 5 additions & 2 deletions anta/cli/debug/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]))
Expand Down
5 changes: 2 additions & 3 deletions anta/cli/debug/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"]),
Expand All @@ -44,7 +44,6 @@ def wrapper(
ctx: click.Context,
*args: tuple[Any],
inventory: AntaInventory,
tags: set[str] | None,
device: str,
**kwargs: Any,
) -> Any:
Expand Down
46 changes: 33 additions & 13 deletions anta/cli/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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,
Expand All @@ -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:
Expand Down Expand Up @@ -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

Expand Down
9 changes: 3 additions & 6 deletions docs/cli/debug.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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
Expand Down

0 comments on commit aa1fde8

Please sign in to comment.