Skip to content

Commit

Permalink
Do not prompt password when using "--help" option
Browse files Browse the repository at this point in the history
  • Loading branch information
mtache committed Aug 4, 2023
1 parent 039dfc9 commit 74726b7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
6 changes: 3 additions & 3 deletions anta/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,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 check_commands
from anta.cli.utils import IgnoreRequiredWithHelp, parse_catalog, parse_inventory, prompt_enable_password, setup_logging
from anta.cli.utils import IgnoreRequiredWithHelp, parse_catalog, parse_inventory, prompt_password, prompt_enable_password, setup_logging
from anta.result_manager.models import TestResult


Expand All @@ -29,11 +29,11 @@
help="Username to connect to EOS",
required=True,
)
@click.password_option(
@click.option(
"--password",
show_envvar=True,
help="Password to connect to EOS",
prompt="Please enter a password to connect to EOS",
callback=prompt_password
)
@click.option(
"--enable",
Expand Down
18 changes: 18 additions & 0 deletions anta/cli/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,28 @@ def parse_tags(ctx: click.Context, param: Option, value: str) -> Optional[List[s
return None


def prompt_password(ctx: click.Context, param: Option, value: Optional[str]) -> Optional[str]:
# pylint: disable=unused-argument
"""
Click option callback to ensure that enable is True when the option is set
"""
if ctx.obj.get("_anta_help"):
# Currently looking for help for a subcommand so no
# need to prompt the password
return None
if value is None:
return click.prompt("Please enter a password to connect to EOS", type=str, hide_input=True, confirmation_prompt=True)
return value


def prompt_enable_password(ctx: click.Context, param: Option, value: Optional[str]) -> Optional[str]:
"""
Click option callback to ensure that enable is True when the option is set
"""
if ctx.obj.get("_anta_help"):
# Currently looking for help for a subcommand so no
# need to prompt the password
return None
if value is not None and ctx.params.get("enable") is not True:
raise click.BadParameter(f"'{param.opts[0]}' requires '--enable'")
if value is None and ctx.params.get("enable") is True:
Expand Down

0 comments on commit 74726b7

Please sign in to comment.