use anyhow::{Context, Result};
use clap::Args;
use kanade_shared::subject;
#[derive(Args, Debug)]
pub struct InventoryArgs {
pub pc_id: String,
#[arg(long, default_value = "30s")]
pub timeout: humantime::Duration,
}
pub async fn execute(client: async_nats::Client, args: InventoryArgs) -> Result<()> {
let subj = subject::inventory_request(&args.pc_id);
let reply = tokio::time::timeout(
args.timeout.into(),
client.request(subj.clone(), bytes::Bytes::new()),
)
.await
.with_context(|| format!("timeout ({}) waiting for {}", args.timeout, args.pc_id))?
.with_context(|| format!("request {subj}"))?;
let body = String::from_utf8_lossy(&reply.payload);
println!("{pc}: {body}", pc = args.pc_id);
if !body.starts_with("ok") {
anyhow::bail!(
"inventory collection failed on {} — see agent log for details",
args.pc_id
);
}
Ok(())
}