From b2a809d87fc6591bb1bd430acb8284b7bb9f0e13 Mon Sep 17 00:00:00 2001 From: John Vandenberg Date: Mon, 22 Apr 2024 14:05:51 +0800 Subject: [PATCH] Add bin --- Cargo.toml | 10 +++++++++- src/main.rs | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 src/main.rs diff --git a/Cargo.toml b/Cargo.toml index 1c5bc6a..f82655c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,12 +9,20 @@ readme = "README.md" keywords = ["openapi"] categories = ["web-programming::http-server"] +[[bin]] +name = "openapi-linter" +path = "src/main.rs" +required-features = ["serde_json", "serde_yaml"] +doc = false + [dependencies] -heck = "0.4.0" +heck = "0.5.0" indexmap = "2.0.0" lazy_static = "1.4.0" openapiv3 = "2.0.0-rc.1" regex = "1.7.3" +serde_json = { version = "1.0", optional = true } +serde_yaml = { version = "0.9", optional = true } [dev-dependencies] expectorate = "1.0" diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..533773b --- /dev/null +++ b/src/main.rs @@ -0,0 +1,37 @@ +use std::{env, fs::File, path::Path, process}; + +use openapiv3::OpenAPI; + +fn main() { + let args: Vec<_> = env::args().collect(); + if args.len() != 2 { + println!("usage: openapi-linter filename"); + } + let filename = &args[1]; + + //let content = fs::read_to_string(filename).expect("Unable to read file"); + + // Make sure the result parses as a valid OpenAPI spec. + let spec = load_api(&filename); + + // Check for lint errors. + let errors = openapi_lint::validate(&spec); + if !errors.is_empty() { + eprintln!("{}", errors.join("\n\n")); + process::exit(1); + } +} + +fn load_api

(path: P) -> OpenAPI +where + P: AsRef + std::clone::Clone + std::fmt::Debug, +{ + let mut file = File::open(path.clone()).unwrap(); + match serde_json::from_reader(file) { + Ok(json_value) => json_value, + _ => { + file = File::open(path.clone()).unwrap(); + serde_yaml::from_reader(file).expect("file was not valid OpenAPI") + } + } +}