Skip to main content

xbp_cli/cli/
router.rs

1use super::app::AppContext;
2use super::commands::{Cli, Commands};
3use super::error::CliResult;
4#[cfg(feature = "docker")]
5use super::handle_docker;
6#[cfg(feature = "kubernetes")]
7use super::handle_kubernetes;
8#[cfg(feature = "monitoring")]
9use super::handle_monitoring;
10#[cfg(feature = "nordvpn")]
11use super::handle_nordvpn;
12#[cfg(feature = "secrets")]
13use super::handle_secrets;
14use super::{
15    handle_api, handle_config, handle_curl, handle_diag, handle_done, handle_env, handle_flush,
16    handle_generate, handle_init, handle_install, handle_list, handle_login, handle_logs,
17    handle_logs_flag, handle_monitor, handle_network, handle_nginx, handle_no_command,
18    handle_ports, handle_redeploy, handle_redeploy_v2, handle_resurrect, handle_service,
19    handle_services, handle_setup, handle_snapshot, handle_start, handle_stop, handle_tail,
20    handle_version,
21};
22use std::future::Future;
23use std::pin::Pin;
24
25type DispatchFuture<'a> = Pin<Box<dyn Future<Output = CliResult<()>> + 'a>>;
26
27pub fn dispatch(cli: Cli, ctx: &mut AppContext) -> DispatchFuture<'_> {
28    let debug = ctx.debug();
29    let Cli {
30        logs,
31        list,
32        port,
33        command,
34        ..
35    } = cli;
36
37    if logs {
38        return Box::pin(handle_logs_flag());
39    }
40
41    if list && command.is_none() {
42        return Box::pin(handle_list(debug));
43    }
44
45    match command {
46        Some(Commands::Ports(cmd)) => Box::pin(handle_ports(cmd, port, debug)),
47        Some(Commands::Init) => Box::pin(handle_init(debug)),
48        Some(Commands::Setup) => Box::pin(handle_setup(debug)),
49        Some(Commands::Redeploy { service_name }) => Box::pin(handle_redeploy(service_name, debug)),
50        Some(Commands::RedeployV2(cmd)) => Box::pin(handle_redeploy_v2(cmd, debug)),
51        Some(Commands::Config(cmd)) => Box::pin(handle_config(cmd, debug)),
52        Some(Commands::Install { list, package }) => Box::pin(handle_install(package, list, debug)),
53        Some(Commands::Logs(cmd)) => Box::pin(handle_logs(cmd, debug)),
54        Some(Commands::List) => Box::pin(handle_list(debug)),
55        Some(Commands::Curl(cmd)) => Box::pin(handle_curl(cmd, debug)),
56        Some(Commands::Services) => Box::pin(handle_services(debug)),
57        Some(Commands::Service {
58            command,
59            service_name,
60        }) => Box::pin(handle_service(command, service_name, debug)),
61        Some(Commands::Nginx(cmd)) => Box::pin(handle_nginx(cmd.command, debug)),
62        Some(Commands::Network(cmd)) => Box::pin(handle_network(cmd, debug)),
63        Some(Commands::Diag(cmd)) => Box::pin(handle_diag(cmd, debug)),
64        Some(Commands::Monitor(cmd)) => Box::pin(handle_monitor(cmd, debug)),
65        Some(Commands::Snapshot) => Box::pin(handle_snapshot(debug)),
66        Some(Commands::Resurrect) => Box::pin(handle_resurrect(debug)),
67        Some(Commands::Stop { target }) => Box::pin(handle_stop(target, debug)),
68        Some(Commands::Flush { target }) => Box::pin(handle_flush(target, debug)),
69        Some(Commands::Login) => Box::pin(handle_login()),
70        Some(Commands::Version(cmd)) => Box::pin(handle_version(cmd, debug)),
71        Some(Commands::Env { target }) => Box::pin(handle_env(target, debug)),
72        Some(Commands::Tail(cmd)) => Box::pin(handle_tail(cmd, debug)),
73        Some(Commands::Start { args }) => Box::pin(handle_start(args, debug)),
74        Some(Commands::Generate(cmd)) => Box::pin(handle_generate(cmd, debug)),
75        Some(Commands::Api(cmd)) => Box::pin(handle_api(cmd, debug)),
76        #[cfg(feature = "docker")]
77        Some(Commands::Docker(cmd)) => Box::pin(handle_docker(cmd, debug)),
78        #[cfg(feature = "secrets")]
79        Some(Commands::Secrets(cmd)) => Box::pin(handle_secrets(cmd, debug)),
80        Some(Commands::Done(cmd)) => Box::pin(handle_done(cmd, debug)),
81        #[cfg(feature = "kubernetes")]
82        Some(Commands::Kubernetes(cmd)) => Box::pin(handle_kubernetes(cmd, debug)),
83        #[cfg(feature = "nordvpn")]
84        Some(Commands::Nordvpn(cmd)) => Box::pin(handle_nordvpn(cmd, debug)),
85        #[cfg(feature = "monitoring")]
86        Some(Commands::Monitoring(cmd)) => Box::pin(handle_monitoring(cmd)),
87        None => {
88            // preserve previous no-command behavior
89            Box::pin(handle_no_command(port, debug))
90        }
91    }
92}
93
94#[cfg(test)]
95mod tests {
96    use super::*;
97    use clap::Parser;
98
99    #[test]
100    fn dispatch_future_is_boxed_and_small() {
101        let cli = Cli::parse_from(["xbp", "login"]);
102        let mut ctx = AppContext::new(false);
103        let future = dispatch(cli, &mut ctx);
104        assert!(std::mem::size_of_val(&future) <= 2 * std::mem::size_of::<usize>());
105    }
106}