mirror of
https://github.com/OMGeeky/advisory-db.git
synced 2025-12-31 00:30:25 +01:00
Convert CI check process to use a bin instead of testing a lib
This allows us to add some additional commands to the Rust-based part of this crate.
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
language: rust
|
||||
script: cargo run check # check that the advisory-db is well-formed
|
||||
|
||||
branches:
|
||||
only:
|
||||
|
||||
@@ -9,5 +9,10 @@ documentation = "https://github.com/rustsec/advisory-db"
|
||||
categories = ["api-bindings", "development-tools"]
|
||||
keywords = ["rustsec", "security", "advisory", "vulnerability"]
|
||||
|
||||
[[bin]]
|
||||
name = "rustsec-advisory-db"
|
||||
|
||||
[dependencies]
|
||||
gumdrop = "0.4"
|
||||
gumdrop_derive = "0.4"
|
||||
rustsec = "0.7"
|
||||
|
||||
20
src/lib.rs
20
src/lib.rs
@@ -1,20 +0,0 @@
|
||||
extern crate rustsec;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use rustsec::{AdvisoryDatabase, Repository};
|
||||
|
||||
#[test]
|
||||
fn advisories_toml_is_well_formed() {
|
||||
let repo = Repository::open(".").unwrap();
|
||||
|
||||
// Ensure Advisories.toml parses
|
||||
let advisory_count = AdvisoryDatabase::from_repository(&repo)
|
||||
.unwrap()
|
||||
.advisories()
|
||||
.count();
|
||||
|
||||
// Ensure we're parsing some advisories
|
||||
assert!(advisory_count > 5);
|
||||
}
|
||||
}
|
||||
85
src/main.rs
Normal file
85
src/main.rs
Normal file
@@ -0,0 +1,85 @@
|
||||
extern crate gumdrop;
|
||||
#[macro_use]
|
||||
extern crate gumdrop_derive;
|
||||
extern crate rustsec;
|
||||
|
||||
use gumdrop::Options;
|
||||
use rustsec::{AdvisoryDatabase, Repository};
|
||||
use std::{env, process::exit};
|
||||
|
||||
const MIN_EXPECTED_ADVISORIES: usize = 5;
|
||||
|
||||
/// Subcommands
|
||||
#[derive(Debug, Options)]
|
||||
enum Opts {
|
||||
#[options(help = "show help for a command")]
|
||||
Help(HelpOpts),
|
||||
|
||||
#[options(help = "check the advisory DB is well-formed")]
|
||||
Check(CheckOpts),
|
||||
}
|
||||
|
||||
/// Options for the `help` command
|
||||
#[derive(Debug, Default, Options)]
|
||||
struct HelpOpts {
|
||||
#[options(free)]
|
||||
commands: Vec<String>,
|
||||
}
|
||||
|
||||
/// Options for the `check` command
|
||||
#[derive(Debug, Default, Options)]
|
||||
struct CheckOpts {}
|
||||
|
||||
fn main() {
|
||||
let args: Vec<_> = env::args().collect();
|
||||
|
||||
let opts = Opts::parse_args_default(&args[1..]).unwrap_or_else(|e| {
|
||||
match e.to_string().as_ref() {
|
||||
// Show usage if no command name is given or if "help" is given
|
||||
"missing command name" => help(&[]),
|
||||
string => eprintln!("{}: {}", args[0], string),
|
||||
}
|
||||
|
||||
exit(2);
|
||||
});
|
||||
|
||||
match opts {
|
||||
Opts::Help(opts) => help(&opts.commands),
|
||||
Opts::Check(_) => check(),
|
||||
}
|
||||
|
||||
exit(0);
|
||||
}
|
||||
|
||||
/// Print help message
|
||||
fn help(_commands: &[String]) {
|
||||
println!("Usage: {} [COMMAND] [OPTIONS]", env::args().next().unwrap());
|
||||
println!();
|
||||
println!("Available commands:");
|
||||
println!();
|
||||
println!("{}", Opts::command_list().unwrap());
|
||||
println!();
|
||||
}
|
||||
|
||||
fn check() {
|
||||
let repo = Repository::open(".").unwrap();
|
||||
|
||||
// Ensure Advisories.toml parses
|
||||
let advisory_count = AdvisoryDatabase::from_repository(&repo)
|
||||
.unwrap()
|
||||
.advisories()
|
||||
.count();
|
||||
|
||||
// Ensure we're parsing some advisories
|
||||
if advisory_count > MIN_EXPECTED_ADVISORIES {
|
||||
println!(
|
||||
"*** Check succeeded! Successfully parsed {} advisories.",
|
||||
advisory_count
|
||||
);
|
||||
} else {
|
||||
panic!(
|
||||
"Missing advisories! Expected at least {}, but got {}",
|
||||
MIN_EXPECTED_ADVISORIES, advisory_count
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user