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

View File

@@ -0,0 +1,38 @@
use clap::{Arg, Command};
use utoipa::OpenApi;
use crate::{cmd::CliCommand, routes::ApiDoc};
pub fn get_cli_command() -> CliCommand {
CliCommand {
command: command(),
action,
}
}
fn command() -> Command {
Command::new("generate:openapi")
.arg(
Arg::new("output_path")
.short('o')
.long("output-path")
.value_name("PATH")
.help("Path to output the generated OpenAPI documentation")
.required(true),
)
.about("Generate OpenAPI documentation")
}
fn action(
_matches: &clap::ArgMatches,
) -> std::pin::Pin<Box<dyn std::future::Future<Output = ()> + Send>> {
let output_path = _matches.get_one::<String>("output_path");
let output_path = output_path.unwrap().to_string();
Box::pin(async move {
let doc = ApiDoc::openapi();
let json = doc
.to_pretty_json()
.expect("Failed to serialize OpenAPI doc to JSON");
std::fs::write(&output_path, json).expect("Failed to write OpenAPI doc to file");
})
}