use clap::Args;
use microsandbox::sandbox::Sandbox;
use crate::ui;
#[derive(Debug, Args)]
pub struct StopArgs {
pub name: String,
#[arg(short, long)]
pub force: bool,
#[arg(short = 't', long)]
pub timeout: Option<u64>,
#[arg(short, long)]
pub quiet: bool,
}
pub async fn run(args: StopArgs) -> anyhow::Result<()> {
let spinner = if args.quiet {
ui::Spinner::quiet()
} else {
ui::Spinner::start("Stopping", &args.name)
};
let mut handle = Sandbox::get(&args.name).await?;
let result = if args.force {
handle.kill().await
} else if let Some(timeout_secs) = args.timeout {
match tokio::time::timeout(std::time::Duration::from_secs(timeout_secs), handle.stop())
.await
{
Ok(stop_result) => stop_result,
Err(_) => handle.kill().await,
}
} else {
handle.stop().await
};
match result {
Ok(()) => {
spinner.finish_success("Stopped");
}
Err(e) => {
spinner.finish_error();
return Err(e.into());
}
}
Ok(())
}