1
1
import argparse
2
- import json
3
2
import sys
4
3
from enum import Enum
5
4
from typing import List , Optional , Tuple , Type
6
5
7
- import yaml
8
6
from typing_extensions import Literal
9
7
10
8
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
12
10
from knot_resolver_manager .utils .requests import request
13
11
14
12
@@ -18,11 +16,6 @@ class Operations(Enum):
18
16
GET = 2
19
17
20
18
21
- class Formats (Enum ):
22
- JSON = 0
23
- YAML = 1
24
-
25
-
26
19
def operation_to_method (operation : Operations ) -> Literal ["PUT" , "GET" , "DELETE" ]:
27
20
if operation == Operations .SET :
28
21
return "PUT"
@@ -31,17 +24,6 @@ def operation_to_method(operation: Operations) -> Literal["PUT", "GET", "DELETE"
31
24
return "GET"
32
25
33
26
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
-
45
27
# def _properties_words(props: Dict[str, Any]) -> CompWords:
46
28
# words: CompWords = {}
47
29
# for name, prop in props.items():
@@ -97,7 +79,7 @@ class ConfigCommand(Command):
97
79
def __init__ (self , namespace : argparse .Namespace ) -> None :
98
80
super ().__init__ (namespace )
99
81
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
101
83
self .operation : Optional [Operations ] = namespace .operation if hasattr (namespace , "operation" ) else None
102
84
self .file : Optional [str ] = namespace .file if hasattr (namespace , "file" ) else None
103
85
@@ -112,7 +94,7 @@ def register_args_subparser(
112
94
113
95
# GET operation
114
96
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 )
116
98
117
99
get .add_argument (
118
100
"-p" ,
@@ -132,15 +114,15 @@ def register_args_subparser(
132
114
get_formats = get .add_mutually_exclusive_group ()
133
115
get_formats .add_argument (
134
116
"--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 ,
137
119
action = "store_const" ,
138
120
dest = "format" ,
139
121
)
140
122
get_formats .add_argument (
141
123
"--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 ,
144
126
action = "store_const" ,
145
127
dest = "format" ,
146
128
)
@@ -172,22 +154,6 @@ def register_args_subparser(
172
154
nargs = "?" ,
173
155
)
174
156
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
-
191
157
# DELETE operation
192
158
delete = config_subparsers .add_parser (
193
159
"delete" , help = "Delete given configuration property or list item at the given index."
@@ -241,15 +207,16 @@ def run(self, args: CommandArgs) -> None:
241
207
# use STDIN also when file is not specified
242
208
new_config = input ("Type new configuration: " )
243
209
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 )
245
212
246
213
if response .status != 200 :
247
214
print (response , file = sys .stderr )
248
215
sys .exit (1 )
249
216
250
217
if self .operation == Operations .GET and self .file :
251
218
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 ))
253
220
print (f"saved to: { self .file } " )
254
221
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