@@ -98,6 +98,12 @@ fn main() {
9898        . subcommand ( 
9999            Command :: new ( "indexer" ) 
100100                . about ( "Add indexer support for an alto chain." ) 
101+                 . arg ( 
102+                     Arg :: new ( "count" ) 
103+                         . long ( "count" ) 
104+                         . required ( true ) 
105+                         . value_parser ( value_parser ! ( usize ) ) , 
106+                 ) 
101107                . arg ( 
102108                    Arg :: new ( "dir" ) 
103109                        . long ( "dir" ) 
@@ -276,6 +282,8 @@ fn generate(sub_matches: &ArgMatches) {
276282
277283fn  indexer ( sub_matches :  & ArgMatches )  { 
278284    // Extract arguments 
285+     let  count = * sub_matches. get_one :: < usize > ( "count" ) . unwrap ( ) ; 
286+     assert ! ( count > 0 ,  "count must be greater than zero" ) ; 
279287    let  dir = sub_matches. get_one :: < String > ( "dir" ) . unwrap ( ) . clone ( ) ; 
280288    let  url = sub_matches. get_one :: < String > ( "url" ) . unwrap ( ) . clone ( ) ; 
281289
@@ -290,56 +298,68 @@ fn indexer(sub_matches: &ArgMatches) {
290298        std:: process:: exit ( 1 ) ; 
291299    } 
292300
293-     // Iterate over all peer configuration files and add indexer URL 
301+     // Collect and sort file paths 
302+     let  mut  file_paths = Vec :: new ( ) ; 
294303    for  entry in  fs:: read_dir ( & dir) . unwrap ( )  { 
295304        let  entry = entry. unwrap ( ) ; 
296305        let  path = entry. path ( ) ; 
297306        if  path. is_file ( )  { 
298307            if  let  Some ( file_name)  = path. file_name ( ) . and_then ( |s| s. to_str ( ) )  { 
299308                if  file_name. ends_with ( ".yaml" )  && file_name != "config.yaml"  { 
300-                     let  relative_path = path. strip_prefix ( & dir) . unwrap ( ) ; 
301-                     match  fs:: read_to_string ( & path)  { 
302-                         Ok ( content)  => match  serde_yaml:: from_str :: < Config > ( & content)  { 
303-                             Ok ( mut  config)  => { 
304-                                 config. indexer  = Some ( url. clone ( ) ) ; 
305-                                 match  serde_yaml:: to_string ( & config)  { 
306-                                     Ok ( updated_content)  => { 
307-                                         if  let  Err ( e)  = fs:: write ( & path,  updated_content)  { 
308-                                             error ! ( 
309-                                                 path = ?relative_path, 
310-                                                 error = ?e, 
311-                                                 "failed to write" , 
312-                                             ) ; 
313-                                         }  else  { 
314-                                             info ! ( path = ?relative_path,  "updated" ) ; 
315-                                         } 
316-                                     } 
317-                                     Err ( e)  => { 
318-                                         error ! ( 
319-                                             path = ?relative_path, 
320-                                             error = ?e, 
321-                                             "failed to serialize config" , 
322-                                         ) ; 
323-                                     } 
324-                                 } 
325-                             } 
326-                             Err ( e)  => { 
309+                     file_paths. push ( path) ; 
310+                 } 
311+             } 
312+         } 
313+     } 
314+     file_paths. sort ( ) ; 
315+ 
316+     // Iterate over sorted file paths and add indexer URL 
317+     let  mut  applied = 0 ; 
318+     for  path in  file_paths { 
319+         if  applied >= count { 
320+             break ; 
321+         } 
322+         let  relative_path = path. strip_prefix ( & dir) . unwrap ( ) ; 
323+         match  fs:: read_to_string ( & path)  { 
324+             Ok ( content)  => match  serde_yaml:: from_str :: < Config > ( & content)  { 
325+                 Ok ( mut  config)  => { 
326+                     config. indexer  = Some ( url. clone ( ) ) ; 
327+                     match  serde_yaml:: to_string ( & config)  { 
328+                         Ok ( updated_content)  => { 
329+                             if  let  Err ( e)  = fs:: write ( & path,  updated_content)  { 
327330                                error ! ( 
328331                                    path = ?relative_path, 
329332                                    error = ?e, 
330-                                     "failed to parse"  
333+                                     "failed to write"  , 
331334                                ) ; 
335+                             }  else  { 
336+                                 info ! ( path = ?relative_path,  "updated" ) ; 
337+                                 applied += 1 ; 
332338                            } 
333-                         } , 
339+                         } 
334340                        Err ( e)  => { 
335341                            error ! ( 
336342                                path = ?relative_path, 
337343                                error = ?e, 
338-                                 "failed to read " , 
344+                                 "failed to serialize config " , 
339345                            ) ; 
340346                        } 
341347                    } 
342348                } 
349+                 Err ( e)  => { 
350+                     error ! ( 
351+                         path = ?relative_path, 
352+                         error = ?e, 
353+                         "failed to parse" 
354+                     ) ; 
355+                 } 
356+             } , 
357+             Err ( e)  => { 
358+                 error ! ( 
359+                     path = ?relative_path, 
360+                     error = ?e, 
361+                     "failed to read" , 
362+                 ) ; 
343363            } 
344364        } 
345365    } 
0 commit comments