Add CLI command for generating OpenAPI documentation
This commit is contained in:
38
apps/api/src/cmd/generate_openapi.rs
Normal file
38
apps/api/src/cmd/generate_openapi.rs
Normal 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");
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user