Skip to content

Commit

Permalink
feat: add pretty print output format
Browse files Browse the repository at this point in the history
  • Loading branch information
louib committed Aug 29, 2023
1 parent b504fb7 commit e60595a
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ struct NixToSBOM {
#[clap(long, short)]
file_path: Option<String>,

/// Output format for the SBOM manifest. Defaults to CycloneDX
/// Output format for the SBOM manifest. Defaults to cdx (CycloneDX).
#[clap(long)]
format: Option<String>,

Expand Down Expand Up @@ -113,6 +113,9 @@ fn main() -> Result<std::process::ExitCode, Box<dyn std::error::Error>> {
);
return Ok(std::process::ExitCode::FAILURE);
}
crate::sbom::Format::PrettyPrint => {
println!("{}", crate::nix::pretty_print_package_graph(&package_graph));
}
};

Ok(std::process::ExitCode::SUCCESS)
Expand Down
59 changes: 59 additions & 0 deletions src/nix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,20 @@ impl Derivation {
}
vec![]
}

pub fn pretty_print(&self) -> Vec<PrettyPrintLine> {
let mut response: Vec<PrettyPrintLine> = vec![];
if let Some(name) = self.get_name() {
response.push(PrettyPrintLine::new(name, 0));
} else {
response.push(PrettyPrintLine::new("unknown derivation?", 0));
}
if let Some(url) = self.get_url() {
response.push(PrettyPrintLine::new(format!("url: {}", url), 1));
}

response
}
}

#[derive(Debug)]
Expand Down Expand Up @@ -206,6 +220,24 @@ pub struct Meta {
pub packages: HashMap<String, PackageMeta>,
}

#[derive(Debug)]
#[derive(Default)]
pub struct PackageURL {
pub scheme: String,
pub host: String,
pub path: Vec<String>,
pub query_params: HashMap<String, String>,
}

impl PackageURL {
pub fn to_string(&self) -> String {
let mut full_path = self.path.join("/");

// TODO add the query params
format!("{}://{}/{}", self.scheme, self.host, full_path)
}
}

#[derive(Debug)]
#[derive(Clone)]
#[derive(Deserialize)]
Expand Down Expand Up @@ -409,8 +441,35 @@ pub struct PackageNode {
pub children: HashSet<String>,
}

impl PackageNode {
pub fn pretty_print(&self) -> Vec<PrettyPrintLine> {
let mut response: Vec<PrettyPrintLine> = vec![];
response
}
}

pub type PackageGraph = HashMap<String, PackageNode>;

pub fn pretty_print_package_graph(package_graph: &PackageGraph) -> String {
let mut lines: Vec<PrettyPrintLine> = vec![];
let mut response = "".to_string();

for (derivation_path, package_node) in package_graph {
for line in package_node.package.pretty_print() {
lines.push(line);
}
for line in package_node.main_derivation.pretty_print() {
lines.push(line);
}
}

for line in lines {
response += &line.to_string();
response += "\n";
}
response
}

// Small struct to make it easier to pretty-print the
// internal representation for the package graph.
#[derive(Debug)]
Expand Down
8 changes: 8 additions & 0 deletions src/sbom.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
pub const CYCLONE_DX_NAME: &str = "CycloneDX";
pub const SPDX_NAME: &str = "SPDX";
pub const PRETTY_PRINT_NAME: &str = "pretty-print";

pub enum Format {
SPDX,
CycloneDX,
PrettyPrint,
}

impl Format {
Expand All @@ -14,20 +16,26 @@ impl Format {
if format.ends_with("cdx") {
return Some(Format::CycloneDX);
}
if format.ends_with("pretty") {
return Some(Format::PrettyPrint);
}
None
}

pub fn to_pretty_name(&self) -> String {
match self {
crate::sbom::Format::CycloneDX => CYCLONE_DX_NAME.to_string(),
crate::sbom::Format::SPDX => SPDX_NAME.to_string(),
crate::sbom::Format::PrettyPrint => PRETTY_PRINT_NAME.to_string(),
}
}

pub fn get_default_serialization_format(&self) -> SerializationFormat {
match self {
crate::sbom::Format::CycloneDX => crate::sbom::SerializationFormat::JSON,
crate::sbom::Format::SPDX => crate::sbom::SerializationFormat::JSON,
// We don't really care which value is returned in this case.
crate::sbom::Format::PrettyPrint => crate::sbom::SerializationFormat::XML,
}
}
}
Expand Down

0 comments on commit e60595a

Please sign in to comment.