]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
manager: cli: validate: --no-strict arg added
authorAleš Mrázek <ales.mrazek@nic.cz>
Fri, 16 Jun 2023 12:43:53 +0000 (14:43 +0200)
committerAleš Mrázek <ales.mrazek@nic.cz>
Mon, 19 Jun 2023 11:34:30 +0000 (13:34 +0200)
manager/knot_resolver_manager/cli/cmd/convert.py
manager/knot_resolver_manager/cli/cmd/validate.py

index fc09db543574eb178970b8972f4b125f40c2c42b..9c7e5d2a76a842cfa438c4256c31f87106f0ffa5 100644 (file)
@@ -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)
index e77048c792afcc96ee2437e9e62a3896fa003e3e..0770d7573b303eece06745e960d1a13e0cd23318 100644 (file)
@@ -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)