Skip to content

Commit 7451367

Browse files
committed
kresctl: cache command: output formats for 'clear' operation
1 parent b5b10e7 commit 7451367

File tree

1 file changed

+20
-2
lines changed
  • manager/knot_resolver_manager/cli/cmd

1 file changed

+20
-2
lines changed

manager/knot_resolver_manager/cli/cmd/cache.py

+20-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from knot_resolver_manager.cli.command import Command, CommandArgs, CompWords, register_command
77
from knot_resolver_manager.datamodel.cache_schema import CacheClearRPCSchema
88
from knot_resolver_manager.utils.modeling.exceptions import AggregateDataValidationError, DataValidationError
9-
from knot_resolver_manager.utils.modeling.parsing import DataFormat
9+
from knot_resolver_manager.utils.modeling.parsing import DataFormat, parse_json
1010
from knot_resolver_manager.utils.requests import request
1111

1212

@@ -19,6 +19,7 @@ class CacheCommand(Command):
1919
def __init__(self, namespace: argparse.Namespace) -> None:
2020
super().__init__(namespace)
2121
self.operation: Optional[CacheOperations] = namespace.operation if hasattr(namespace, "operation") else None
22+
self.out_format: DataFormat = namespace.out_format if hasattr(namespace, "out_format") else DataFormat.YAML
2223

2324
# CLEAR operation
2425
self.clear_dict: Dict[str, Any] = {}
@@ -70,6 +71,22 @@ def register_args_subparser(
7071
default=None,
7172
)
7273

74+
out_format = clear_subparser.add_mutually_exclusive_group()
75+
out_format.add_argument(
76+
"--json",
77+
help="Set output format in JSON format, default.",
78+
const=DataFormat.JSON,
79+
action="store_const",
80+
dest="format",
81+
)
82+
out_format.add_argument(
83+
"--yaml",
84+
help="Set configuration data in YAML format.",
85+
const=DataFormat.YAML,
86+
action="store_const",
87+
dest="format",
88+
)
89+
7390
return cache_parser, CacheCommand
7491

7592
@staticmethod
@@ -90,8 +107,9 @@ def run(self, args: CommandArgs) -> None:
90107

91108
body: str = DataFormat.JSON.dict_dump(validated.get_unparsed_data())
92109
response = request(args.socket, "POST", "cache/clear", body)
110+
body_dict = parse_json(response.body)
93111

94112
if response.status != 200:
95113
print(response, file=sys.stderr)
96114
sys.exit(1)
97-
print(response.body)
115+
print(self.out_format.dict_dump(body_dict, indent=4))

0 commit comments

Comments
 (0)