-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add CLI command to generate an openapi schema
- Loading branch information
1 parent
3e35f41
commit 257e915
Showing
2 changed files
with
54 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
use std::fs::File; | ||
use std::io::Write; | ||
use std::path::PathBuf; | ||
|
||
use async_trait::async_trait; | ||
use clap::Parser; | ||
use tracing::info; | ||
|
||
use crate::app_context::AppContext; | ||
use crate::cli::{RoadsterCli, RunRoadsterCommand}; | ||
|
||
#[derive(Debug, Parser)] | ||
pub struct OpenApiArgs { | ||
/// The file to write the schema to. If not provided, will write to stdout. | ||
#[clap(short, long, value_name = "FILE", value_hint = clap::ValueHint::FilePath)] | ||
pub output: Option<PathBuf>, | ||
/// Whether to pretty-print the schema. Default: false. | ||
#[clap(short, long, default_value_t = false)] | ||
pub pretty_print: bool, | ||
} | ||
|
||
#[async_trait] | ||
impl RunRoadsterCommand for OpenApiArgs { | ||
async fn run(&self, _cli: &RoadsterCli, context: &AppContext) -> anyhow::Result<bool> { | ||
let schema_json = if self.pretty_print { | ||
serde_json::to_string_pretty(context.api.as_ref())? | ||
} else { | ||
serde_json::to_string(context.api.as_ref())? | ||
}; | ||
if let Some(path) = &self.output { | ||
info!("Writing schema to {:?}", path); | ||
write!(File::create(path)?, "{schema_json}")?; | ||
} else { | ||
info!("OpenAPI schema:"); | ||
info!("{schema_json}"); | ||
}; | ||
|
||
Ok(true) | ||
} | ||
} |