Skip to content

Commit

Permalink
Merge pull request #32 from CoinFabrik/develop
Browse files Browse the repository at this point in the history
version with better output
  • Loading branch information
matiascabello authored Apr 24, 2024
2 parents cd833d5 + b9c6dd4 commit be0473f
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 30 deletions.
2 changes: 1 addition & 1 deletion apps/cargo-scout-audit/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cargo-scout-audit"
version = "0.2.9"
version = "0.2.10"
edition = "2021"
authors = [
"Agustin Aon <agustin.aon@coinfabrik.com>",
Expand Down
11 changes: 3 additions & 8 deletions apps/cargo-scout-audit/src/output/markdown/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,9 @@ use crate::output::report::{Category, Finding};

use super::utils;

const BANNER_URL: &str = "https://www.example.com/banner.png";

// Generate the header for the report
pub fn generate_header(date: String) -> String {
format!(
"![Banner Scout report]({})\n# Scout Report - {}\n\n",
BANNER_URL, date
)
format!("# Scout Report - {}\n\n", date)
}

// Generate the summary for the report
Expand Down Expand Up @@ -116,7 +111,7 @@ fn generate_finding(finding: &Finding) -> String {
<li>- [ ] Resolved</li></ul>";

format!(
"<tr><td>{}</td><td><a href=\"{}\">{}</a></td><td>{}</td></tr>\n",
finding.id, "link-to-github", finding.span, status_options
"<tr><td>{}</td><td><a>{}</a></td><td>{}</td></tr>\n",
finding.id, finding.span, status_options
)
}
15 changes: 4 additions & 11 deletions apps/cargo-scout-audit/src/output/markdown/lib.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
use std::path::PathBuf;
use anyhow::Result;

use anyhow::{Context, Result};

use crate::output::{report::Report, utils::write_to_file};
use crate::output::report::Report;

use super::generator::{generate_body, generate_header, generate_summary};

const REPORT_MD_PATH: &str = "build/report.md";

// Generates a markdown report from a given `Report` object.
pub fn generate_markdown(report: &Report) -> Result<&'static str> {
pub fn generate_markdown(report: &Report) -> Result<String> {
let mut report_markdown = String::new();

// Header
Expand All @@ -21,8 +17,5 @@ pub fn generate_markdown(report: &Report) -> Result<&'static str> {
// Body
report_markdown.push_str(&generate_body(&report.categories, &report.findings));

write_to_file(&PathBuf::from(REPORT_MD_PATH), report_markdown.as_bytes())
.with_context(|| format!("Failed to write markdown to '{}'", REPORT_MD_PATH))?;

Ok(REPORT_MD_PATH)
Ok(report_markdown)
}
4 changes: 2 additions & 2 deletions apps/cargo-scout-audit/src/output/pdf/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ fn generate_finding(finding: &Finding) -> String {
<li>- [ ] Resolved</li></ul>";

format!(
"<tr><td>{}</td><td><a href=\"{}\">{}</a></td><td>{}</td></tr>\n",
finding.id, "link-to-github", finding.span, status_options
"<tr><td>{}</td><td><a >{}</a></td><td>{}</td></tr>\n",
finding.id, finding.span, status_options
)
}
2 changes: 1 addition & 1 deletion apps/cargo-scout-audit/src/output/pdf/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::output::{report::Report, utils::write_to_file};

use super::generator::{generate_body, generate_header, generate_summary};

const REPORT_MD_PATH: &str = "build/report.html";
const REPORT_MD_PATH: &str = "temp_report.html";

// Generates a markdown report from a given `Report` object.
pub fn generate_pdf(report: &Report) -> Result<&'static str> {
Expand Down
16 changes: 12 additions & 4 deletions apps/cargo-scout-audit/src/output/report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use anyhow::Result;
use chrono::offset::Local;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::collections::HashMap;
use std::path::Path;
use std::{collections::HashMap, os::unix::process::CommandExt};

use super::{html, markdown, pdf};

Expand Down Expand Up @@ -78,17 +78,25 @@ impl Report {
html::generate_html(self)
}

pub fn generate_markdown(&self) -> Result<&'static str> {
pub fn generate_markdown(&self) -> Result<String> {
markdown::generate_markdown(self)
}

pub fn generate_pdf(&self, path: &Path) -> Result<()> {
let temp_html = pdf::generate_pdf(self)?;

std::process::Command::new("wkhtmltopdf")
//probe if wkhtmltopdf is installed
std::process::Command::new("which")
.arg("wkhtmltopdf")
.output()
.expect("Please, install wkhtmltopdf to generate pdf reports.");

let mut child = std::process::Command::new("wkhtmltopdf")
.arg(temp_html)
.arg(path.to_str().unwrap())
.exec();
.spawn()?;

child.wait()?;

std::fs::remove_file(temp_html)?;

Expand Down
6 changes: 3 additions & 3 deletions apps/cargo-scout-audit/src/startup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,14 +389,14 @@ fn run_dylint(
let report = generate_report(content, info, detectors_info);

// Generate Markdown
let markdown_path = report.generate_markdown()?;
let md_text = report.generate_markdown()?;

let mut md_file = match &opts.output_path {
Some(path) => fs::File::create(path)?,
None => fs::File::create("report.html")?,
None => fs::File::create("report.md")?,
};

std::io::Write::write_all(&mut md_file, markdown_path.as_bytes())?;
std::io::Write::write_all(&mut md_file, md_text.as_bytes())?;
}
OutputFormat::Sarif => {
let path = if let Some(path) = opts.output_path {
Expand Down

0 comments on commit be0473f

Please sign in to comment.