Skip to main content

xianyun_cli/commands/
destroy.rs

1//! `caproni destroy`: destroy the cluster and delete all its data.
2//!
3//! Port of Go's `cmd/destroy` package.
4
5use anyhow::Context as _;
6use clap::Args;
7use xianyun_core::config::caproni::load_from_context;
8use xianyun_core::config::global::Context;
9
10#[derive(Args, Debug)]
11pub struct DestroyArgs {}
12
13pub fn run(_args: &DestroyArgs, ctx: &Context) -> anyhow::Result<()> {
14    run_destroy(ctx)
15}
16
17/// Port of `destroy.runDestroy`: loads the configuration, resolves the
18/// cluster manager for its configured driver, destroys the cluster, and
19/// (best-effort, matching Go) releases this profile's hold on the
20/// host-side registry cache.
21pub fn run_destroy(ctx: &Context) -> anyhow::Result<()> {
22    crate::drivers::ensure_registered();
23
24    let container = load_from_context(ctx).context("loading configuration")?;
25
26    let manager = xianyun_cluster::registry::new_manager(
27        container.caproni.cluster.clone(),
28        container.global.profile_name.clone(),
29    )
30    .context("creating cluster manager")?;
31
32    manager.destroy().context("destroying cluster")?;
33
34    // Best-effort: a stale registry-cache ref leak doesn't block destruction
35    // of the cluster, matching Go's `slog.Warn`-and-continue behavior.
36    if let Err(err) =
37        xianyun_deploy::registrycache::ensure_stopped_for_profile(&container.global.profile_name)
38    {
39        tracing::warn!(
40            profile = %container.global.profile_name,
41            error = %err,
42            "could not release registry cache ref"
43        );
44    }
45
46    Ok(())
47}