Skip to content

Commit 2c6af02

Browse files
committed
kresctl: config: reduction of duplicate code related to the data parsing
- set: there is no need to specify the input data format - get: YAML is now the default format for output data
1 parent 7451367 commit 2c6af02

File tree

1 file changed

+11
-44
lines changed
  • manager/knot_resolver_manager/cli/cmd

1 file changed

+11
-44
lines changed

manager/knot_resolver_manager/cli/cmd/config.py

+11-44
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
import argparse
2-
import json
32
import sys
43
from enum import Enum
54
from typing import List, Optional, Tuple, Type
65

7-
import yaml
86
from typing_extensions import Literal
97

108
from knot_resolver_manager.cli.command import Command, CommandArgs, CompWords, register_command
11-
from knot_resolver_manager.utils.modeling import try_to_parse
9+
from knot_resolver_manager.utils.modeling.parsing import DataFormat, parse_json, try_to_parse
1210
from knot_resolver_manager.utils.requests import request
1311

1412

@@ -18,11 +16,6 @@ class Operations(Enum):
1816
GET = 2
1917

2018

21-
class Formats(Enum):
22-
JSON = 0
23-
YAML = 1
24-
25-
2619
def operation_to_method(operation: Operations) -> Literal["PUT", "GET", "DELETE"]:
2720
if operation == Operations.SET:
2821
return "PUT"
@@ -31,17 +24,6 @@ def operation_to_method(operation: Operations) -> Literal["PUT", "GET", "DELETE"
3124
return "GET"
3225

3326

34-
def reformat(json_str: str, req_format: Formats) -> str:
35-
d = json.loads(json_str)
36-
if req_format == Formats.YAML:
37-
return yaml.dump(d, indent=4)
38-
return json.dumps(d, indent=4)
39-
40-
41-
def json_dump(yaml_or_json_str: str) -> str:
42-
return json.dumps(try_to_parse(yaml_or_json_str))
43-
44-
4527
# def _properties_words(props: Dict[str, Any]) -> CompWords:
4628
# words: CompWords = {}
4729
# for name, prop in props.items():
@@ -97,7 +79,7 @@ class ConfigCommand(Command):
9779
def __init__(self, namespace: argparse.Namespace) -> None:
9880
super().__init__(namespace)
9981
self.path: str = str(namespace.path) if hasattr(namespace, "path") else ""
100-
self.format: Formats = namespace.format if hasattr(namespace, "format") else Formats.JSON
82+
self.format: DataFormat = namespace.format if hasattr(namespace, "format") else DataFormat.JSON
10183
self.operation: Optional[Operations] = namespace.operation if hasattr(namespace, "operation") else None
10284
self.file: Optional[str] = namespace.file if hasattr(namespace, "file") else None
10385

@@ -112,7 +94,7 @@ def register_args_subparser(
11294

11395
# GET operation
11496
get = config_subparsers.add_parser("get", help="Get current configuration from the resolver.")
115-
get.set_defaults(operation=Operations.GET)
97+
get.set_defaults(operation=Operations.GET, format=DataFormat.YAML)
11698

11799
get.add_argument(
118100
"-p",
@@ -132,15 +114,15 @@ def register_args_subparser(
132114
get_formats = get.add_mutually_exclusive_group()
133115
get_formats.add_argument(
134116
"--json",
135-
help="Get configuration data in JSON format, default.",
136-
const=Formats.JSON,
117+
help="Get configuration data in JSON format.",
118+
const=DataFormat.JSON,
137119
action="store_const",
138120
dest="format",
139121
)
140122
get_formats.add_argument(
141123
"--yaml",
142-
help="Get configuration data in YAML format.",
143-
const=Formats.YAML,
124+
help="Get configuration data in YAML format, default.",
125+
const=DataFormat.YAML,
144126
action="store_const",
145127
dest="format",
146128
)
@@ -172,22 +154,6 @@ def register_args_subparser(
172154
nargs="?",
173155
)
174156

175-
set_formats = set.add_mutually_exclusive_group()
176-
set_formats.add_argument(
177-
"--json",
178-
help="Set configuration data in JSON format, default.",
179-
const=Formats.JSON,
180-
action="store_const",
181-
dest="format",
182-
)
183-
set_formats.add_argument(
184-
"--yaml",
185-
help="Set configuration data in YAML format.",
186-
const=Formats.YAML,
187-
action="store_const",
188-
dest="format",
189-
)
190-
191157
# DELETE operation
192158
delete = config_subparsers.add_parser(
193159
"delete", help="Delete given configuration property or list item at the given index."
@@ -241,15 +207,16 @@ def run(self, args: CommandArgs) -> None:
241207
# use STDIN also when file is not specified
242208
new_config = input("Type new configuration: ")
243209

244-
response = request(args.socket, method, path, json_dump(new_config) if new_config else None)
210+
body = DataFormat.JSON.dict_dump(try_to_parse(new_config)) if new_config else None
211+
response = request(args.socket, method, path, body)
245212

246213
if response.status != 200:
247214
print(response, file=sys.stderr)
248215
sys.exit(1)
249216

250217
if self.operation == Operations.GET and self.file:
251218
with open(self.file, "w") as f:
252-
f.write(reformat(response.body, self.format))
219+
f.write(self.format.dict_dump(parse_json(response.body), indent=4))
253220
print(f"saved to: {self.file}")
254221
elif response.body:
255-
print(reformat(response.body, self.format))
222+
print(self.format.dict_dump(parse_json(response.body), indent=4))

0 commit comments

Comments
 (0)