Skip to main content

the_code_graph_cli/commands/
communities.rs

1use domain::error::Result;
2use domain::model::CommunityConfig;
3use domain::use_cases::community::CommunityUseCase;
4
5use crate::commands::helpers::open_graph;
6use crate::commands::CommunitiesArgs;
7use crate::config::load_config;
8use crate::output::{print, OutputFormat};
9
10pub fn run_communities(args: &CommunitiesArgs, output_format: OutputFormat) -> Result<()> {
11    let (store, root) = open_graph()?;
12    let config = load_config(&root)?;
13
14    let mut community_config = CommunityConfig::default();
15
16    // Config file overrides (lowest priority after defaults)
17    if let Some(cc) = &config.communities {
18        if let Some(r) = cc.resolution {
19            community_config.resolution = r;
20        }
21        if let Some(s) = cc.min_community_size {
22            community_config.min_community_size = s;
23        }
24        if let Some(s) = cc.seed {
25            community_config.seed = Some(s);
26        }
27    }
28
29    // CLI flag overrides (highest priority)
30    if let Some(r) = args.resolution {
31        community_config.resolution = r;
32    }
33    if let Some(s) = args.min_size {
34        community_config.min_community_size = s;
35    }
36    if let Some(s) = args.seed {
37        community_config.seed = Some(s);
38    }
39
40    let uc = CommunityUseCase::new(store);
41
42    if let Some(ref symbol) = args.symbol {
43        match uc.community_of(symbol, &community_config)? {
44            Some(c) => {
45                eprintln!(
46                    "{} -> Community #{} ({}, {} members)",
47                    symbol,
48                    c.id,
49                    c.name,
50                    c.members.len()
51                );
52            }
53            None => {
54                eprintln!("symbol '{}' not found in any community", symbol);
55            }
56        }
57        return Ok(());
58    }
59
60    let mut analysis = uc.analyze(&community_config)?;
61
62    if let Some(community_id) = args.community_id {
63        if let Some(c) = analysis.communities.iter().find(|c| c.id == community_id) {
64            print(&vec![c.clone()], output_format);
65        } else {
66            eprintln!(
67                "community {} not found ({} communities total)",
68                community_id,
69                analysis.communities.len()
70            );
71        }
72    } else {
73        analysis.communities.truncate(args.limit);
74        print(&analysis, output_format);
75    }
76    Ok(())
77}