]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
manager: kresctl: config: better reformating
authorAleš Mrázek <ales.mrazek@nic.cz>
Fri, 10 Mar 2023 11:54:37 +0000 (12:54 +0100)
committerAleš Mrázek <ales.mrazek@nic.cz>
Fri, 10 Mar 2023 11:54:37 +0000 (12:54 +0100)
manager/knot_resolver_manager/cli/cmd/config.py

index 33e7ea32cc8910680122387d5794c15ca26888fb..28994d8bc6211ae7372160375cbede8972ce69cc 100644 (file)
@@ -31,12 +31,15 @@ def operation_to_method(operation: Operations) -> Literal["PUT", "GET", "DELETE"
     return "GET"
 
 
-def reformat(data: str, req_format: Formats) -> str:
-    dict = try_to_parse(data)
-
+def reformat(json_str: str, req_format: Formats) -> str:
+    d = json.loads(json_str)
     if req_format == Formats.YAML:
-        return yaml.dump(dict, indent=4)
-    return json.dumps(dict, indent=4)
+        return yaml.dump(d, indent=4)
+    return json.dumps(d, indent=4)
+
+
+def json_dump(yaml_or_json_str: str) -> str:
+    return json.dumps(try_to_parse(yaml_or_json_str))
 
 
 # def _properties_words(props: Dict[str, Any]) -> CompWords:
@@ -238,7 +241,7 @@ class ConfigCommand(Command):
                 # use STDIN also when file is not specified
                 new_config = input("Type new configuration: ")
 
-        response = request(method, url, reformat(new_config, Formats.JSON) if new_config else None)
+        response = request(method, url, json_dump(new_config) if new_config else None)
 
         if response.status != 200:
             print(response)
@@ -248,5 +251,5 @@ class ConfigCommand(Command):
             with open(self.file, "w") as f:
                 f.write(reformat(response.body, self.format))
             print(f"saved to: {self.file}")
-        else:
+        elif response.body:
             print(reformat(response.body, self.format))