use crate::cli::commands;
use crate::cli::error::CliResult;
use xbp_monitoring::{list_items, run_all_once, serve, RunKind};
pub async fn handle_monitoring(cmd: commands::MonitoringCmd) -> CliResult<()> {
match cmd.command {
commands::MonitoringSubCommand::Serve { file } => {
if let Err(err) = serve(&file).await {
return Err(format!("Failed to start monitoring service: {}", err).into());
}
}
commands::MonitoringSubCommand::RunOnce {
file,
probes_only,
stories_only,
} => {
let results = run_all_once(&file, probes_only, stories_only)
.await
.map_err(|e| e.to_string())?;
if results.is_empty() {
println!("No probes or stories configured");
} else {
for result in results {
let label = match result.kind {
RunKind::Probe => "probe",
RunKind::Story => "story",
};
if result.success {
println!("{} {} ✓", label, result.name);
} else {
let code = result
.status_code
.map(|c| c.to_string())
.unwrap_or_else(|| "-".into());
let err = result.error.unwrap_or_else(|| "Unknown error".into());
println!("{} {} ✗ (code {}, {})", label, result.name, code, err);
}
}
}
}
commands::MonitoringSubCommand::List { file } => {
let (probes, stories) = list_items(&file).await.map_err(|e| e.to_string())?;
println!(
"Probes: {}",
if probes.is_empty() {
"none".into()
} else {
probes.join(", ")
}
);
println!(
"Stories: {}",
if stories.is_empty() {
"none".into()
} else {
stories.join(", ")
}
);
}
}
Ok(())
}