]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
manager: kresctl: schema command added
authorAleš Mrázek <ales.mrazek@nic.cz>
Mon, 12 Sep 2022 08:20:31 +0000 (10:20 +0200)
committerAleš Mrázek <ales.mrazek@nic.cz>
Tue, 10 Jan 2023 18:57:13 +0000 (19:57 +0100)
manager/knot_resolver_manager/cli/cmd/metrics.py
manager/knot_resolver_manager/cli/cmd/schema.py [new file with mode: 0644]

index 73f4cbaf8b2c60b4674a2ccc40e569a8c195ee03..967e79fd8f92f0561f36bb78958713c7efe49213 100644 (file)
@@ -28,7 +28,7 @@ class MetricsCommand(Command):
         response = request("GET", url)
 
         if self.file and response.status == 200:
-            with open(self.file, 'a') as f:
+            with open(self.file, 'w') as f:
                 f.write(response.body)
         else:
             print(response)
diff --git a/manager/knot_resolver_manager/cli/cmd/schema.py b/manager/knot_resolver_manager/cli/cmd/schema.py
new file mode 100644 (file)
index 0000000..bbc1780
--- /dev/null
@@ -0,0 +1,34 @@
+import argparse
+from optparse import Option
+import sys
+from typing import Optional, Tuple, Type
+from knot_resolver_manager.utils.requests import request
+
+from knot_resolver_manager.cli.command import Command, CommandArgs, register_command
+
+
+@register_command
+class SchemaCommand(Command):
+
+    def __init__(self, namespace: argparse.Namespace) -> None:
+        self.file: Optional[str] = namespace.file
+
+        super().__init__(namespace)
+
+    @staticmethod
+    def register_args_subparser(
+        parser: "argparse._SubParsersAction[argparse.ArgumentParser]",
+    ) -> Tuple[argparse.ArgumentParser, "Type[Command]"]:
+        schema = parser.add_parser("schema", help="get JSON schema reprezentation of the configuration")
+        schema.add_argument('file', help="optional, file to export JSON schema to", nargs='?', default=None)
+        return schema, SchemaCommand
+
+    def run(self, args: CommandArgs) -> None:
+        url = f"{args.socket}/schema"
+        response = request("GET", url)
+
+        if self.file and response.status == 200:
+            with open(self.file, 'w') as f:
+                f.write(response.body)
+        else:
+            print(response)