lean_ctx/cli/
import_cmd.rs1use crate::core::import;
4use crate::core::knowledge::{KnowledgeFact, ProjectKnowledge};
5
6pub fn cmd_import(args: &[String]) {
7 if args
8 .iter()
9 .any(|arg| matches!(arg.as_str(), "--help" | "-h"))
10 {
11 print_help();
12 return;
13 }
14
15 let dry_run = args.iter().any(|arg| arg == "--dry-run");
16 let all = args.iter().any(|arg| arg == "--all");
17 let sources: Vec<&str> = args
18 .iter()
19 .filter_map(|arg| (!arg.starts_with('-')).then_some(arg.as_str()))
20 .collect();
21
22 if (all && !sources.is_empty()) || sources.len() > 1 {
23 eprintln!("Use one source (claude-code, codex, cursor, opencode) or --all.");
24 print_help();
25 return;
26 }
27
28 let (label, mut result) = if all {
29 let mut result = import::claude_code::import();
30 result.merge(import::codex::import());
31 result.merge(import::cursor::import());
32 result.merge(import::opencode::import());
33 ("all sources", result)
34 } else {
35 match sources.first().copied() {
36 Some("claude-code") => ("claude-code", import::claude_code::import()),
37 Some("codex") => ("codex", import::codex::import()),
38 Some("cursor") => ("cursor", import::cursor::import()),
39 Some("opencode") => ("opencode", import::opencode::import()),
40 _ => {
41 eprintln!("Choose a source: claude-code, codex, cursor, opencode, or --all.");
42 print_help();
43 return;
44 }
45 }
46 };
47
48 let extracted = result.facts_extracted;
49 let persisted = if dry_run {
50 0
51 } else {
52 let project_root = match std::env::current_dir() {
53 Ok(path) => path.to_string_lossy().into_owned(),
54 Err(error) => {
55 eprintln!("Import failed: cannot determine project root: {error}");
56 return;
57 }
58 };
59 match persist_facts(&project_root, std::mem::take(&mut result.facts)) {
60 Ok(count) => count,
61 Err(error) => {
62 eprintln!("Import failed: {error}");
63 return;
64 }
65 }
66 };
67
68 if dry_run {
69 println!("[DRY RUN] {label}: no facts written");
70 } else {
71 println!("Imported {persisted} new fact(s) from {label}.");
72 }
73 println!(
74 "Sessions: {}; facts extracted: {extracted}; files touched: {}",
75 result.sessions_found, result.files_touched
76 );
77 for error in &result.errors {
78 eprintln!("Import warning: {error}");
79 }
80}
81
82fn persist_facts(project_root: &str, facts: Vec<KnowledgeFact>) -> Result<usize, String> {
83 ProjectKnowledge::mutate_locked(project_root, |knowledge| {
84 let mut added = 0;
85 for fact in facts {
86 let exists = knowledge.facts.iter().any(|existing| {
87 existing.category == fact.category
88 && existing.value == fact.value
89 && existing.imported_from == fact.imported_from
90 });
91 if !exists {
92 knowledge.facts.push(fact);
93 added += 1;
94 }
95 }
96 added
97 })
98 .map(|(_, added)| added)
99}
100
101fn print_help() {
102 println!("Usage: lean-ctx import <claude-code|codex|cursor|opencode> [--dry-run]");
103 println!(" lean-ctx import --all [--dry-run]");
104}
105
106#[cfg(test)]
107mod tests {
108 use crate::core::import::ImportSource;
109
110 #[test]
111 fn source_names_match_import_engine() {
112 assert_eq!(ImportSource::ClaudeCode.as_str(), "claude-code");
113 assert_eq!(ImportSource::Codex.as_str(), "codex");
114 assert_eq!(ImportSource::Cursor.as_str(), "cursor");
115 assert_eq!(ImportSource::OpenCode.as_str(), "opencode");
116 }
117}