From 3a511cd25028acd863e2c0da7387c6fa9dee36db Mon Sep 17 00:00:00 2001 From: =?utf8?q?Ale=C5=A1=20Mr=C3=A1zek?= Date: Fri, 16 Jun 2023 14:43:53 +0200 Subject: [PATCH] manager: cli: validate: --no-strict arg added --- .../knot_resolver_manager/cli/cmd/convert.py | 17 +++++++++++++++-- .../knot_resolver_manager/cli/cmd/validate.py | 17 +++++++++++++++-- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/manager/knot_resolver_manager/cli/cmd/convert.py b/manager/knot_resolver_manager/cli/cmd/convert.py index fc09db543..9c7e5d2a7 100644 --- a/manager/knot_resolver_manager/cli/cmd/convert.py +++ b/manager/knot_resolver_manager/cli/cmd/convert.py @@ -5,7 +5,11 @@ from typing import List, Optional, Tuple, Type from knot_resolver_manager.cli.command import Command, CommandArgs, CompWords, register_command from knot_resolver_manager.datamodel import KresConfig -from knot_resolver_manager.datamodel.globals import Context, set_global_validation_context +from knot_resolver_manager.datamodel.globals import ( + Context, + reset_global_validation_context, + set_global_validation_context, +) from knot_resolver_manager.utils.modeling import try_to_parse from knot_resolver_manager.utils.modeling.exceptions import DataParsingError, DataValidationError @@ -16,12 +20,20 @@ class ConvertCommand(Command): super().__init__(namespace) self.input_file: str = namespace.input_file self.output_file: Optional[str] = namespace.output_file + self.strict: bool = namespace.strict @staticmethod def register_args_subparser( subparser: "argparse._SubParsersAction[argparse.ArgumentParser]", ) -> Tuple[argparse.ArgumentParser, "Type[Command]"]: convert = subparser.add_parser("convert", help="Converts JSON or YAML configuration to Lua script.") + convert.set_defaults(strict=True) + convert.add_argument( + "--no-strict", + help="Ignore strict rules during validation, e.g. path/file existence.", + action="store_false", + dest="strict", + ) convert.add_argument( "input_file", type=str, @@ -48,8 +60,9 @@ class ConvertCommand(Command): try: parsed = try_to_parse(data) - set_global_validation_context(Context(resolve_directory=Path(self.input_file).parent)) + set_global_validation_context(Context(Path(Path(self.input_file).parent), self.strict)) lua = KresConfig(parsed).render_lua() + reset_global_validation_context() except (DataParsingError, DataValidationError) as e: print(e) sys.exit(1) diff --git a/manager/knot_resolver_manager/cli/cmd/validate.py b/manager/knot_resolver_manager/cli/cmd/validate.py index e77048c79..0770d7573 100644 --- a/manager/knot_resolver_manager/cli/cmd/validate.py +++ b/manager/knot_resolver_manager/cli/cmd/validate.py @@ -5,7 +5,11 @@ from typing import List, Tuple, Type from knot_resolver_manager.cli.command import Command, CommandArgs, CompWords, register_command from knot_resolver_manager.datamodel import KresConfig -from knot_resolver_manager.datamodel.globals import Context, set_global_validation_context +from knot_resolver_manager.datamodel.globals import ( + Context, + reset_global_validation_context, + set_global_validation_context, +) from knot_resolver_manager.utils.modeling import try_to_parse from knot_resolver_manager.utils.modeling.exceptions import DataParsingError, DataValidationError @@ -15,12 +19,20 @@ class ValidateCommand(Command): def __init__(self, namespace: argparse.Namespace) -> None: super().__init__(namespace) self.input_file: str = namespace.input_file + self.strict: bool = namespace.strict @staticmethod def register_args_subparser( subparser: "argparse._SubParsersAction[argparse.ArgumentParser]", ) -> Tuple[argparse.ArgumentParser, "Type[Command]"]: validate = subparser.add_parser("validate", help="Validates configuration in JSON or YAML format.") + validate.set_defaults(strict=True) + validate.add_argument( + "--no-strict", + help="Ignore strict rules during validation, e.g. path/file existence.", + action="store_false", + dest="strict", + ) validate.add_argument( "input_file", type=str, @@ -43,8 +55,9 @@ class ValidateCommand(Command): data = input("Type configuration to validate: ") try: - set_global_validation_context(Context(resolve_directory=Path(self.input_file).parent)) + set_global_validation_context(Context(Path(self.input_file).parent, self.strict)) KresConfig(try_to_parse(data)) + reset_global_validation_context() except (DataParsingError, DataValidationError) as e: print(e) sys.exit(1) -- 2.47.2