1111from  infrahub_sdk  import  InfrahubClient 
1212from  infrahub_sdk .async_typer  import  AsyncTyper 
1313from  infrahub_sdk .ctl .client  import  initialize_client 
14- from  infrahub_sdk .ctl .exceptions  import  FileNotValidError 
1514from  infrahub_sdk .ctl .utils  import  catch_exception , init_logging 
1615from  infrahub_sdk .queries  import  SCHEMA_HASH_SYNC_STATUS 
17- from  infrahub_sdk .utils  import  find_files 
1816from  infrahub_sdk .yaml  import  SchemaFile 
1917
2018from  .parameters  import  CONFIG_PARAM 
19+ from  .utils  import  load_yamlfile_from_disk_and_exit 
2120
2221app  =  AsyncTyper ()
2322console  =  Console ()
@@ -30,45 +29,6 @@ def callback() -> None:
3029    """ 
3130
3231
33- def  load_schemas_from_disk (schemas : list [Path ]) ->  list [SchemaFile ]:
34-     schemas_data : list [SchemaFile ] =  []
35-     for  schema  in  schemas :
36-         if  schema .is_file ():
37-             schema_file  =  SchemaFile (location = schema )
38-             schema_file .load_content ()
39-             schemas_data .append (schema_file )
40-         elif  schema .is_dir ():
41-             files  =  find_files (extension = ["yaml" , "yml" , "json" ], directory = schema )
42-             for  item  in  files :
43-                 schema_file  =  SchemaFile (location = item )
44-                 schema_file .load_content ()
45-                 schemas_data .append (schema_file )
46-         else :
47-             raise  FileNotValidError (name = schema , message = f"Schema path: { schema }   does not exist!" )
48- 
49-     return  schemas_data 
50- 
51- 
52- def  load_schemas_from_disk_and_exit (schemas : list [Path ]) ->  list [SchemaFile ]:
53-     has_error  =  False 
54-     try :
55-         schemas_data  =  load_schemas_from_disk (schemas = schemas )
56-     except  FileNotValidError  as  exc :
57-         console .print (f"[red]{ exc .message }  " )
58-         raise  typer .Exit (1 ) from  exc 
59- 
60-     for  schema_file  in  schemas_data :
61-         if  schema_file .valid  and  schema_file .content :
62-             continue 
63-         console .print (f"[red]{ schema_file .error_message }   ({ schema_file .location }  )" )
64-         has_error  =  True 
65- 
66-     if  has_error :
67-         raise  typer .Exit (1 )
68- 
69-     return  schemas_data 
70- 
71- 
7232def  validate_schema_content_and_exit (client : InfrahubClient , schemas : list [SchemaFile ]) ->  None :
7333    has_error : bool  =  False 
7434    for  schema_file  in  schemas :
@@ -153,7 +113,7 @@ async def load(
153113
154114    init_logging (debug = debug )
155115
156-     schemas_data  =  load_schemas_from_disk_and_exit ( schemas = schemas )
116+     schemas_data  =  load_yamlfile_from_disk_and_exit ( paths = schemas ,  file_type = SchemaFile ,  console = console )
157117    schema_definition  =  "schema"  if  len (schemas_data ) ==  1  else  "schemas" 
158118    client  =  await  initialize_client ()
159119    validate_schema_content_and_exit (client = client , schemas = schemas_data )
@@ -203,7 +163,7 @@ async def check(
203163
204164    init_logging (debug = debug )
205165
206-     schemas_data  =  load_schemas_from_disk_and_exit ( schemas = schemas )
166+     schemas_data  =  load_yamlfile_from_disk_and_exit ( paths = schemas ,  file_type = SchemaFile ,  console = console )
207167    client  =  await  initialize_client ()
208168    validate_schema_content_and_exit (client = client , schemas = schemas_data )
209169
0 commit comments