10
10
from colorama import Fore , Style
11
11
12
12
from 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
14
14
15
15
if t .TYPE_CHECKING :
16
16
import argparse
@@ -33,6 +33,11 @@ def create_fmt_subparser(parser: argparse.ArgumentParser) -> None:
33
33
action = "store_true" ,
34
34
help = "Write formatted configuration back to file" ,
35
35
)
36
+ parser .add_argument (
37
+ "--all" ,
38
+ action = "store_true" ,
39
+ help = "Format all discovered config files (home, config dir, and current dir)" ,
40
+ )
36
41
37
42
38
43
def 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]
118
123
return formatted , changes
119
124
120
125
121
- def format_config_file (
122
- config_file_path_str : str | None ,
126
+ def format_single_config (
127
+ config_file_path : pathlib . Path ,
123
128
write : bool ,
124
- ) -> None :
125
- """Format a vcspull configuration file.
129
+ ) -> bool :
130
+ """Format a single vcspull configuration file.
126
131
127
132
Parameters
128
133
----------
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
131
136
write : bool
132
137
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 ]
159
138
139
+ Returns
140
+ -------
141
+ bool
142
+ True if formatting was successful, False otherwise
143
+ """
160
144
# Check if file exists
161
145
if not config_file_path .exists ():
162
146
log .error (
@@ -167,7 +151,7 @@ def format_config_file(
167
151
config_file_path ,
168
152
Style .RESET_ALL ,
169
153
)
170
- return
154
+ return False
171
155
172
156
# Load existing config
173
157
try :
@@ -177,12 +161,12 @@ def format_config_file(
177
161
"Config file %s is not a valid YAML dictionary." ,
178
162
config_file_path ,
179
163
)
180
- return
164
+ return False
181
165
except Exception :
182
166
log .exception ("Error loading config from %s" , config_file_path )
183
167
if log .isEnabledFor (logging .DEBUG ):
184
168
traceback .print_exc ()
185
- return
169
+ return False
186
170
187
171
# Format the configuration
188
172
formatted_config , change_count = format_config (raw_config )
@@ -196,7 +180,7 @@ def format_config_file(
196
180
config_file_path ,
197
181
Style .RESET_ALL ,
198
182
)
199
- return
183
+ return True
200
184
201
185
# Show what would be changed
202
186
log .info (
@@ -280,6 +264,7 @@ def format_config_file(
280
264
log .exception ("Error saving formatted config to %s" , config_file_path )
281
265
if log .isEnabledFor (logging .DEBUG ):
282
266
traceback .print_exc ()
267
+ return False
283
268
else :
284
269
log .info (
285
270
"\n %s→%s Run with %s--write%s to apply these formatting changes." ,
@@ -288,3 +273,117 @@ def format_config_file(
288
273
Fore .CYAN ,
289
274
Style .RESET_ALL ,
290
275
)
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