diff options
Diffstat (limited to 'anta/cli/__init__.py')
-rw-r--r-- | anta/cli/__init__.py | 75 |
1 files changed, 21 insertions, 54 deletions
diff --git a/anta/cli/__init__.py b/anta/cli/__init__.py index 759194d..b542a6d 100644 --- a/anta/cli/__init__.py +++ b/anta/cli/__init__.py @@ -5,70 +5,37 @@ from __future__ import annotations -import logging -import pathlib import sys +from typing import Callable -import click +from anta import __DEBUG__ -from anta import GITHUB_SUGGESTION, __version__ -from anta.cli.check import check as check_command -from anta.cli.debug import debug as debug_command -from anta.cli.exec import _exec as exec_command -from anta.cli.get import get as get_command -from anta.cli.nrfu import nrfu as nrfu_command -from anta.cli.utils import AliasedGroup, ExitCode -from anta.logger import Log, LogLevel, anta_log_exception, setup_logging +# Note: need to separate this file from _main to be able to fail on the import. +try: + from ._main import anta, cli -logger = logging.getLogger(__name__) +except ImportError as exc: + def build_cli(exception: Exception) -> Callable[[], None]: + """Build CLI function using the caught exception.""" -@click.group(cls=AliasedGroup) -@click.pass_context -@click.version_option(__version__) -@click.option( - "--log-file", - help="Send the logs to a file. If logging level is DEBUG, only INFO or higher will be sent to stdout.", - show_envvar=True, - type=click.Path(file_okay=True, dir_okay=False, writable=True, path_type=pathlib.Path), -) -@click.option( - "--log-level", - "-l", - help="ANTA logging level", - default=logging.getLevelName(logging.INFO), - show_envvar=True, - show_default=True, - type=click.Choice( - [Log.CRITICAL, Log.ERROR, Log.WARNING, Log.INFO, Log.DEBUG], - case_sensitive=False, - ), -) -def anta(ctx: click.Context, log_level: LogLevel, log_file: pathlib.Path) -> None: - """Arista Network Test Automation (ANTA) CLI.""" - ctx.ensure_object(dict) - setup_logging(log_level, log_file) + def wrap() -> None: + """Error message if any CLI dependency is missing.""" + print( + "The ANTA command line client could not run because the required " + "dependencies were not installed.\nMake sure you've installed " + "everything with: pip install 'anta[cli]'" + ) + if __DEBUG__: + print(f"The caught exception was: {exception}") + sys.exit(1) -anta.add_command(nrfu_command) -anta.add_command(check_command) -anta.add_command(exec_command) -anta.add_command(get_command) -anta.add_command(debug_command) + return wrap + cli = build_cli(exc) -def cli() -> None: - """Entrypoint for pyproject.toml.""" - try: - anta(obj={}, auto_envvar_prefix="ANTA") - except Exception as exc: # pylint: disable=broad-exception-caught - anta_log_exception( - exc, - f"Uncaught Exception when running ANTA CLI\n{GITHUB_SUGGESTION}", - logger, - ) - sys.exit(ExitCode.INTERNAL_ERROR) - +__all__ = ["cli", "anta"] if __name__ == "__main__": cli() |