use crate::daemon_base_url;
use anyhow::Result;
use colored::Colorize;
pub async fn handle_list(json: bool) -> Result<()> {
let base = daemon_base_url();
crate::commands::daemon_guard::ensure_daemon_running_or_exit(&base).await;
let url = format!("{}/indexes", base);
let list_client = trusty_common::server::daemon_http_client()?;
match list_client.get(&url).send().await {
Ok(resp) if resp.status().is_success() => {
let body: serde_json::Value =
resp.json().await.unwrap_or_else(|_| serde_json::json!({}));
if json {
println!("{}", body);
} else {
println!("{}", "Registered indexes:".bold());
let empty: Vec<serde_json::Value> = Vec::new();
let arr = body
.get("indexes")
.and_then(|v| v.as_array())
.unwrap_or(&empty);
if arr.is_empty() {
println!(" {}", "(none)".dimmed());
} else {
for v in arr {
if let Some(s) = v.as_str() {
println!(" • {}", s);
}
}
}
}
}
Ok(resp) => {
eprintln!("{} daemon returned {}", "✗".red(), resp.status());
std::process::exit(1);
}
Err(e) => {
eprintln!("{} could not reach daemon at {}: {e}", "✗".red(), base);
std::process::exit(1);
}
}
Ok(())
}