From d7025323b5e39b837e2f0744acbee78650afa458 Mon Sep 17 00:00:00 2001 From: Mendy Man Date: Fri, 14 Mar 2025 10:21:55 -0400 Subject: [PATCH] Add new option `--specified-operations` Allows me to only include operations from a specified list of operations, This will be helpful when I generate the internal JS lib --- src/api.rs | 14 +++++++++++--- src/main.rs | 24 +++++++++++++++++++++--- src/postprocessing.rs | 2 +- 3 files changed, 33 insertions(+), 7 deletions(-) diff --git a/src/api.rs b/src/api.rs index 38655c6..62d724c 100644 --- a/src/api.rs +++ b/src/api.rs @@ -26,6 +26,7 @@ impl Api { paths: openapi::Paths, component_schemas: &IndexMap, include_mode: IncludeMode, + specified_operations: BTreeSet, ) -> anyhow::Result { let mut resources = BTreeMap::new(); @@ -40,9 +41,14 @@ impl Api { } for (method, op) in path_item { - if let Some((res_path, op)) = - Operation::from_openapi(&path, method, op, component_schemas, include_mode) - { + if let Some((res_path, op)) = Operation::from_openapi( + &path, + method, + op, + component_schemas, + include_mode, + &specified_operations, + ) { let resource = get_or_insert_resource(&mut resources, res_path); resource.operations.push(op); } @@ -186,6 +192,7 @@ impl Operation { op: openapi::Operation, component_schemas: &IndexMap, include_mode: IncludeMode, + specified_operations: &BTreeSet, ) -> Option<(Vec, Self)> { let Some(op_id) = op.operation_id else { // ignore operations without an operationId @@ -199,6 +206,7 @@ impl Operation { IncludeMode::OnlyPublic => !x_hidden, IncludeMode::PublicAndHidden => true, IncludeMode::OnlyHidden => x_hidden, + IncludeMode::Specified => specified_operations.contains(&op_id), }; if !include_operation { return None; diff --git a/src/main.rs b/src/main.rs index a019c5b..e8d0bf2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,5 @@ use std::{ + collections::BTreeSet, io::{self, BufWriter, Write as _}, path::PathBuf, }; @@ -43,6 +44,12 @@ enum Command { #[clap(flatten)] flags: GenerateFlags, + + /// The specified operations for --include-mode=specified + /// + /// This expects the operation ID, for example v1.message.create + #[clap(long)] + specified_operations: Vec, }, } @@ -71,6 +78,8 @@ enum IncludeMode { PublicAndHidden, /// Only operations marked with `x-hidden` OnlyHidden, + /// Only include operations specified in `--specified-operations` + Specified, } fn main() -> anyhow::Result<()> { @@ -82,7 +91,9 @@ fn main() -> anyhow::Result<()> { input_file, output_dir, flags, + specified_operations, } = args.command; + let specified_operations = BTreeSet::from_iter(specified_operations); let spec = fs::read_to_string(&input_file)?; @@ -90,7 +101,7 @@ fn main() -> anyhow::Result<()> { match &output_dir { Some(path) => { - analyze_and_generate(spec, template.into(), path, flags)?; + analyze_and_generate(spec, template.into(), path, flags, specified_operations)?; } None => { let output_dir_root = PathBuf::from("out"); @@ -112,7 +123,7 @@ fn main() -> anyhow::Result<()> { .path() .try_into() .context("non-UTF8 tempdir path")?; - analyze_and_generate(spec, template.into(), path, flags)?; + analyze_and_generate(spec, template.into(), path, flags, specified_operations)?; // Persist the TempDir if everything was successful _ = output_dir.into_path(); } @@ -126,11 +137,18 @@ fn analyze_and_generate( template: String, path: &Utf8Path, flags: GenerateFlags, + specified_operations: BTreeSet, ) -> anyhow::Result<()> { let webhooks = get_webhooks(&spec); let mut components = spec.components.unwrap_or_default(); if let Some(paths) = spec.paths { - let api = Api::new(paths, &components.schemas, flags.include_mode).unwrap(); + let api = Api::new( + paths, + &components.schemas, + flags.include_mode, + specified_operations, + ) + .unwrap(); let types = api.types(&mut components.schemas, webhooks); if flags.debug { diff --git a/src/postprocessing.rs b/src/postprocessing.rs index e4d69dc..628a9d6 100644 --- a/src/postprocessing.rs +++ b/src/postprocessing.rs @@ -154,7 +154,7 @@ fn execute_command( bail!("`{command}` failed with exit code {:?}", exit_status.code()); } Err(e) if e.kind() == io::ErrorKind::NotFound => { - bail!("`{command}` not found - run with --no-postprocessing to skip"); + bail!("`{command}` not found - run with --no-postprocess to skip"); } Err(e) => Err(e.into()), }