2323from cirro .models .process import PipelineDefinition , ConfigAppStatus , CONFIG_APP_URL
2424from cirro .services .service_helpers import list_all_datasets
2525
26- NO_PROJECTS = "No projects available"
2726# Log to STDOUT
2827log_formatter = logging .Formatter (
2928 '%(asctime)s %(levelname)-8s [Cirro CLI] %(message)s'
3736
3837def run_list_datasets (input_params : ListArguments , interactive = False ):
3938 """List the datasets available in a particular project."""
40- _check_configure ()
41- cirro = CirroApi ()
42- logger .info (f"Collecting data from { cirro .configuration .base_url } " )
43- projects = cirro .projects .list ()
44-
45- if len (projects ) == 0 :
46- raise InputError (NO_PROJECTS )
39+ cirro = _init_cirro_client ()
40+ projects = _get_projects (cirro )
4741
4842 if interactive :
4943 # Prompt the user for the project
@@ -63,17 +57,10 @@ def run_list_datasets(input_params: ListArguments, interactive=False):
6357
6458
6559def run_ingest (input_params : UploadArguments , interactive = False ):
66- _check_configure ()
67- cirro = CirroApi ()
68- logger .info (f"Collecting data from { cirro .configuration .base_url } " )
60+ cirro = _init_cirro_client ()
61+ projects = _get_projects (cirro )
6962 processes = cirro .processes .list (process_type = Executor .INGEST )
7063
71- logger .info ("Listing available projects" )
72- projects = cirro .projects .list ()
73-
74- if len (projects ) == 0 :
75- raise InputError (NO_PROJECTS )
76-
7764 if interactive :
7865 input_params , files = gather_upload_arguments (input_params , projects , processes )
7966 directory = input_params ['data_directory' ]
@@ -121,15 +108,8 @@ def run_ingest(input_params: UploadArguments, interactive=False):
121108
122109
123110def run_validate_folder (input_params : ValidateArguments , interactive = False ):
124- _check_configure ()
125- cirro = CirroApi ()
126- logger .info (f"Collecting data from { cirro .configuration .base_url } " )
127-
128- logger .info ("Listing available projects" )
129- projects = cirro .projects .list ()
130-
131- if len (projects ) == 0 :
132- raise InputError (NO_PROJECTS )
111+ cirro = _init_cirro_client ()
112+ projects = _get_projects (cirro )
133113
134114 if interactive :
135115 input_params = gather_validate_arguments (input_params , projects )
@@ -154,34 +134,27 @@ def run_validate_folder(input_params: ValidateArguments, interactive=False):
154134
155135 logger .info ("Validating files" )
156136
157- validation_results = cirro .datasets .validate_folder (
137+ results = cirro .datasets .validate_folder (
158138 project_id = project_id ,
159139 dataset_id = dataset_id ,
160140 local_folder = input_params ['data_directory' ]
161141 )
162142
163143 for file_list , label , log_level in [
164- (validation_results .files_matching , "✅ Matched Files (identical in Cirro and locally)" , logging .INFO ),
165- (validation_results .files_not_matching , "⚠️ Checksum Mismatches (same file name, different content)" , logging .WARNING ),
166- (validation_results .files_missing , "⚠️ Missing Locally (present in system but not found locally)" , logging .WARNING ),
167- (validation_results .local_only_files , "⚠️ Unexpected Local Files (present locally but not in system)" , logging .WARNING ),
168- (validation_results .validate_errors , "⚠️ Validation Failed (checksums may not be available)" , logging .WARNING )
144+ (results .files_matching , "✅ Matched Files (identical in Cirro and locally)" , logging .INFO ),
145+ (results .files_not_matching , "⚠️ Checksum Mismatches (same file name, different content)" , logging .WARNING ),
146+ (results .files_missing , "⚠️ Missing Locally (present in system but not found locally)" , logging .WARNING ),
147+ (results .local_only_files , "⚠️ Unexpected Local Files (present locally but not in system)" , logging .WARNING ),
148+ (results .validate_errors , "⚠️ Validation Failed (checksums may not be available)" , logging .WARNING )
169149 ]:
170150 logger .log (level = log_level , msg = f"{ label } : { len (file_list ):,} " )
171151 for file in file_list :
172152 logger .log (level = log_level , msg = f" - { file } " )
173153
174154
175155def run_download (input_params : DownloadArguments , interactive = False ):
176- _check_configure ()
177- cirro = CirroApi ()
178- logger .info (f"Collecting data from { cirro .configuration .base_url } " )
179-
180- logger .info ("Listing available projects" )
181- projects = cirro .projects .list ()
182-
183- if len (projects ) == 0 :
184- raise InputError (NO_PROJECTS )
156+ cirro = _init_cirro_client ()
157+ projects = _get_projects (cirro )
185158
186159 files_to_download = None
187160 if interactive :
@@ -229,15 +202,9 @@ def run_download(input_params: DownloadArguments, interactive=False):
229202
230203
231204def run_upload_reference (input_params : UploadReferenceArguments , interactive = False ):
232- _check_configure ()
233- cirro = CirroApi ()
234- logger .info (f"Collecting data from { cirro .configuration .base_url } " )
235-
205+ cirro = _init_cirro_client ()
206+ projects = _get_projects (cirro )
236207 reference_types = cirro .references .get_types ()
237- projects = cirro .projects .list ()
238-
239- if len (projects ) == 0 :
240- raise InputError (NO_PROJECTS )
241208
242209 if interactive :
243210 input_params , files = gather_reference_upload_arguments (input_params , projects , reference_types )
@@ -307,6 +274,21 @@ def run_create_pipeline_config(input_params: CreatePipelineConfigArguments, inte
307274 f"{ CONFIG_APP_URL } " )
308275
309276
277+ def _init_cirro_client ():
278+ _check_configure ()
279+ cirro = CirroApi (user_agent = "Cirro CLI" )
280+ logger .info (f"Collecting data from { cirro .configuration .base_url } " )
281+ return cirro
282+
283+
284+ def _get_projects (cirro : CirroApi ):
285+ logger .info ("Listing available projects" )
286+ projects = cirro .projects .list ()
287+ if len (projects ) == 0 :
288+ raise InputError ("No projects available" )
289+ return projects
290+
291+
310292def _check_configure ():
311293 """
312294 Prompts the user to do initial configuration if needed
0 commit comments