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
63
64
65
66
67
68
69
70
use dialoguer::Confirm;
use is_terminal::IsTerminal;

use crate::{cmd::AsyncCliCommand, ApiOpts};

use super::AppIdent;

/// Show an app.
#[derive(clap::Parser, Debug)]
pub struct CmdAppDelete {
    #[clap(flatten)]
    api: ApiOpts,

    #[clap(long)]
    non_interactive: bool,

    /// Identifier of the app.
    ///
    /// Eg:
    /// - name (assumes current user)
    /// - namespace/name
    ///
    /// NOTE: if not specified, the command will try to infer the app from an
    /// app.yaml file in the current directory.
    ident: AppIdent,
}

impl CmdAppDelete {
    async fn run(self) -> Result<(), anyhow::Error> {
        let interactive = std::io::stdin().is_terminal() && !self.non_interactive;
        let client = self.api.client()?;

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

        if interactive {
            let should_use = Confirm::new()
                .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_api::backend::delete_app(&client, app.id.into_inner()).await?;

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

        Ok(())
    }
}

impl AsyncCliCommand for CmdAppDelete {
    fn run_async(self) -> futures::future::BoxFuture<'static, Result<(), anyhow::Error>> {
        Box::pin(self.run())
    }
}