]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
manager: client: exit code on cmd fail
authorAleš Mrázek <ales.mrazek@nic.cz>
Tue, 10 Jan 2023 18:12:45 +0000 (19:12 +0100)
committerAleš Mrázek <ales.mrazek@nic.cz>
Tue, 10 Jan 2023 18:57:14 +0000 (19:57 +0100)
manager/knot_resolver_manager/cli/cmd/config.py
manager/knot_resolver_manager/cli/cmd/convert.py
manager/knot_resolver_manager/cli/cmd/metrics.py
manager/knot_resolver_manager/cli/cmd/reload.py
manager/knot_resolver_manager/cli/cmd/schema.py
manager/knot_resolver_manager/cli/cmd/stop.py
manager/knot_resolver_manager/cli/cmd/validate.py

index 38932d55e1048f5a1315e1a10722ffcea9a078c3..8f2dce8a3e3fa230d808bc4973544c905bdebce9 100644 (file)
@@ -1,5 +1,6 @@
 import argparse
 import json
+import sys
 from enum import Enum
 from typing import Any, Dict, List, Optional, Tuple, Type
 
@@ -205,14 +206,16 @@ class ConfigCommand(Command):
                     new_config = self.value_or_file
 
         response = request(method, url, reformat(new_config, Formats.JSON) if new_config else None)
+
+        if response.status != 200:
+            print(response)
+            sys.exit(1)
+
         print(f"status: {response.status}")
 
-        if self.operation == Operations.GET and response.status == 200:
-            if self.value_or_file:
-                with open(self.value_or_file, "w") as f:
-                    f.write(reformat(response.body, self.format))
-                print(f"saved to: {self.value_or_file}")
-            else:
-                print(reformat(response.body, self.format))
+        if self.operation == Operations.GET and self.value_or_file:
+            with open(self.value_or_file, "w") as f:
+                f.write(reformat(response.body, self.format))
+            print(f"saved to: {self.value_or_file}")
         else:
-            print(response.body)
+            print(reformat(response.body, self.format))
index eda47d7c115b535ded14bf9b8e70817812c3db1f..b78699d94d4505b8d6817f6a9f66326414913a9a 100644 (file)
@@ -1,10 +1,11 @@
 import argparse
+import sys
 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.utils.modeling import try_to_parse
-from knot_resolver_manager.utils.modeling.exceptions import DataParsingError
+from knot_resolver_manager.utils.modeling.exceptions import DataParsingError, DataValidationError
 
 
 @register_command
@@ -47,11 +48,10 @@ class ConvertCommand(Command):
 
         try:
             parsed = try_to_parse(data)
-        except DataParsingError as e:
+            lua = KresConfig(parsed).render_lua()
+        except (DataParsingError, DataValidationError) as e:
             print(e)
-            return
-
-        lua = KresConfig(parsed).render_lua()
+            sys.exit(1)
 
         if self.output_file:
             with open(self.output_file, "w") as f:
index 44c3fb10f44b433ace0a2f9e90d8ecd500929b06..8b9c8028e5dab932fe5b40bc9782dc76ce30226c 100644 (file)
@@ -1,4 +1,5 @@
 import argparse
+import sys
 from typing import List, Optional, Tuple, Type
 
 from knot_resolver_manager.cli.command import Command, CommandArgs, CompWords, register_command
@@ -33,8 +34,12 @@ class MetricsCommand(Command):
         url = f"{args.socket}/metrics"
         response = request("GET", url)
 
-        if self.file and response.status == 200:
-            with open(self.file, "w") as f:
-                f.write(response.body)
+        if response.status == 200:
+            if self.file:
+                with open(self.file, "w") as f:
+                    f.write(response.body)
+            else:
+                print(response.body)
         else:
             print(response)
+            sys.exit(1)
index f3b87cf9a6aca8761a7dc4092fdc03c4165eaf93..0ed903c0f5eb60a5594eb6a6a16a79556a25a17a 100644 (file)
@@ -1,4 +1,5 @@
 import argparse
+import sys
 from typing import List, Tuple, Type
 
 from knot_resolver_manager.cli.command import Command, CommandArgs, CompWords, register_command
@@ -31,3 +32,6 @@ class ReloadCommand(Command):
         url = f"{args.socket}/reload"
         response = request("POST", url)
         print(response)
+
+        if response.status != 200:
+            sys.exit(1)
index 1f086a7e960d499fa8ac1b6948b84681cfbeaf2d..a60f225c80bdda7f718b51013638074b0c508deb 100644 (file)
@@ -1,5 +1,6 @@
 import argparse
 import json
+import sys
 from typing import List, Optional, Tuple, Type
 
 from knot_resolver_manager.cli.command import Command, CommandArgs, CompWords, parser_words, register_command
@@ -43,7 +44,7 @@ class SchemaCommand(Command):
                 schema = response.body
             else:
                 print(response)
-                return
+                sys.exit(1)
         else:
             schema = json.dumps(KresConfig.json_schema(), indent=4)
 
index a0519473588f782bd242cdd9769d2a50277a30c3..471927f6327b2cfe56d035c67374cb05cf30d2b5 100644 (file)
@@ -1,4 +1,5 @@
 import argparse
+import sys
 from typing import List, Tuple, Type
 
 from knot_resolver_manager.cli.command import Command, CommandArgs, CompWords, register_command
@@ -15,6 +16,9 @@ class StopCommand(Command):
         response = request("POST", url)
         print(response)
 
+        if response.status != 200:
+            sys.exit(1)
+
     @staticmethod
     def completion(args: List[str], parser: argparse.ArgumentParser) -> CompWords:
         return {}
index 415286b5a5ae13321a38ab202442c9c0ac62c235..a93e5a9d72ec1c8efd2ac43355edcceb29e57b05 100644 (file)
@@ -1,4 +1,5 @@
 import argparse
+import sys
 from typing import List, Tuple, Type
 
 from knot_resolver_manager.cli.command import Command, CommandArgs, CompWords, register_command
@@ -45,3 +46,4 @@ class ValidateCommand(Command):
             print("config is valid")
         except (DataParsingError, DataValidationError) as e:
             print(e)
+            sys.exit(1)