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