Skip to main content

xbp_cli/cli/
router.rs

1use super::app::AppContext;
2use super::auto_commit::{reset_explicit_push, set_explicit_push};
3use super::commands::{Cli, Commands};
4use super::error::CliResult;
5use super::handle_cloudflare;
6use super::handle_dns;
7#[cfg(feature = "docker")]
8use super::handle_docker;
9use super::handle_domains;
10#[cfg(feature = "kubernetes")]
11use super::handle_kubernetes;
12#[cfg(feature = "linear")]
13use super::handle_linear;
14#[cfg(feature = "monitoring")]
15use super::handle_monitoring;
16#[cfg(feature = "nordvpn")]
17use super::handle_nordvpn;
18#[cfg(feature = "secrets")]
19use super::handle_secrets;
20use super::handle_workers;
21use super::{
22    handle_api, handle_cloudflared, handle_commit, handle_config, handle_curl, handle_cursor,
23    handle_diag, handle_done, handle_env, handle_fix_process_monitor_json, handle_flush,
24    handle_generate, handle_github, handle_init, handle_install, handle_issues, handle_list,
25    handle_login, handle_logs, handle_logs_flag, handle_mcp, handle_monitor, handle_network,
26    handle_deploy, handle_nginx, handle_no_command, handle_oci, handle_ports, handle_publish, handle_push,
27    handle_redeploy,
28    handle_redeploy_v2, handle_resurrect, handle_runners, handle_service, handle_services,
29    handle_setup, handle_snapshot, handle_ssh, handle_start, handle_stop, handle_tail,
30    handle_todos, handle_update, handle_version, handle_whoami, handle_worktree_watch,
31};
32use std::future::Future;
33use std::pin::Pin;
34
35type DispatchFuture<'a> = Pin<Box<dyn Future<Output = CliResult<()>> + 'a>>;
36
37pub fn dispatch(cli: Cli, ctx: &mut AppContext) -> DispatchFuture<'_> {
38    let debug = ctx.debug();
39    let Cli {
40        logs,
41        list,
42        port,
43        push,
44        command,
45        ..
46    } = cli;
47
48    reset_explicit_push();
49    if push {
50        set_explicit_push(true);
51    }
52
53    if logs {
54        return Box::pin(handle_logs_flag());
55    }
56
57    if list && command.is_none() {
58        return Box::pin(handle_list(debug));
59    }
60
61    match command {
62        Some(Commands::Ports(cmd)) => Box::pin(handle_ports(cmd, port, debug)),
63        Some(Commands::Commit(cmd)) => Box::pin(handle_commit(cmd)),
64        Some(Commands::Push) => Box::pin(handle_push()),
65        Some(Commands::Deploy(cmd)) => Box::pin(handle_deploy(cmd, debug)),
66        Some(Commands::Oci(cmd)) => Box::pin(handle_oci(cmd, debug)),
67        Some(Commands::Init) => Box::pin(handle_init(debug)),
68        Some(Commands::Setup) => Box::pin(handle_setup(debug)),
69        Some(Commands::Redeploy { service_name }) => Box::pin(handle_redeploy(service_name, debug)),
70        Some(Commands::RedeployV2(cmd)) => Box::pin(handle_redeploy_v2(cmd, debug)),
71        Some(Commands::Config(cmd)) => Box::pin(handle_config(cmd, debug)),
72        Some(Commands::Install {
73            list,
74            force,
75            package,
76        }) => Box::pin(handle_install(package, list, force, debug)),
77        Some(Commands::Logs(cmd)) => Box::pin(handle_logs(cmd, debug)),
78        Some(Commands::Ssh(cmd)) => Box::pin(handle_ssh(cmd, debug)),
79        Some(Commands::Cloudflared(cmd)) => Box::pin(handle_cloudflared(cmd, debug)),
80        Some(Commands::Cloudflare(cmd)) => Box::pin(handle_cloudflare(cmd, debug)),
81        Some(Commands::List) => Box::pin(handle_list(debug)),
82        Some(Commands::Curl(cmd)) => Box::pin(handle_curl(cmd, debug)),
83        Some(Commands::Services) => Box::pin(handle_services(debug)),
84        Some(Commands::Service {
85            command,
86            service_name,
87        }) => Box::pin(handle_service(command, service_name, debug)),
88        Some(Commands::Nginx(cmd)) => Box::pin(handle_nginx(cmd.command, debug)),
89        Some(Commands::Network(cmd)) => Box::pin(handle_network(cmd, debug)),
90        Some(Commands::Diag(cmd)) => Box::pin(handle_diag(cmd, debug)),
91        Some(Commands::Monitor(cmd)) => Box::pin(handle_monitor(cmd, debug)),
92        Some(Commands::Snapshot) => Box::pin(handle_snapshot(debug)),
93        Some(Commands::Resurrect) => Box::pin(handle_resurrect(debug)),
94        Some(Commands::Stop { target }) => Box::pin(handle_stop(target, debug)),
95        Some(Commands::Flush { target }) => Box::pin(handle_flush(target, debug)),
96        Some(Commands::Login(cmd)) => Box::pin(handle_login(cmd)),
97        Some(Commands::Whoami) => Box::pin(handle_whoami()),
98        Some(Commands::Update(cmd)) => Box::pin(handle_update(cmd)),
99        Some(Commands::Version(cmd)) => Box::pin(handle_version(cmd, debug)),
100        Some(Commands::Publish(cmd)) => Box::pin(handle_publish(cmd, debug)),
101        Some(Commands::Env { target }) => Box::pin(handle_env(target, debug)),
102        Some(Commands::Tail(cmd)) => Box::pin(handle_tail(cmd, debug)),
103        Some(Commands::Start { args }) => Box::pin(handle_start(args, debug)),
104        Some(Commands::Generate(cmd)) => Box::pin(handle_generate(cmd, debug)),
105        Some(Commands::Api(cmd)) => Box::pin(handle_api(cmd, debug)),
106        Some(Commands::Runners(cmd)) => Box::pin(handle_runners(cmd, debug)),
107        Some(Commands::Mcp(cmd)) => Box::pin(handle_mcp(cmd, debug)),
108        Some(Commands::WorktreeWatch(cmd)) => Box::pin(handle_worktree_watch(cmd, debug)),
109        #[cfg(feature = "docker")]
110        Some(Commands::Docker(cmd)) => Box::pin(handle_docker(cmd, debug)),
111        #[cfg(feature = "secrets")]
112        Some(Commands::Secrets(cmd)) => Box::pin(handle_secrets(cmd, debug)),
113        Some(Commands::Workers(cmd)) => Box::pin(handle_workers(cmd, debug)),
114        Some(Commands::Dns(cmd)) => Box::pin(handle_dns(cmd, debug)),
115        Some(Commands::Domains(cmd)) => Box::pin(handle_domains(cmd, debug)),
116        Some(Commands::Done(cmd)) => Box::pin(handle_done(cmd, debug)),
117        #[cfg(feature = "linear")]
118        Some(Commands::Linear(cmd)) => Box::pin(handle_linear(cmd, debug)),
119        Some(Commands::Github(cmd)) => Box::pin(handle_github(cmd, debug)),
120        Some(Commands::Issues(cmd)) => Box::pin(handle_issues(cmd, debug)),
121        Some(Commands::Todos(cmd)) => Box::pin(handle_todos(cmd, debug)),
122        Some(Commands::FixProcessMonitorJson(cmd)) => {
123            Box::pin(handle_fix_process_monitor_json(cmd, debug))
124        }
125        Some(Commands::Cursor(cmd)) => Box::pin(handle_cursor(cmd)),
126        #[cfg(feature = "kubernetes")]
127        Some(Commands::Kubernetes(cmd)) => Box::pin(handle_kubernetes(cmd, debug)),
128        #[cfg(feature = "nordvpn")]
129        Some(Commands::Nordvpn(cmd)) => Box::pin(handle_nordvpn(cmd, debug)),
130        #[cfg(feature = "monitoring")]
131        Some(Commands::Monitoring(cmd)) => Box::pin(handle_monitoring(cmd)),
132        None => {
133            // preserve previous no-command behavior
134            Box::pin(handle_no_command(port, debug))
135        }
136    }
137}
138
139#[cfg(test)]
140mod tests {
141    use super::*;
142    use clap::Parser;
143
144    #[test]
145    fn dispatch_future_is_boxed_and_small() {
146        let cli = Cli::parse_from(["xbp", "login"]);
147        let mut ctx = AppContext::new(false);
148        let future = dispatch(cli, &mut ctx);
149        assert!(std::mem::size_of_val(&future) <= 2 * std::mem::size_of::<usize>());
150    }
151}