Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

minor improvements #18

Merged
merged 5 commits into from
Oct 7, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 31 additions & 35 deletions kepler/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use anyhow::{bail, Context, Result};
use anyhow::{Context, Result};
use clap::{Parser, Subcommand};
use domain_db::{cve_sources::nist, db};
use dotenvy::dotenv;
use env_logger::Env;
use lazy_static::lazy_static;
use std::borrow::Cow;
use std::{fs, path::Path};

mod api;
Expand Down Expand Up @@ -60,23 +61,21 @@ async fn main() -> Result<()> {
}

match opts.cmd {
Some(cmd) => match cmd {
Commands::ImportNist {
year,
data_dir,
refresh,
} => {
let data_path = check_data_path(&data_dir);
Some(Commands::ImportNist {
year,
data_dir,
refresh,
}) => {
let data_path = check_data_path(&data_dir);

let (_, cve_list) = nist::download(year, data_path, refresh)?;
let (_, cve_list) = nist::download(year, data_path, refresh)?;

let num_records = import_nist(&repository, cve_list)?;
let num_records = import_nist(&repository, cve_list)?;

let report = report_message(num_records);
let report = report_message(num_records);

log::info!("{report}");
}
},
log::info!("{report}");
}
None => {
let ApiSettings { address, port } = ApiSettings::try_from_env()?;

Expand Down Expand Up @@ -146,20 +145,19 @@ pub fn import_nist(
for item in &cve_list {
let json = serde_json::to_string(item)?;

let object_id = match repository
.create_object_if_not_exist(db::models::NewObject::with(item.id().into(), json))
{
Err(e) => bail!(e),
Ok(id) => id,
};
let object_id = repository
.create_object_if_not_exist(db::models::NewObject::with(item.id().into(), json))?;

let mut refs = Vec::new();
for data in &item.cve.references.reference_data {
refs.push(db::models::Reference {
let refs = item
.cve
.references
.reference_data
.iter()
.map(|data| db::models::Reference {
url: data.url.clone(),
tags: data.tags.clone(),
})
}
.collect::<Vec<_>>();

for product in item.collect_unique_products() {
let new_cve = db::models::NewCVE::with(
Expand All @@ -174,26 +172,24 @@ pub fn import_nist(
refs.clone(),
Some(object_id),
);
match repository.create_cve_if_not_exist(new_cve) {
Err(e) => bail!(e),
Ok(true) => num_imported += 1,
Ok(false) => {}
}

if num_imported > 0 && num_imported % 100 == 0 {
log::info!("imported {} records ...", num_imported);
}
if repository.create_cve_if_not_exist(new_cve)? {
num_imported += 1;
if num_imported % 100 == 0 {
log::info!("imported {} records ...", num_imported);
}
};
}
}

Ok(num_imported)
}

fn report_message(num_records: u32) -> String {
fn report_message(num_records: u32) -> Cow<'static, str> {
if num_records == 0 {
"No new records created".to_string()
Cow::Borrowed("No new records created")
} else {
format!("{num_records} new records created")
Cow::Owned(format!("{num_records} new records created"))
}
}

Expand Down
Loading