1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
//! Delete an Edge app.

use dialoguer::Confirm;
use is_terminal::IsTerminal;

use super::util::AppIdentOpts;
use crate::{commands::AsyncCliCommand, config::WasmerEnv};

/// Delete an existing Edge app
#[derive(clap::Parser, Debug)]
pub struct CmdAppDelete {
    #[clap(flatten)]
    env: WasmerEnv,

    #[clap(long, default_value_t = !std::io::stdin().is_terminal())]
    non_interactive: bool,

    #[clap(flatten)]
    ident: AppIdentOpts,
}

#[async_trait::async_trait]
impl AsyncCliCommand for CmdAppDelete {
    type Output = ();

    async fn run_async(self) -> Result<(), anyhow::Error> {
        let interactive = !self.non_interactive;
        let client = self.env.client()?;

        eprintln!("Looking up the app...");
        let (_ident, app) = self.ident.load_app(&client).await?;

        if interactive {
            let theme = dialoguer::theme::ColorfulTheme::default();
            let should_use = Confirm::with_theme(&theme)
                .with_prompt(format!(
                    "Really delete the app '{}/{}'? (id: {})",
                    app.owner.global_name,
                    app.name,
                    app.id.inner()
                ))
                .interact()?;

            if !should_use {
                eprintln!("App will not be deleted.");
                return Ok(());
            }
        }

        eprintln!(
            "Deleting app {}/{} (id: {})...",
            app.owner.global_name,
            app.name,
            app.id.inner(),
        );
        wasmer_backend_api::query::delete_app(&client, app.id.into_inner()).await?;

        eprintln!("App '{}/{}' was deleted!", app.owner.global_name, app.name);

        Ok(())
    }
}