use clap::Parser;
use std::net::SocketAddr;
use std::str::FromStr;
use tracing::debug;
use crate::error::{CliError, CliResult};
use crate::output::formatters::ActorAction;
use crate::CommandContext;
use theater::id::TheaterId;
#[derive(Debug, Parser)]
pub struct StopArgs {
#[arg(required = true)]
pub actor_id: String,
#[arg(short, long, default_value = "127.0.0.1:9000")]
pub address: SocketAddr,
}
pub async fn execute_async(args: &StopArgs, ctx: &CommandContext) -> CliResult<()> {
debug!("Stopping actor: {}", args.actor_id);
debug!("Connecting to server at: {}", args.address);
let actor_id = TheaterId::from_str(&args.actor_id).map_err(|_| CliError::InvalidInput {
field: "actor_id".to_string(),
value: args.actor_id.clone(),
suggestion: "Provide a valid actor ID in the correct format".to_string(),
})?;
let client = ctx.create_client();
client
.connect()
.await
.map_err(|e| CliError::connection_failed(args.address, e))?;
client
.stop_actor(&actor_id.to_string())
.await
.map_err(|e| CliError::ServerError {
message: format!("Failed to stop actor: {}", e),
})?;
let action_result = ActorAction {
action: "stopped".to_string(),
actor_id: actor_id.to_string(),
success: true,
message: None,
};
let format = if ctx.json { Some("json") } else { None };
ctx.output.output(&action_result, format)?;
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
use crate::config::Config;
use crate::output::OutputManager;
#[tokio::test]
async fn test_stop_command_invalid_actor_id() {
let args = StopArgs {
actor_id: "invalid-id".to_string(),
address: "127.0.0.1:9000".parse().unwrap(),
};
let config = Config::default();
let output = OutputManager::new(config.output.clone());
let ctx = CommandContext {
config,
output,
verbose: false,
json: false,
};
let result = execute_async(&args, &ctx).await;
assert!(result.is_err());
if let Err(CliError::InvalidInput { field, .. }) = result {
assert_eq!(field, "actor_id");
} else {
panic!("Expected InvalidInput error");
}
}
}