50 lines
1.2 KiB
Rust
50 lines
1.2 KiB
Rust
mod generate_openapi;
|
|
mod start_server;
|
|
|
|
pub use start_server::start_server;
|
|
|
|
use std::pin::Pin;
|
|
use std::{future::Future, process::exit};
|
|
|
|
use clap::{ArgMatches, Command};
|
|
|
|
pub struct CliCommand {
|
|
pub command: Command,
|
|
pub action: fn(&clap::ArgMatches) -> Pin<Box<dyn std::future::Future<Output = ()> + Send>>,
|
|
}
|
|
|
|
static CLI_COMMANDS: once_cell::sync::Lazy<
|
|
[CliCommand; 2 /* Update this count when adding new commands */],
|
|
> =
|
|
once_cell::sync::Lazy::new(|| {
|
|
[
|
|
// Add new commands here
|
|
generate_openapi::get_cli_command(),
|
|
start_server::get_cli_command(),
|
|
]
|
|
});
|
|
|
|
pub fn get_command() -> Command {
|
|
let mut c = Command::new("cmd");
|
|
|
|
for cmd in CLI_COMMANDS.iter() {
|
|
c = c.subcommand(cmd.command.clone());
|
|
}
|
|
|
|
c
|
|
}
|
|
|
|
pub fn execute(matches: &ArgMatches, help_msg: &str) -> Pin<Box<dyn Future<Output = ()> + Send>> {
|
|
if let Some((subcommand_name, subcommand_matches)) = matches.subcommand() {
|
|
for cmd in CLI_COMMANDS.iter() {
|
|
if cmd.command.get_name() == subcommand_name {
|
|
return (cmd.action)(subcommand_matches);
|
|
}
|
|
}
|
|
}
|
|
|
|
eprintln!("Error: No valid subcommand provided.");
|
|
eprintln!("{}", help_msg);
|
|
exit(1);
|
|
}
|