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_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::Init) => Box::pin(handle_init(debug)),
67 Some(Commands::Setup) => Box::pin(handle_setup(debug)),
68 Some(Commands::Redeploy { service_name }) => Box::pin(handle_redeploy(service_name, debug)),
69 Some(Commands::RedeployV2(cmd)) => Box::pin(handle_redeploy_v2(cmd, debug)),
70 Some(Commands::Config(cmd)) => Box::pin(handle_config(cmd, debug)),
71 Some(Commands::Install {
72 list,
73 force,
74 package,
75 }) => Box::pin(handle_install(package, list, force, debug)),
76 Some(Commands::Logs(cmd)) => Box::pin(handle_logs(cmd, debug)),
77 Some(Commands::Ssh(cmd)) => Box::pin(handle_ssh(cmd, debug)),
78 Some(Commands::Cloudflared(cmd)) => Box::pin(handle_cloudflared(cmd, debug)),
79 Some(Commands::Cloudflare(cmd)) => Box::pin(handle_cloudflare(cmd, debug)),
80 Some(Commands::List) => Box::pin(handle_list(debug)),
81 Some(Commands::Curl(cmd)) => Box::pin(handle_curl(cmd, debug)),
82 Some(Commands::Services) => Box::pin(handle_services(debug)),
83 Some(Commands::Service {
84 command,
85 service_name,
86 }) => Box::pin(handle_service(command, service_name, debug)),
87 Some(Commands::Nginx(cmd)) => Box::pin(handle_nginx(cmd.command, debug)),
88 Some(Commands::Network(cmd)) => Box::pin(handle_network(cmd, debug)),
89 Some(Commands::Diag(cmd)) => Box::pin(handle_diag(cmd, debug)),
90 Some(Commands::Monitor(cmd)) => Box::pin(handle_monitor(cmd, debug)),
91 Some(Commands::Snapshot) => Box::pin(handle_snapshot(debug)),
92 Some(Commands::Resurrect) => Box::pin(handle_resurrect(debug)),
93 Some(Commands::Stop { target }) => Box::pin(handle_stop(target, debug)),
94 Some(Commands::Flush { target }) => Box::pin(handle_flush(target, debug)),
95 Some(Commands::Login(cmd)) => Box::pin(handle_login(cmd)),
96 Some(Commands::Whoami) => Box::pin(handle_whoami()),
97 Some(Commands::Update(cmd)) => Box::pin(handle_update(cmd)),
98 Some(Commands::Version(cmd)) => Box::pin(handle_version(cmd, debug)),
99 Some(Commands::Publish(cmd)) => Box::pin(handle_publish(cmd, debug)),
100 Some(Commands::Env { target }) => Box::pin(handle_env(target, debug)),
101 Some(Commands::Tail(cmd)) => Box::pin(handle_tail(cmd, debug)),
102 Some(Commands::Start { args }) => Box::pin(handle_start(args, debug)),
103 Some(Commands::Generate(cmd)) => Box::pin(handle_generate(cmd, debug)),
104 Some(Commands::Api(cmd)) => Box::pin(handle_api(cmd, debug)),
105 Some(Commands::Runners(cmd)) => Box::pin(handle_runners(cmd, debug)),
106 Some(Commands::Mcp(cmd)) => Box::pin(handle_mcp(cmd, debug)),
107 Some(Commands::WorktreeWatch(cmd)) => Box::pin(handle_worktree_watch(cmd, debug)),
108 #[cfg(feature = "docker")]
109 Some(Commands::Docker(cmd)) => Box::pin(handle_docker(cmd, debug)),
110 #[cfg(feature = "secrets")]
111 Some(Commands::Secrets(cmd)) => Box::pin(handle_secrets(cmd, debug)),
112 Some(Commands::Workers(cmd)) => Box::pin(handle_workers(cmd, debug)),
113 Some(Commands::Dns(cmd)) => Box::pin(handle_dns(cmd, debug)),
114 Some(Commands::Domains(cmd)) => Box::pin(handle_domains(cmd, debug)),
115 Some(Commands::Done(cmd)) => Box::pin(handle_done(cmd, debug)),
116 #[cfg(feature = "linear")]
117 Some(Commands::Linear(cmd)) => Box::pin(handle_linear(cmd, debug)),
118 Some(Commands::Github(cmd)) => Box::pin(handle_github(cmd, debug)),
119 Some(Commands::Issues(cmd)) => Box::pin(handle_issues(cmd, debug)),
120 Some(Commands::Todos(cmd)) => Box::pin(handle_todos(cmd, debug)),
121 Some(Commands::FixProcessMonitorJson(cmd)) => {
122 Box::pin(handle_fix_process_monitor_json(cmd, debug))
123 }
124 Some(Commands::Cursor(cmd)) => Box::pin(handle_cursor(cmd)),
125 #[cfg(feature = "kubernetes")]
126 Some(Commands::Kubernetes(cmd)) => Box::pin(handle_kubernetes(cmd, debug)),
127 #[cfg(feature = "nordvpn")]
128 Some(Commands::Nordvpn(cmd)) => Box::pin(handle_nordvpn(cmd, debug)),
129 #[cfg(feature = "monitoring")]
130 Some(Commands::Monitoring(cmd)) => Box::pin(handle_monitoring(cmd)),
131 None => {
132 Box::pin(handle_no_command(port, debug))
134 }
135 }
136}
137
138#[cfg(test)]
139mod tests {
140 use super::*;
141 use clap::Parser;
142
143 #[test]
144 fn dispatch_future_is_boxed_and_small() {
145 let cli = Cli::parse_from(["xbp", "login"]);
146 let mut ctx = AppContext::new(false);
147 let future = dispatch(cli, &mut ctx);
148 assert!(std::mem::size_of_val(&future) <= 2 * std::mem::size_of::<usize>());
149 }
150}