use super::app::AppContext;
use super::commands::{Cli, Commands};
use super::error::CliResult;
use super::handle_dns;
#[cfg(feature = "docker")]
use super::handle_docker;
use super::handle_domains;
#[cfg(feature = "kubernetes")]
use super::handle_kubernetes;
#[cfg(feature = "monitoring")]
use super::handle_monitoring;
#[cfg(feature = "nordvpn")]
use super::handle_nordvpn;
#[cfg(feature = "secrets")]
use super::handle_secrets;
use super::{
handle_api, handle_cloudflared, handle_commit, handle_config, handle_curl, handle_diag,
handle_done, handle_env, handle_flush, handle_generate, handle_init, handle_install,
handle_list, handle_login, handle_logs, handle_logs_flag, handle_monitor, handle_network,
handle_nginx, handle_no_command, handle_ports, handle_publish, handle_redeploy,
handle_redeploy_v2, handle_resurrect, handle_service, handle_services, handle_setup,
handle_snapshot, handle_ssh, handle_start, handle_stop, handle_tail, handle_version,
};
use std::future::Future;
use std::pin::Pin;
type DispatchFuture<'a> = Pin<Box<dyn Future<Output = CliResult<()>> + 'a>>;
pub fn dispatch(cli: Cli, ctx: &mut AppContext) -> DispatchFuture<'_> {
let debug = ctx.debug();
let Cli {
logs,
list,
port,
command,
..
} = cli;
if logs {
return Box::pin(handle_logs_flag());
}
if list && command.is_none() {
return Box::pin(handle_list(debug));
}
match command {
Some(Commands::Ports(cmd)) => Box::pin(handle_ports(cmd, port, debug)),
Some(Commands::Commit(cmd)) => Box::pin(handle_commit(cmd)),
Some(Commands::Init) => Box::pin(handle_init(debug)),
Some(Commands::Setup) => Box::pin(handle_setup(debug)),
Some(Commands::Redeploy { service_name }) => Box::pin(handle_redeploy(service_name, debug)),
Some(Commands::RedeployV2(cmd)) => Box::pin(handle_redeploy_v2(cmd, debug)),
Some(Commands::Config(cmd)) => Box::pin(handle_config(cmd, debug)),
Some(Commands::Install { list, package }) => Box::pin(handle_install(package, list, debug)),
Some(Commands::Logs(cmd)) => Box::pin(handle_logs(cmd, debug)),
Some(Commands::Ssh(cmd)) => Box::pin(handle_ssh(cmd, debug)),
Some(Commands::Cloudflared(cmd)) => Box::pin(handle_cloudflared(cmd, debug)),
Some(Commands::List) => Box::pin(handle_list(debug)),
Some(Commands::Curl(cmd)) => Box::pin(handle_curl(cmd, debug)),
Some(Commands::Services) => Box::pin(handle_services(debug)),
Some(Commands::Service {
command,
service_name,
}) => Box::pin(handle_service(command, service_name, debug)),
Some(Commands::Nginx(cmd)) => Box::pin(handle_nginx(cmd.command, debug)),
Some(Commands::Network(cmd)) => Box::pin(handle_network(cmd, debug)),
Some(Commands::Diag(cmd)) => Box::pin(handle_diag(cmd, debug)),
Some(Commands::Monitor(cmd)) => Box::pin(handle_monitor(cmd, debug)),
Some(Commands::Snapshot) => Box::pin(handle_snapshot(debug)),
Some(Commands::Resurrect) => Box::pin(handle_resurrect(debug)),
Some(Commands::Stop { target }) => Box::pin(handle_stop(target, debug)),
Some(Commands::Flush { target }) => Box::pin(handle_flush(target, debug)),
Some(Commands::Login) => Box::pin(handle_login()),
Some(Commands::Version(cmd)) => Box::pin(handle_version(cmd, debug)),
Some(Commands::Publish(cmd)) => Box::pin(handle_publish(cmd, debug)),
Some(Commands::Env { target }) => Box::pin(handle_env(target, debug)),
Some(Commands::Tail(cmd)) => Box::pin(handle_tail(cmd, debug)),
Some(Commands::Start { args }) => Box::pin(handle_start(args, debug)),
Some(Commands::Generate(cmd)) => Box::pin(handle_generate(cmd, debug)),
Some(Commands::Api(cmd)) => Box::pin(handle_api(cmd, debug)),
#[cfg(feature = "docker")]
Some(Commands::Docker(cmd)) => Box::pin(handle_docker(cmd, debug)),
#[cfg(feature = "secrets")]
Some(Commands::Secrets(cmd)) => Box::pin(handle_secrets(cmd, debug)),
Some(Commands::Dns(cmd)) => Box::pin(handle_dns(cmd, debug)),
Some(Commands::Domains(cmd)) => Box::pin(handle_domains(cmd, debug)),
Some(Commands::Done(cmd)) => Box::pin(handle_done(cmd, debug)),
#[cfg(feature = "kubernetes")]
Some(Commands::Kubernetes(cmd)) => Box::pin(handle_kubernetes(cmd, debug)),
#[cfg(feature = "nordvpn")]
Some(Commands::Nordvpn(cmd)) => Box::pin(handle_nordvpn(cmd, debug)),
#[cfg(feature = "monitoring")]
Some(Commands::Monitoring(cmd)) => Box::pin(handle_monitoring(cmd)),
None => {
Box::pin(handle_no_command(port, debug))
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use clap::Parser;
#[test]
fn dispatch_future_is_boxed_and_small() {
let cli = Cli::parse_from(["xbp", "login"]);
let mut ctx = AppContext::new(false);
let future = dispatch(cli, &mut ctx);
assert!(std::mem::size_of_val(&future) <= 2 * std::mem::size_of::<usize>());
}
}