use crate::client::KoiClient;
use crate::format;
pub fn status(client: &KoiClient, json: bool) -> anyhow::Result<()> {
let status = client.admin_status()?;
if json {
println!("{}", serde_json::to_string_pretty(&status)?);
} else {
println!("Koi daemon v{}", status.version);
println!(" Platform: {}", status.platform);
println!(" Uptime: {}s", status.uptime_secs);
let r = &status.registrations;
println!(
" Services: {} total ({} alive, {} draining, {} permanent)",
r.total, r.alive, r.draining, r.permanent,
);
}
Ok(())
}
pub fn list(client: &KoiClient, json: bool) -> anyhow::Result<()> {
let registrations = client.admin_registrations()?;
if json {
println!("{}", serde_json::to_string_pretty(®istrations)?);
} else if registrations.is_empty() {
println!("No registrations.");
} else {
println!(
"{:<10} {:<20} {:<16} {:>5} {:<10} {:<10}",
"ID", "NAME", "TYPE", "PORT", "STATE", "MODE"
);
for reg in ®istrations {
print!("{}", format::registration_row(reg));
}
}
Ok(())
}
pub fn inspect(client: &KoiClient, id: &str, json: bool) -> anyhow::Result<()> {
let reg = client.admin_inspect(id)?;
if json {
let body = serde_json::to_string_pretty(®)?;
println!("{body}");
} else {
print!("{}", format::registration_detail(®));
}
Ok(())
}
pub fn unregister(client: &KoiClient, id: &str, json: bool) -> anyhow::Result<()> {
client.admin_force_unregister(id)?;
if json {
println!(
"{}",
serde_json::json!({"unregistered": id, "source": "admin"})
);
} else {
println!("Force-unregistered {id}");
}
Ok(())
}
pub fn drain(client: &KoiClient, id: &str, json: bool) -> anyhow::Result<()> {
client.admin_drain(id)?;
if json {
println!("{}", serde_json::json!({"drained": id}));
} else {
println!("Draining {id}");
}
Ok(())
}
pub fn revive(client: &KoiClient, id: &str, json: bool) -> anyhow::Result<()> {
client.admin_revive(id)?;
if json {
println!("{}", serde_json::json!({"revived": id}));
} else {
println!("Revived {id}");
}
Ok(())
}