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
pub mod app;
pub mod connect;
pub mod deploy;
pub mod namespace;
pub mod ssh;

#[derive(clap::Subcommand, Debug)]
pub enum SubCmd {
    #[clap(subcommand)]
    App(app::CmdApp),
    #[clap(subcommand)]
    Namespace(namespace::CmdNamespace),
    Deploy(deploy::CmdDeploy),
    Ssh(ssh::CmdSsh),
    Connect(connect::CmdConnect),
}

impl CliCommand for SubCmd {
    fn run(self) -> Result<(), anyhow::Error> {
        match self {
            Self::Ssh(cmd) => cmd.run(),
            Self::App(cmd) => cmd.run(),
            Self::Namespace(cmd) => cmd.run(),
            Self::Deploy(cmd) => cmd.run(),
            Self::Connect(cmd) => cmd.run(),
        }
    }
}

pub trait CliCommand {
    fn run(self) -> Result<(), anyhow::Error>;
}

pub trait AsyncCliCommand: Send + Sync + 'static {
    fn run_async(self) -> futures::future::BoxFuture<'static, Result<(), anyhow::Error>>;
}

impl<C: AsyncCliCommand> CliCommand for C {
    fn run(self) -> Result<(), anyhow::Error> {
        tokio::runtime::Runtime::new()?.block_on(self.run_async())
    }
}