Skip to content

Commit 9ddea53

Browse files
feat: make kopium usable as a library
1 parent 9f63975 commit 9ddea53

File tree

5 files changed

+597
-495
lines changed

5 files changed

+597
-495
lines changed

Cargo.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ readme = "./README.md"
1111
repository = "https://github.com/kube-rs/kopium"
1212
keywords = ["kubernetes", "openapi"]
1313
categories = ["command-line-utilities", "parsing"]
14+
default-run = "kopium"
1415

1516
[package.metadata.binstall]
1617
pkg-url = "{ repo }/releases/download/{ version }/kopium-{ target }{ archive-suffix }"
@@ -19,12 +20,15 @@ bin-dir = "kopium-{ target }/{ bin }{ format }"
1920
[[bin]]
2021
doc = false
2122
name = "kopium"
22-
path = "src/main.rs"
23+
path = "src/bin/kopium.rs"
2324

2425
[lib]
2526
name = "kopium"
2627
path = "src/lib.rs"
2728

29+
[lints.clippy]
30+
uninlined_format_args = "allow"
31+
2832
[dependencies]
2933
tokio = { version = "1.46.1", features = ["full"] }
3034
anyhow = "1.0.98"

src/analyzer.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
//! Deals entirely with schema analysis for the purpose of creating output structs + members
2-
use crate::{Container, MapType, Member, Output};
2+
use std::collections::{BTreeMap, HashMap};
3+
34
use anyhow::{bail, Result};
45
use heck::ToUpperCamelCase;
56
use k8s_openapi::apiextensions_apiserver::pkg::apis::apiextensions::v1::{
67
JSONSchemaProps, JSONSchemaPropsOrArray, JSONSchemaPropsOrBool, JSON,
78
};
8-
use std::collections::{BTreeMap, HashMap};
9+
10+
use crate::{Container, MapType, Member, Output};
911

1012
const IGNORED_KEYS: [&str; 3] = ["metadata", "apiVersion", "kind"];
1113

@@ -429,8 +431,10 @@ fn array_recurse_for_type(
429431
if s.type_.is_none() && s.x_kubernetes_preserve_unknown_fields == Some(true) {
430432
return Ok(("Vec<serde_json::Value>".to_string(), level));
431433
}
434+
432435
let inner_array_type = s.type_.clone().unwrap_or_default();
433-
return match inner_array_type.as_ref() {
436+
437+
match inner_array_type.as_ref() {
434438
"object" => {
435439
// Same logic as in `extract_container` to simplify types to maps.
436440
let mut dict_value = None;
@@ -467,7 +471,7 @@ fn array_recurse_for_type(
467471
unknown => {
468472
bail!("unsupported recursive array type \"{unknown}\" for {key}")
469473
}
470-
};
474+
}
471475
}
472476
// maybe fallback to serde_json::Value
473477
_ => bail!("only support single schema in array {}", key),
@@ -580,10 +584,11 @@ fn extract_integer_type(value: &JSONSchemaProps) -> Result<String> {
580584
// unit tests particular schema patterns
581585
#[cfg(test)]
582586
mod test {
583-
use super::{analyze, Config as Cfg};
587+
use std::sync::Once;
588+
584589
use k8s_openapi::apiextensions_apiserver::pkg::apis::apiextensions::v1::JSONSchemaProps;
585590

586-
use std::sync::Once;
591+
use super::{analyze, Config as Cfg};
587592

588593
static START: Once = Once::new();
589594
fn init() {

src/bin/kopium.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#[tokio::main]
2+
async fn main() -> anyhow::Result<()> {
3+
env_logger::init();
4+
5+
// Ignore SIGPIPE errors to avoid having to use let _ = write! everywhere
6+
// See https://github.com/rust-lang/rust/issues/46016
7+
#[cfg(unix)]
8+
unsafe {
9+
libc::signal(libc::SIGPIPE, libc::SIG_DFL);
10+
}
11+
12+
let mut args = <kopium::Kopium as clap::Parser>::parse();
13+
14+
if args.auto {
15+
args.docs = true;
16+
args.schema = "derived".into();
17+
}
18+
if args.schema == "derived" {
19+
let json_schema = kopium::Derive::all("JsonSchema");
20+
21+
if !args.derive.contains(&json_schema) {
22+
args.derive.push(json_schema)
23+
}
24+
}
25+
26+
args.dispatch().await
27+
}

0 commit comments

Comments
 (0)