Add advisory task (#237)

This commit is contained in:
Folyd 2023-03-22 20:57:17 +08:00 committed by GitHub
parent 2fc32aec4d
commit d1ac7bd090
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 49 additions and 0 deletions

1
.gitignore vendored
View file

@ -7,6 +7,7 @@ web-ext-artifacts
node_modules node_modules
target target
docs/public docs/public
docs/static/advisory
manifest.json manifest.json
extension/core extension/core
extension/manage/*.html extension/manage/*.html

View file

@ -21,3 +21,4 @@ libflate = "1"
rayon = "1" rayon = "1"
regex = "1" regex = "1"
argh = "0.1" argh = "0.1"
rustsec = "0"

View file

@ -19,6 +19,7 @@ struct Options {
#[argh(subcommand)] #[argh(subcommand)]
#[non_exhaustive] #[non_exhaustive]
enum Subcommand { enum Subcommand {
Advisory(AdvisoryTask),
Crates(CratesTask), Crates(CratesTask),
Books(BooksTask), Books(BooksTask),
Caniuse(CaniuseTask), Caniuse(CaniuseTask),
@ -34,6 +35,7 @@ pub type Result<T> = std::result::Result<T, Box<dyn Error>>;
fn main() -> Result<()> { fn main() -> Result<()> {
let options: Options = argh::from_env(); let options: Options = argh::from_env();
match options.subcommand { match options.subcommand {
Subcommand::Advisory(cmd) => cmd.execute()?,
Subcommand::Crates(cmd) => cmd.execute()?, Subcommand::Crates(cmd) => cmd.execute()?,
Subcommand::Books(cmd) => cmd.execute()?, Subcommand::Books(cmd) => cmd.execute()?,
Subcommand::Caniuse(cmd) => cmd.execute()?, Subcommand::Caniuse(cmd) => cmd.execute()?,

View file

@ -0,0 +1,43 @@
use std::{collections::HashMap, io::Write, path::Path};
use argh::FromArgs;
use rustsec::{database::Query, Advisory, Collection, Database};
use super::Task;
const ADVISORY_INDEX_PATH: &str = "../docs/static/advisory";
/// Advisory task
#[derive(FromArgs)]
#[argh(subcommand, name = "advisory")]
pub struct AdvisoryTask {}
impl Task for AdvisoryTask {
fn execute(&self) -> crate::Result<()> {
let mut map = HashMap::new();
let db = Database::fetch()?;
for advisory in db
.query(&Query::new().collection(Collection::Crates).withdrawn(false))
.into_iter()
{
map.entry(&advisory.metadata.package)
.or_insert_with(Vec::new)
.push(advisory);
}
for (package, advisories) in map {
generate_advisory_json(package.as_str(), &advisories)?;
}
Ok(())
}
}
fn generate_advisory_json(package: &str, advisory: &[&Advisory]) -> crate::Result<()> {
let path = Path::new(ADVISORY_INDEX_PATH);
if !path.exists() {
std::fs::create_dir(path)?;
}
let mut file = std::fs::File::create(path.join(format!("{package}.json")))?;
let json = serde_json::to_string_pretty(&advisory)?;
file.write_all(json.as_bytes())?;
Ok(())
}

View file

@ -1,3 +1,4 @@
pub use advisory::AdvisoryTask;
pub use books::BooksTask; pub use books::BooksTask;
pub use caniuse::CaniuseTask; pub use caniuse::CaniuseTask;
pub use crates::CratesTask; pub use crates::CratesTask;
@ -7,6 +8,7 @@ pub use rfcs::RfcsTask;
pub use rustc::RustcTask; pub use rustc::RustcTask;
pub use targets::TargetsTask; pub use targets::TargetsTask;
mod advisory;
mod books; mod books;
mod caniuse; mod caniuse;
mod crates; mod crates;