11use std:: io:: { self , Write } ;
22
3- use crate :: cache:: { build_cache, load_cache, store_cache} ;
3+ use crate :: cache:: { build_cache, load_cache, store_cache, sync_cache } ;
44use crate :: common:: find_files;
5- use crate :: types:: { CacheEncoding , CodeownersEntry , OutputFormat } ;
5+ use crate :: parse:: parse_repo;
6+ use crate :: types:: { CacheEncoding , CodeownersCache , CodeownersEntry , OutputFormat } ;
67
78use utils:: app_config:: AppConfig ;
89use utils:: error:: Result ;
@@ -21,10 +22,18 @@ pub fn codeowners_parse(
2122) -> Result < ( ) > {
2223 println ! ( "Parsing CODEOWNERS files at {}" , path. display( ) ) ;
2324
24- let codeowners_files = crate :: common:: find_codeowners_files ( path) ?;
25+ let cache_file = match cache_file {
26+ Some ( file) => path. join ( file) ,
27+ None => {
28+ let config = utils:: app_config:: AppConfig :: fetch ( ) ?;
29+ path. join ( config. cache_file )
30+ }
31+ } ;
2532
26- //dbg!(&codeowners_files);
33+ // Collect all CODEOWNERS files in the specified path
34+ let codeowners_files = crate :: common:: find_codeowners_files ( path) ?;
2735
36+ // Parse each CODEOWNERS file and collect entries
2837 let parsed_codeowners: Vec < CodeownersEntry > = codeowners_files
2938 . iter ( )
3039 . filter_map ( |file| {
@@ -34,18 +43,17 @@ pub fn codeowners_parse(
3443 . flatten ( )
3544 . collect ( ) ;
3645
37- //dbg!(&parsed_codeowners);
38-
46+ // Collect all files in the specified path
3947 let files = find_files ( path) ?;
4048
41- //dbg!(& files);
49+ // Build the cache from the parsed CODEOWNERS entries and the files
4250 let cache = build_cache ( parsed_codeowners, files) ?;
4351
44- store_cache ( & cache, cache_file. unwrap ( ) , encoding) ?;
45-
46- let cache = load_cache ( cache_file. unwrap ( ) ) ?;
52+ // Store the cache in the specified file
53+ store_cache ( & cache, & cache_file, encoding) ?;
4754
48- dbg ! ( cache) ;
55+ // Test the cache by loading it back
56+ let _cache = load_cache ( & cache_file) ?;
4957
5058 println ! ( "CODEOWNERS parsing completed successfully" ) ;
5159
@@ -54,33 +62,14 @@ pub fn codeowners_parse(
5462
5563/// Find and list files with their owners based on filter criteria
5664pub fn codeowners_list_files (
57- path : Option < & std:: path:: Path > , tags : Option < & str > , owners : Option < & str > , unowned : bool ,
58- format : & OutputFormat ,
65+ repo : Option < & std:: path:: Path > , tags : Option < & str > , owners : Option < & str > , unowned : bool ,
66+ format : & OutputFormat , cache_file : Option < & std :: path :: Path > ,
5967) -> Result < ( ) > {
60- let path_str = path. map_or ( "." . into ( ) , |p| p. display ( ) . to_string ( ) ) ;
61- info ! ( "Listing files in {}" , path_str) ;
62- info ! ( "Tags filter: {:?}" , tags) ;
63- info ! ( "Owners filter: {:?}" , owners) ;
64- info ! ( "Unowned only: {}" , unowned) ;
65- info ! ( "Output format: {}" , format) ;
66-
67- // Determine the cache file path based on repository path
68- let repo_path = path. unwrap_or_else ( || std:: path:: Path :: new ( "." ) ) ;
69- //let config = utils::app_config::AppConfig::fetch()?;
70- // let cache_dir = config
71- // .cache_dir
72- // .unwrap_or_else(|| repo_path.join(".codeowners.cache"));
73- let cache_file = repo_path. join ( ".codeowners.cache" ) ;
74-
75- if !cache_file. exists ( ) {
76- return Err ( utils:: error:: Error :: new ( & format ! (
77- "Cache file not found at {}. Please run 'codeowners parse' first." ,
78- cache_file. display( )
79- ) ) ) ;
80- }
68+ // Repository path
69+ let repo = repo. unwrap_or_else ( || std:: path:: Path :: new ( "." ) ) ;
8170
8271 // Load the cache
83- let cache = load_cache ( & cache_file) ?;
72+ let cache = sync_cache ( repo , cache_file) ?;
8473
8574 // Filter files based on criteria
8675 let filtered_files = cache
@@ -179,23 +168,14 @@ pub fn codeowners_list_files(
179168}
180169
181170/// Display aggregated owner statistics and associations
182- pub fn codeowners_list_owners ( path : Option < & std:: path:: Path > , format : & OutputFormat ) -> Result < ( ) > {
183- info ! ( "Listing owners" ) ;
184- info ! ( "Output format: {}" , format) ;
185-
186- // Determine the cache file path based on repository path
187- let repo_path = path. unwrap_or_else ( || std:: path:: Path :: new ( "." ) ) ;
188- let cache_file = repo_path. join ( ".codeowners.cache" ) ;
189-
190- if !cache_file. exists ( ) {
191- return Err ( utils:: error:: Error :: new ( & format ! (
192- "Cache file not found at {}. Please run 'codeowners parse' first." ,
193- cache_file. display( )
194- ) ) ) ;
195- }
171+ pub fn codeowners_list_owners (
172+ repo : Option < & std:: path:: Path > , format : & OutputFormat , cache_file : Option < & std:: path:: Path > ,
173+ ) -> Result < ( ) > {
174+ // Repository path
175+ let repo = repo. unwrap_or_else ( || std:: path:: Path :: new ( "." ) ) ;
196176
197177 // Load the cache
198- let cache = load_cache ( & cache_file) ?;
178+ let cache = sync_cache ( repo , cache_file) ?;
199179
200180 // Process the owners from the cache
201181 match format {
@@ -267,23 +247,14 @@ pub fn codeowners_list_owners(path: Option<&std::path::Path>, format: &OutputFor
267247}
268248
269249/// Audit and analyze tag usage across CODEOWNERS files
270- pub fn codeowners_list_tags ( path : Option < & std:: path:: Path > , format : & OutputFormat ) -> Result < ( ) > {
271- info ! ( "Listing tags" ) ;
272- info ! ( "Output format: {}" , format) ;
273-
274- // Determine the cache file path based on repository path
275- let repo_path = path. unwrap_or_else ( || std:: path:: Path :: new ( "." ) ) ;
276- let cache_file = repo_path. join ( ".codeowners.cache" ) ;
277-
278- if !cache_file. exists ( ) {
279- return Err ( utils:: error:: Error :: new ( & format ! (
280- "Cache file not found at {}. Please run 'codeowners parse' first." ,
281- cache_file. display( )
282- ) ) ) ;
283- }
250+ pub fn codeowners_list_tags (
251+ repo : Option < & std:: path:: Path > , format : & OutputFormat , cache_file : Option < & std:: path:: Path > ,
252+ ) -> Result < ( ) > {
253+ // Repository path
254+ let repo = repo. unwrap_or_else ( || std:: path:: Path :: new ( "." ) ) ;
284255
285256 // Load the cache
286- let cache = load_cache ( & cache_file) ?;
257+ let cache = sync_cache ( repo , cache_file) ?;
287258
288259 // Process the tags from the cache
289260 match format {
0 commit comments