1010from colorama import Fore , Style
1111
1212from vcspull ._internal .config_reader import ConfigReader
13- from vcspull .config import find_home_config_files , save_config_yaml
13+ from vcspull .config import find_config_files , find_home_config_files , save_config_yaml
1414
1515if t .TYPE_CHECKING :
1616 import argparse
@@ -33,6 +33,11 @@ def create_fmt_subparser(parser: argparse.ArgumentParser) -> None:
3333 action = "store_true" ,
3434 help = "Write formatted configuration back to file" ,
3535 )
36+ parser .add_argument (
37+ "--all" ,
38+ action = "store_true" ,
39+ help = "Format all discovered config files (home, config dir, and current dir)" ,
40+ )
3641
3742
3843def normalize_repo_config (repo_data : t .Any ) -> dict [str , t .Any ]:
@@ -118,45 +123,24 @@ def format_config(config_data: dict[str, t.Any]) -> tuple[dict[str, t.Any], int]
118123 return formatted , changes
119124
120125
121- def format_config_file (
122- config_file_path_str : str | None ,
126+ def format_single_config (
127+ config_file_path : pathlib . Path ,
123128 write : bool ,
124- ) -> None :
125- """Format a vcspull configuration file.
129+ ) -> bool :
130+ """Format a single vcspull configuration file.
126131
127132 Parameters
128133 ----------
129- config_file_path_str : str | None
130- Path to config file, or None to use default
134+ config_file_path : pathlib.Path
135+ Path to config file
131136 write : bool
132137 Whether to write changes back to file
133- """
134- # Determine config file
135- config_file_path : pathlib .Path
136- if config_file_path_str :
137- config_file_path = pathlib .Path (config_file_path_str ).expanduser ().resolve ()
138- else :
139- home_configs = find_home_config_files (filetype = ["yaml" ])
140- if not home_configs :
141- # Try local .vcspull.yaml
142- local_config = pathlib .Path .cwd () / ".vcspull.yaml"
143- if local_config .exists ():
144- config_file_path = local_config
145- else :
146- log .error (
147- "%s✗%s No configuration file found. Create .vcspull.yaml first." ,
148- Fore .RED ,
149- Style .RESET_ALL ,
150- )
151- return
152- elif len (home_configs ) > 1 :
153- log .error (
154- "Multiple home config files found, please specify one with -c/--config" ,
155- )
156- return
157- else :
158- config_file_path = home_configs [0 ]
159138
139+ Returns
140+ -------
141+ bool
142+ True if formatting was successful, False otherwise
143+ """
160144 # Check if file exists
161145 if not config_file_path .exists ():
162146 log .error (
@@ -167,7 +151,7 @@ def format_config_file(
167151 config_file_path ,
168152 Style .RESET_ALL ,
169153 )
170- return
154+ return False
171155
172156 # Load existing config
173157 try :
@@ -177,12 +161,12 @@ def format_config_file(
177161 "Config file %s is not a valid YAML dictionary." ,
178162 config_file_path ,
179163 )
180- return
164+ return False
181165 except Exception :
182166 log .exception ("Error loading config from %s" , config_file_path )
183167 if log .isEnabledFor (logging .DEBUG ):
184168 traceback .print_exc ()
185- return
169+ return False
186170
187171 # Format the configuration
188172 formatted_config , change_count = format_config (raw_config )
@@ -196,7 +180,7 @@ def format_config_file(
196180 config_file_path ,
197181 Style .RESET_ALL ,
198182 )
199- return
183+ return True
200184
201185 # Show what would be changed
202186 log .info (
@@ -280,6 +264,7 @@ def format_config_file(
280264 log .exception ("Error saving formatted config to %s" , config_file_path )
281265 if log .isEnabledFor (logging .DEBUG ):
282266 traceback .print_exc ()
267+ return False
283268 else :
284269 log .info (
285270 "\n %s→%s Run with %s--write%s to apply these formatting changes." ,
@@ -288,3 +273,117 @@ def format_config_file(
288273 Fore .CYAN ,
289274 Style .RESET_ALL ,
290275 )
276+
277+ return True
278+
279+
280+ def format_config_file (
281+ config_file_path_str : str | None ,
282+ write : bool ,
283+ format_all : bool = False ,
284+ ) -> None :
285+ """Format vcspull configuration file(s).
286+
287+ Parameters
288+ ----------
289+ config_file_path_str : str | None
290+ Path to config file, or None to use default
291+ write : bool
292+ Whether to write changes back to file
293+ format_all : bool
294+ If True, format all discovered config files
295+ """
296+ if format_all :
297+ # Format all discovered config files
298+ config_files = find_config_files (include_home = True )
299+
300+ # Also check for local .vcspull.yaml
301+ local_yaml = pathlib .Path .cwd () / ".vcspull.yaml"
302+ if local_yaml .exists () and local_yaml not in config_files :
303+ config_files .append (local_yaml )
304+
305+ # Also check for local .vcspull.json
306+ local_json = pathlib .Path .cwd () / ".vcspull.json"
307+ if local_json .exists () and local_json not in config_files :
308+ config_files .append (local_json )
309+
310+ if not config_files :
311+ log .error (
312+ "%s✗%s No configuration files found." ,
313+ Fore .RED ,
314+ Style .RESET_ALL ,
315+ )
316+ return
317+
318+ log .info (
319+ "%si%s Found %s%d%s configuration %s to format:" ,
320+ Fore .CYAN ,
321+ Style .RESET_ALL ,
322+ Fore .YELLOW ,
323+ len (config_files ),
324+ Style .RESET_ALL ,
325+ "file" if len (config_files ) == 1 else "files" ,
326+ )
327+
328+ for config_file in config_files :
329+ log .info (
330+ " %s•%s %s%s%s" ,
331+ Fore .BLUE ,
332+ Style .RESET_ALL ,
333+ Fore .CYAN ,
334+ config_file ,
335+ Style .RESET_ALL ,
336+ )
337+
338+ log .info ("" ) # Empty line for readability
339+
340+ success_count = 0
341+ for config_file in config_files :
342+ if format_single_config (config_file , write ):
343+ success_count += 1
344+
345+ # Summary
346+ if success_count == len (config_files ):
347+ log .info (
348+ "\n %s✓%s All %d configuration files processed successfully." ,
349+ Fore .GREEN ,
350+ Style .RESET_ALL ,
351+ len (config_files ),
352+ )
353+ else :
354+ log .info (
355+ "\n %si%s Processed %d/%d configuration files successfully." ,
356+ Fore .CYAN ,
357+ Style .RESET_ALL ,
358+ success_count ,
359+ len (config_files ),
360+ )
361+ else :
362+ # Format single config file
363+ if config_file_path_str :
364+ config_file_path = pathlib .Path (config_file_path_str ).expanduser ().resolve ()
365+ else :
366+ home_configs = find_home_config_files (filetype = ["yaml" ])
367+ if not home_configs :
368+ # Try local .vcspull.yaml
369+ local_config = pathlib .Path .cwd () / ".vcspull.yaml"
370+ if local_config .exists ():
371+ config_file_path = local_config
372+ else :
373+ log .error (
374+ "%s✗%s No configuration file found. "
375+ "Create .vcspull.yaml first." ,
376+ Fore .RED ,
377+ Style .RESET_ALL ,
378+ )
379+ return
380+ elif len (home_configs ) > 1 :
381+ log .error (
382+ "Multiple home config files found, "
383+ "please specify one with -c/--config" ,
384+ )
385+ return
386+ else :
387+ config_file_path = home_configs [0 ]
388+
389+ format_single_config (config_file_path , write )
0 commit comments