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
use crate::{cmd::AsyncCliCommand, ApiOpts, ListFormatOpts};

/// List apps.
#[derive(clap::Parser, Debug)]
pub struct CmdAppList {
    #[clap(flatten)]
    fmt: ListFormatOpts,
    #[clap(flatten)]
    api: ApiOpts,

    /// Get apps in a specific namespace.
    ///
    /// Will fetch the apps owned by the current user otherwise.
    #[clap(short = 'n', long)]
    namespace: Option<String>,

    /// Get all apps that are accessible by the current user, including apps
    /// directly owned by the user and apps in namespaces the user can access.
    #[clap(short = 'a', long)]
    all: bool,
}

impl CmdAppList {
    async fn run(self) -> Result<(), anyhow::Error> {
        let client = self.api.client()?;

        let apps = if let Some(ns) = self.namespace {
            wasmer_api::backend::namespace_apps(&client, &ns).await?
        } else if self.all {
            wasmer_api::backend::user_accessible_apps(&client).await?
        } else {
            wasmer_api::backend::user_apps(&client).await?
        };

        println!("{}", self.fmt.format.render(&apps));

        Ok(())
    }
}

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