Add CLI command for generating OpenAPI documentation

This commit is contained in:
GW_MC
2025-12-05 17:22:51 +08:00
parent 34ebfaddbc
commit d2b842d933
5 changed files with 128 additions and 8 deletions

45
apps/api/src/cmd.rs Normal file
View File

@@ -0,0 +1,45 @@
use std::pin::Pin;
use std::{future::Future, process::exit};
use clap::{ArgMatches, Command};
pub mod generate_openapi;
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; 1 /* Update this count when adding new commands */],
> =
once_cell::sync::Lazy::new(|| {
[
// Add new commands here
generate_openapi::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);
}