use anyhow::Result;
use clap::Parser;
use futures::StreamExt;
use super::{validate_component_id, CliConnectionOpts, CommandOutput};
use crate::{config::WashConnectionOptions, spier::Spier};
#[derive(Debug, Parser, Clone)]
pub struct SpyCommand {
#[clap(name = "component_id", value_parser = validate_component_id)]
pub component_id: String,
#[clap(flatten)]
pub opts: CliConnectionOpts,
}
pub async fn handle_command(cmd: SpyCommand) -> Result<CommandOutput> {
let wco: WashConnectionOptions = cmd.opts.try_into()?;
let ctl_client = wco.clone().into_ctl_client(None).await?;
let nats_client = wco.into_nats_client().await?;
let mut spier = Spier::new(&cmd.component_id, &ctl_client, &nats_client).await?;
println!("Spying on component {}\n", spier.component_id());
while let Some(msg) = spier.next().await {
println!(
r#"
[{}]
From: {:<25} To: {:<25}
Operation: {}
Message: {}"#,
msg.timestamp, msg.from, msg.to, msg.operation, msg.message
);
}
println!("Message subscribers closed");
Ok(CommandOutput::default())
}