5
5
from rich .console import Console
6
6
from rich .prompt import Confirm , Prompt
7
7
8
+ from helm_values_manager .errors import ErrorHandler , SchemaError , error_console
8
9
from helm_values_manager .models import SchemaValue , ValueType
9
10
from helm_values_manager .utils import load_schema , save_schema , validate_key_unique , validate_path_format
10
11
@@ -22,15 +23,15 @@ def parse_value_by_type(value_str: str, value_type: ValueType) -> Any:
22
23
return float (value_str )
23
24
return int (value_str )
24
25
except ValueError :
25
- raise typer . BadParameter (f"Invalid number: { value_str } " )
26
+ raise SchemaError (f"Invalid number: { value_str } " )
26
27
elif value_type == "boolean" :
27
28
lower = value_str .lower ()
28
29
if lower in ("true" , "yes" , "y" , "1" ):
29
30
return True
30
31
elif lower in ("false" , "no" , "n" , "0" ):
31
32
return False
32
33
else :
33
- raise typer . BadParameter (f"Invalid boolean: { value_str } " )
34
+ raise SchemaError (f"Invalid boolean: { value_str } " )
34
35
elif value_type == "array" :
35
36
try :
36
37
return json .loads (value_str )
@@ -41,7 +42,7 @@ def parse_value_by_type(value_str: str, value_type: ValueType) -> Any:
41
42
try :
42
43
return json .loads (value_str )
43
44
except json .JSONDecodeError :
44
- raise typer . BadParameter (f"Invalid JSON object: { value_str } " )
45
+ raise SchemaError (f"Invalid JSON object: { value_str } " )
45
46
46
47
47
48
@app .command ("add" )
@@ -52,20 +53,19 @@ def add_command(
52
53
# Load existing schema
53
54
schema = load_schema (schema_path )
54
55
if not schema :
55
- console .print ("[red]Error:[/red] No schema.json found. Run 'init' first." )
56
- raise typer .Exit (1 )
56
+ ErrorHandler .print_error ("schema" , "No schema.json found. Run 'init' first." )
57
57
58
58
console .print ("[bold]Add new value to schema[/bold]\n " )
59
59
60
60
# Prompt for key
61
61
while True :
62
62
key = Prompt .ask ("Key (unique identifier)" )
63
63
if not key :
64
- console .print ("[red]Key cannot be empty[/red]" )
64
+ error_console .print ("[red]Key cannot be empty[/red]" )
65
65
continue
66
66
67
67
if not validate_key_unique (schema , key ):
68
- console .print (f"[red]Key '{ key } ' already exists in schema[/red]" )
68
+ error_console .print (f"[red]Key '{ key } ' already exists in schema[/red]" )
69
69
continue
70
70
71
71
break
@@ -74,7 +74,7 @@ def add_command(
74
74
while True :
75
75
path = Prompt .ask ("Path (dot-separated YAML path)" )
76
76
if not validate_path_format (path ):
77
- console .print ("[red]Invalid path format. Use alphanumeric characters and dots only.[/red]" )
77
+ error_console .print ("[red]Invalid path format. Use alphanumeric characters and dots only.[/red]" )
78
78
continue
79
79
break
80
80
@@ -88,7 +88,7 @@ def add_command(
88
88
value_type = Prompt .ask ("Type" , default = "string" ).lower ()
89
89
if value_type in type_choices :
90
90
break
91
- console .print (f"[red]Invalid type. Choose from: { ', ' .join (type_choices )} [/red]" )
91
+ error_console .print (f"[red]Invalid type. Choose from: { ', ' .join (type_choices )} [/red]" )
92
92
93
93
# Prompt for required
94
94
required = Confirm .ask ("Required?" , default = True )
@@ -101,8 +101,8 @@ def add_command(
101
101
try :
102
102
default = parse_value_by_type (default_str , value_type )
103
103
break
104
- except typer . BadParameter as e :
105
- console .print (f"[red]{ e } [/red]" )
104
+ except SchemaError as e :
105
+ error_console .print (f"[red]{ e } [/red]" )
106
106
107
107
# Prompt for sensitive
108
108
sensitive = Confirm .ask ("Sensitive value?" , default = False )
@@ -139,8 +139,7 @@ def list_command(
139
139
"""List all values in the schema."""
140
140
schema = load_schema (schema_path )
141
141
if not schema :
142
- console .print ("[red]Error:[/red] No schema.json found. Run 'init' first." )
143
- raise typer .Exit (1 )
142
+ ErrorHandler .print_error ("schema" , "No schema.json found. Run 'init' first." )
144
143
145
144
if not schema .values :
146
145
console .print ("No values defined in schema." )
@@ -168,14 +167,12 @@ def get_command(
168
167
"""Show details of a specific schema value."""
169
168
schema = load_schema (schema_path )
170
169
if not schema :
171
- console .print ("[red]Error:[/red] No schema.json found. Run 'init' first." )
172
- raise typer .Exit (1 )
170
+ ErrorHandler .print_error ("schema" , "No schema.json found. Run 'init' first." )
173
171
174
172
# Find the value
175
173
value = next ((v for v in schema .values if v .key == key ), None )
176
174
if not value :
177
- console .print (f"[red]Error:[/red] Value with key '{ key } ' not found" )
178
- raise typer .Exit (1 )
175
+ ErrorHandler .print_error ("schema" , f"Value with key '{ key } ' not found" )
179
176
180
177
# Display details
181
178
console .print (f"\n [bold]{ value .key } [/bold]" )
@@ -196,14 +193,12 @@ def update_command(
196
193
"""Update an existing schema value."""
197
194
schema = load_schema (schema_path )
198
195
if not schema :
199
- console .print ("[red]Error:[/red] No schema.json found. Run 'init' first." )
200
- raise typer .Exit (1 )
196
+ ErrorHandler .print_error ("schema" , "No schema.json found. Run 'init' first." )
201
197
202
198
# Find the value
203
199
value_index = next ((i for i , v in enumerate (schema .values ) if v .key == key ), None )
204
200
if value_index is None :
205
- console .print (f"[red]Error:[/red] Value with key '{ key } ' not found" )
206
- raise typer .Exit (1 )
201
+ ErrorHandler .print_error ("schema" , f"Value with key '{ key } ' not found" )
207
202
208
203
value = schema .values [value_index ]
209
204
console .print (f"[bold]Updating '{ key } '[/bold]\n " )
@@ -213,7 +208,7 @@ def update_command(
213
208
new_path = Prompt .ask (f"Path [{ value .path } ]" , default = value .path )
214
209
if new_path != value .path :
215
210
while not validate_path_format (new_path ):
216
- console .print ("[red]Invalid path format. Use alphanumeric characters and dots only.[/red]" )
211
+ error_console .print ("[red]Invalid path format. Use alphanumeric characters and dots only.[/red]" )
217
212
new_path = Prompt .ask (f"Path [{ value .path } ]" , default = value .path )
218
213
value .path = new_path
219
214
@@ -227,7 +222,7 @@ def update_command(
227
222
new_type = Prompt .ask (f"Type [{ value .type } ]" , default = value .type ).lower ()
228
223
if new_type != value .type :
229
224
while new_type not in type_choices :
230
- console .print (f"[red]Invalid type. Choose from: { ', ' .join (type_choices )} [/red]" )
225
+ error_console .print (f"[red]Invalid type. Choose from: { ', ' .join (type_choices )} [/red]" )
231
226
new_type = Prompt .ask (f"Type [{ value .type } ]" , default = value .type ).lower ()
232
227
value .type = new_type
233
228
# Clear default if type changed
@@ -246,8 +241,8 @@ def update_command(
246
241
try :
247
242
value .default = parse_value_by_type (default_str , value .type )
248
243
break
249
- except typer . BadParameter as e :
250
- console .print (f"[red]{ e } [/red]" )
244
+ except SchemaError as e :
245
+ error_console .print (f"[red]{ e } [/red]" )
251
246
else :
252
247
default_display = json .dumps (value .default ) if value .type in ['array' , 'object' ] else str (value .default )
253
248
console .print (f"\n Current default value: { default_display } " )
@@ -271,13 +266,13 @@ def update_command(
271
266
try :
272
267
value .default = parse_value_by_type (default_str , value .type )
273
268
break
274
- except typer . BadParameter as e :
275
- console .print (f"[red]{ e } [/red]" )
269
+ except SchemaError as e :
270
+ error_console .print (f"[red]{ e } [/red]" )
276
271
break
277
272
elif choice == "3" :
278
273
# Remove default value
279
274
if value .required :
280
- console . print ( "[yellow]Warning:[/yellow] This field is required but will have no default value" )
275
+ ErrorHandler . print_warning ( " This field is required but will have no default value" )
281
276
if not Confirm .ask ("Continue removing default?" , default = False ):
282
277
continue
283
278
@@ -303,14 +298,12 @@ def remove_command(
303
298
"""Remove a value from the schema."""
304
299
schema = load_schema (schema_path )
305
300
if not schema :
306
- console .print ("[red]Error:[/red] No schema.json found. Run 'init' first." )
307
- raise typer .Exit (1 )
301
+ ErrorHandler .print_error ("schema" , "No schema.json found. Run 'init' first." )
308
302
309
303
# Find the value
310
304
value_index = next ((i for i , v in enumerate (schema .values ) if v .key == key ), None )
311
305
if value_index is None :
312
- console .print (f"[red]Error:[/red] Value with key '{ key } ' not found" )
313
- raise typer .Exit (1 )
306
+ ErrorHandler .print_error ("schema" , f"Value with key '{ key } ' not found" )
314
307
315
308
value = schema .values [value_index ]
316
309
0 commit comments