1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
//! Handler for `trusty-search init` (register an index without indexing).
use super::reindex_engine::register_index_with_daemon;
use anyhow::Result;
use colored::Colorize;
/// Why: extracted from `main()` so the registration flow is testable in isolation
/// and `main()` stays a thin dispatcher. Behaviour is unchanged from the
/// pre-refactor inline implementation.
/// What: writes a `.trusty-search` marker file (idempotent), then registers the
/// index with the daemon. Distinguishes "registered", "already registered",
/// and "daemon not running" in the user-facing output.
/// Test: `cargo run -- init /tmp/empty` creates a marker and reports daemon
/// status truthfully (registered / already-registered / daemon-down).
pub async fn handle_init(
path: Option<std::path::PathBuf>,
name: Option<String>,
exclude: Vec<String>,
) -> Result<()> {
let cwd = std::env::current_dir().unwrap_or_default();
let project_path = path.unwrap_or(cwd);
let index_name = name.unwrap_or_else(|| {
project_path
.file_name()
.unwrap_or_default()
.to_string_lossy()
.into_owned()
});
let marker = project_path.join(".trusty-search");
if !marker.exists() {
std::fs::write(&marker, format!("index = \"{}\"\n", index_name))?;
println!("{} Created {}", "✓".green(), marker.display());
} else {
println!("{} {} already exists", "·".dimmed(), marker.display());
}
if !exclude.is_empty() {
println!("{} Extra exclusions: {}", "·".dimmed(), exclude.join(", "));
}
// Why: previously we printed "Registered" without contacting the daemon
// — misleading because the daemon had no idea about this index.
// Now: POST /indexes (idempotent) and report truthfully.
match register_index_with_daemon(&index_name, &project_path).await {
Ok((true, _)) => {
println!(
"{} Registered '{}' with daemon at {}",
"✓".green(),
index_name.bold(),
project_path.display()
);
println!(
" Run {} to index this project.",
"trusty-search index".cyan()
);
}
Ok((false, true)) => {
println!(
"{} '{}' already registered with daemon",
"↻".cyan(),
index_name.bold()
);
println!(
" Run {} to index this project.",
"trusty-search index".cyan()
);
}
Ok((_, false)) => {
println!(
"{} Daemon not running — index will be created when daemon starts.",
"·".dimmed()
);
println!(
" Start with {} then run {}.",
"trusty-search start".cyan(),
"trusty-search index".cyan()
);
}
Err(e) => {
eprintln!(
"{} {} — index will need to be re-registered when daemon is healthy",
"⚠".yellow(),
e
);
}
}
Ok(())
}