Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
gmuloc committed Nov 10, 2023
1 parent f1dda8c commit 51fc7f1
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
15 changes: 9 additions & 6 deletions anta/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from anta.cli.exec import commands as exec_commands
from anta.cli.get import commands as get_commands
from anta.cli.nrfu import commands as nrfu_commands
from anta.cli.utils import AliasedGroup, IgnoreRequiredForMainCommand, IgnoreRequiredWithHelp, parse_catalog, parse_inventory
from anta.cli.utils import AliasedGroup, IgnoreRequiredForMainCommand, IgnoreRequiredWithHelp, maybe_required_cb, parse_catalog, parse_inventory
from anta.logger import setup_logging
from anta.result_manager import ResultManager

Expand All @@ -33,7 +33,8 @@
"--username",
help="Username to connect to EOS",
show_envvar=True,
required=True,
callback=maybe_required_cb,
# required=True,
)
@click.option("--password", help="Password to connect to EOS that must be provided. It can be prompted using '--prompt' option.", show_envvar=True)
@click.option(
Expand Down Expand Up @@ -77,7 +78,8 @@
"-i",
help="Path to the inventory YAML file",
show_envvar=True,
required=True,
callback=maybe_required_cb,
# required=True,
type=click.Path(file_okay=True, dir_okay=False, exists=True, readable=True, path_type=pathlib.Path),
)
@click.option(
Expand Down Expand Up @@ -125,16 +127,17 @@ def anta(
ctx.params["enable_password"] = click.prompt(
"Please enter a password to enter EOS privileged EXEC mode", type=str, hide_input=True, confirmation_prompt=True
)
if ctx.params.get("password") is None:
if ctx.params.get("password") is None and not ctx.obj.get("skip_password"):
raise click.BadParameter(
f"EOS password needs to be provided by using either the '{anta.params[2].opts[0]}' option or the '{anta.params[5].opts[0]}' option."
)
if not ctx.params.get("enable") and ctx.params.get("enable_password"):
raise click.BadParameter(f"Providing a password to access EOS Privileged EXEC mode requires '{anta.params[4].opts[0]}' option.")

ctx.ensure_object(dict)
ctx.obj["inventory"] = parse_inventory(ctx, inventory)
ctx.obj["inventory_path"] = ctx.params["inventory"]
if not ctx.obj.get("skip_inventory"):
ctx.obj["inventory"] = parse_inventory(ctx, inventory)
ctx.obj["inventory_path"] = ctx.params["inventory"]


@anta.group("nrfu", cls=IgnoreRequiredWithHelp)
Expand Down
21 changes: 19 additions & 2 deletions anta/cli/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,24 @@ def parse_catalog(ctx: click.Context, param: Option, value: Path) -> AntaCatalog

def maybe_required_cb(ctx: click.Context, param: Option, value: str) -> Any:
"""
Repace the "required" true
Replace the "required" true with a callback to handle our specificies
TODO: evaluate if moving the options to the groups is not better than this ..
"""
if ctx.obj.get("_anta_help"):
# If help then don't do anything
return
if "get" in ctx.obj["args"]:
# the group has put the args from cli in the ctx.obj
# This is a bit convoluted
ctx.obj["skip_password"] = True
if "from-cvp" in ctx.obj["args"] or "from-ansible" in ctx.obj["args"]:
ctx.obj["skip_inventory"] = True
elif param.name == "inventory" and param.value_is_missing(value):
raise click.exceptions.MissingParameter(ctx=ctx, param=param)
return
if param.value_is_missing(value):
raise click.exceptions.MissingParameter(ctx=ctx, param=param)


def exit_with_code(ctx: click.Context) -> None:
Expand Down Expand Up @@ -215,7 +231,8 @@ def parse_args(self, ctx: click.Context, args: list[str]) -> list[str]:
if "--help" in args:
ctx.obj["_anta_help"] = True

raise Exception(ctx.__dict__)
# Storing full CLI call in ctx to get it in callbacks
ctx.obj["args"] = args

try:
return super().parse_args(ctx, args)
Expand Down

0 comments on commit 51fc7f1

Please sign in to comment.