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