use anyhow::Result;
use clap::Parser;
use std::net::SocketAddr;
use tracing::debug;
use theater::id::TheaterId;
use crate::{error::CliError, output::formatters::ActorInspection, CommandContext};
#[derive(Debug, Parser)]
pub struct InspectArgs {
#[arg(required = true)]
pub actor_id: TheaterId,
#[arg(short, long)]
pub address: Option<SocketAddr>,
#[arg(short, long)]
pub detailed: bool,
}
pub async fn execute_async(args: &InspectArgs, ctx: &CommandContext) -> Result<(), CliError> {
debug!("Inspecting actor: {}", args.actor_id);
let address = ctx.server_address(args.address);
debug!("Connecting to server at: {}", address);
let client = ctx.create_client();
client
.connect()
.await
.map_err(|e| CliError::connection_failed(address, e))?;
debug!("Getting actor status");
let status = client
.get_actor_status(&args.actor_id.to_string())
.await
.map_err(|_e| CliError::actor_not_found(&args.actor_id.to_string()))?;
debug!("Getting actor state");
let state_result = client.get_actor_state(&args.actor_id.to_string()).await;
let state = match state_result {
Ok(ref state_value) => {
if state_value.is_null() {
None
} else {
Some(state_value)
}
}
_ => None,
};
debug!("Getting actor events");
let events_result = client.get_actor_events(&args.actor_id.to_string()).await;
let events = match events_result {
Ok(events) => events,
Err(_) => vec![],
};
let metrics: Option<serde_json::Value> = None;
let inspection = ActorInspection {
id: args.actor_id.clone(),
status: format!("{:?}", status),
state: state.cloned(),
events: events.clone(),
metrics,
detailed: args.detailed,
};
ctx.output.output(&inspection, None)?;
Ok(())
}