Skip to content

Commit

Permalink
feat: add support for native output format
Browse files Browse the repository at this point in the history
  • Loading branch information
louib committed Oct 13, 2024
1 parent e045f5e commit 5adac66
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/format.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
pub mod cyclone_dx;
pub mod native;
pub mod spdx;

pub const CYCLONE_DX_NAME: &str = "CycloneDX";
pub const SPDX_NAME: &str = "SPDX";
pub const PRETTY_PRINT_NAME: &str = "pretty-print";
pub const OUT_PATHS_NAME: &str = "pretty-print";
pub const STATS_NAME: &str = "stats";
pub const NATIVE_NAME: &str = "Native nix2sbom format";

pub enum Format {
SPDX,
CycloneDX,
PrettyPrint,
Stats,
Native,
}

impl Format {
Expand All @@ -28,6 +30,9 @@ impl Format {
if format.ends_with("stats") {
return Some(Format::Stats);
}
if format.ends_with("native") {
return Some(Format::Native);
}
None
}

Expand All @@ -37,6 +42,7 @@ impl Format {
Format::SPDX => SPDX_NAME.to_string(),
Format::PrettyPrint => PRETTY_PRINT_NAME.to_string(),
Format::Stats => STATS_NAME.to_string(),
Format::Native => NATIVE_NAME.to_string(),
}
}

Expand All @@ -47,6 +53,7 @@ impl Format {
Format::Stats => SerializationFormat::JSON,
// We don't really care which value is returned in those cases.
Format::PrettyPrint => SerializationFormat::XML,
Format::Native => SerializationFormat::YAML,
}
}

Expand All @@ -69,6 +76,12 @@ impl Format {
Err(s) => Err(anyhow::format_err!("Error dumping manifest: {}", s.to_string())),
};
}
Format::Native => {
return match native::dump(&package_graph, &serialization_format, options) {
Ok(d) => Ok(d),
Err(s) => Err(anyhow::format_err!("Error dumping manifest: {}", s.to_string())),
};
}
Format::PrettyPrint => {
let display_options = crate::nix::DisplayOptions {
print_stdenv: false,
Expand Down
58 changes: 58 additions & 0 deletions src/format/native.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
use serde::{Deserialize, Serialize};

#[derive(Debug)]
#[derive(Deserialize)]
#[derive(Serialize)]
#[derive(Clone)]
#[derive(PartialEq)]
struct NativePackage {
pub id: String,
pub name: Option<String>,
pub version: Option<String>,
pub purl: String,

pub git_urls: Vec<String>,
pub download_urls: Vec<String>,

pub homepages: Vec<String>,

pub source_derivation: String,
// TODO add build derivations and input derivations
}

pub fn dump(
package_graph: &crate::nix::PackageGraph,
_format: &crate::format::SerializationFormat,
options: &crate::nix::DumpOptions,
) -> Result<String, anyhow::Error> {
let mut native_packages: Vec<NativePackage> = vec![];

for package in package_graph.nodes.values() {
let source_derivation = match &package.source_derivation {
Some(derivation) => derivation,
None => continue,
};
let native_package = NativePackage {
id: package.id.clone(),
// name: package.get_name(),
name: Some("".to_string()),
version: package.get_version(),
purl: package.get_purl().to_string(),
git_urls: package.get_git_urls(),
download_urls: package.main_derivation.get_urls(),
homepages: vec![],
source_derivation: source_derivation.to_string(),
};
native_packages.push(native_package);
}

// Sort the native_packages by id
native_packages.sort_by(|a, b| a.id.cmp(&b.id));

let response = match options.pretty {
Some(false) => serde_json::to_string(&native_packages)?,
_ => serde_json::to_string_pretty(&native_packages)?,
};

Ok(response)
}

0 comments on commit 5adac66

Please sign in to comment.