Skip to content

Commit e497792

Browse files
committed
Added CLI with clap
1 parent 5546f18 commit e497792

File tree

4 files changed

+174
-22
lines changed

4 files changed

+174
-22
lines changed

Cargo.lock

Lines changed: 123 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

csaf-lib/src/csaf/validation.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::str::FromStr;
33

44
pub enum ValidationError {}
55

6-
#[derive(Debug, PartialEq, Eq, Hash)]
6+
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
77
pub enum ValidationProfile {
88
Basic,
99
Extended,
@@ -78,8 +78,8 @@ pub fn validate_by_profile<VersionedDocument>(
7878
}
7979
}
8080

81-
fn validate_by_test<VersionedDocument>(
82-
target: Box<dyn Validatable<VersionedDocument>>,
81+
pub fn validate_by_test<VersionedDocument>(
82+
target: impl Validatable<VersionedDocument>,
8383
test_id: &str,
8484
) {
8585
todo!()

csaf-validator/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ edition = "2021"
66
[dependencies]
77
csaf-lib = { path = "../csaf-lib" }
88
anyhow = "1.0.93"
9+
clap = { version = "4.5.23", features = ["derive"] }

csaf-validator/src/main.rs

Lines changed: 47 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,59 @@
11
use anyhow::{anyhow, bail, Result};
22
use csaf_lib::csaf::csaf2_0::loader::load_document as load_document_2_0;
33
use csaf_lib::csaf::csaf2_1::loader::load_document as load_document_2_1;
4-
use csaf_lib::csaf::validation::{validate_by_profile, Validatable, ValidationProfile};
4+
use csaf_lib::csaf::validation::{validate_by_profile, validate_by_test, Validatable, ValidationProfile};
55
use std::env;
6+
use clap::Parser;
67

7-
fn main() -> Result<()> {
8-
let args: Vec<String> = env::args().collect();
8+
/// A validator for CSAF documents
9+
#[derive(Parser, Debug)]
10+
#[command(version, about, long_about = None)]
11+
struct Args {
12+
#[arg()]
13+
path: String,
14+
15+
/// Version of CSAF to use
16+
#[arg(short, long, default_value = "2.0")]
17+
csaf_version: String,
918

10-
let path = match args.get(1) {
11-
None => return Err(anyhow!("Please specify a file to validate")),
12-
Some(v) => v,
13-
};
19+
/// The profile to use
20+
#[arg(short, long, default_value = "basic")]
21+
profile: String,
22+
23+
/// Run only the selected test
24+
#[arg(short, long)]
25+
only_test: Option<String>,
26+
}
1427

28+
fn main() -> Result<()> {
29+
let args = Args::parse();
1530
let profile = ValidationProfile::Basic;
1631

1732
// TODO: it would be nice to return the validatable from this match, but this is beyond my
1833
// rust generics knowledge, so a little bit of duplicate code here
19-
let version = args.get(2).cloned().unwrap_or("2.0".to_string());
20-
let result = match version.as_str() {
21-
"2.0" => {
22-
validate_by_profile(load_document_2_0(path)?, profile)
23-
}
24-
"2.1" => {
25-
validate_by_profile(load_document_2_1(path)?, profile)
26-
}
27-
_ => bail!("invalid version"),
28-
};
29-
30-
Ok(result)
34+
if let Some(test_id) = args.only_test {
35+
let result = match args.csaf_version.as_str() {
36+
"2.0" => {
37+
validate_by_test(load_document_2_0(args.path.as_str())?, test_id.as_str())
38+
}
39+
"2.1" => {
40+
validate_by_test(load_document_2_1(args.path.as_str())?, test_id.as_str())
41+
}
42+
_ => bail!("invalid version"),
43+
};
44+
45+
Ok(result)
46+
} else {
47+
let result = match args.csaf_version.as_str() {
48+
"2.0" => {
49+
validate_by_profile(load_document_2_0(args.path.as_str())?, profile)
50+
}
51+
"2.1" => {
52+
validate_by_profile(load_document_2_1(args.path.as_str())?, profile)
53+
}
54+
_ => bail!("invalid version"),
55+
};
56+
57+
Ok(result)
58+
}
3159
}

0 commit comments

Comments
 (0)