Skip to content

Commit

Permalink
Output: stdout (#232)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexroan committed Mar 18, 2024
1 parent 45c67da commit 0730c16
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 13 deletions.
6 changes: 6 additions & 0 deletions aderyn/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ pub struct CommandLineArgs {
#[arg(short, long)]
no_snippets: bool,

/// Print the output to stdout instead of a file
#[arg(long)]
stdout: bool,

/// Path to aderyn.config.json
#[arg(short, long)]
config_file: Option<String>,
Expand Down Expand Up @@ -81,6 +85,7 @@ fn main() {
scope: cmd_args.scope,
exclude: cmd_args.exclude,
no_snippets: cmd_args.no_snippets,
stdout: cmd_args.stdout,
};

let aderyn_config_path = match cmd_args.config_file {
Expand Down Expand Up @@ -176,6 +181,7 @@ fn main() {
scope: scope_lines,
exclude: args.exclude,
no_snippets: args.no_snippets,
stdout: args.stdout,
};
driver::drive_with(new_args, subscriptions);
}
Expand Down
40 changes: 28 additions & 12 deletions aderyn_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub fn run_with_printer<T>(
reporter: T,
root_rel_path: PathBuf,
no_snippets: bool,
stdout: bool,
) -> Result<(), Box<dyn Error>>
where
T: ReportPrinter<()>,
Expand All @@ -38,6 +39,7 @@ where
reporter,
root_rel_path,
no_snippets,
stdout,
detectors,
)
}
Expand All @@ -48,6 +50,7 @@ pub fn run_with_printer_and_given_detectors<T>(
reporter: T,
root_rel_path: PathBuf,
no_snippets: bool,
stdout: bool,
mut detectors: Vec<Box<dyn IssueDetector>>,
) -> Result<(), Box<dyn Error>>
where
Expand Down Expand Up @@ -104,21 +107,34 @@ where
println!("Detectors run, processing found issues");

println!("Found issues processed. Printing report");
reporter.print_report(
get_markdown_writer(&output_file_path)?,
&report,
context,
root_rel_path,
Some(output_file_path.clone()),
no_snippets,
detectors_used,
)?;

println!("Report printed to {}", output_file_path);
if !stdout {
reporter.print_report(
get_writer(&output_file_path)?,
&report,
context,
root_rel_path,
Some(output_file_path.clone()),
no_snippets,
stdout,
detectors_used,
)?;
println!("Report printed to {}", output_file_path);
} else {
reporter.print_report(
io::stdout(),
&report,
context,
root_rel_path,
Some(output_file_path.clone()),
no_snippets,
stdout,
detectors_used,
)?;
}
Ok(())
}

fn get_markdown_writer(filename: &str) -> io::Result<File> {
fn get_writer(filename: &str) -> io::Result<File> {
let file_path = Path::new(filename);
if let Some(parent_dir) = file_path.parent() {
std::fs::create_dir_all(parent_dir)?;
Expand Down
9 changes: 8 additions & 1 deletion aderyn_core/src/report/json_printer.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::{
io::{Result, Write},
io::{self, Result, Write},
path::PathBuf,
};

Expand Down Expand Up @@ -47,6 +47,7 @@ impl ReportPrinter<()> for JsonPrinter {
_: PathBuf,
_: Option<String>,
_: bool,
stdout: bool,
detectors_used: &[(String, String)],
) -> Result<()> {
let detectors_used_names: Vec<_> = detectors_used.iter().map(|x| x.0.clone()).collect();
Expand All @@ -63,6 +64,12 @@ impl ReportPrinter<()> for JsonPrinter {
detectors_used: detectors_used_names,
};
let value = serde_json::to_value(content).unwrap();
if stdout {
println!("STDOUT START");
let _ = serde_json::to_writer_pretty(io::stdout(), &value);
println!("STDOUT END");
return Ok(());
}
_ = serde_json::to_writer_pretty(writer, &value);
Ok(())
}
Expand Down
1 change: 1 addition & 0 deletions aderyn_core/src/report/markdown_printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ impl ReportPrinter<()> for MarkdownReportPrinter {
root_path: PathBuf,
output_rel_path: Option<String>,
no_snippets: bool,
_: bool,
detectors_used: &[(String, String)],
) -> Result<()> {
self.print_title_and_disclaimer(&mut writer)?;
Expand Down
1 change: 1 addition & 0 deletions aderyn_core/src/report/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub trait ReportPrinter<T> {
root_rel_path: PathBuf,
output_rel_path: Option<String>, // you writer 'W' may or may not be writing a file. Eg: it can simply consume and forget :P
no_snippets: bool,
stdout: bool,
detectors_used: &[(String, String)],
) -> Result<T>;
}
5 changes: 5 additions & 0 deletions aderyn_driver/src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub struct Args {
pub exclude: Option<Vec<String>>,
pub scope: Option<Vec<String>>,
pub no_snippets: bool,
pub stdout: bool,
}

enum Framework {
Expand All @@ -35,6 +36,7 @@ pub fn drive(args: Args) {
JsonPrinter,
root_rel_path,
args.no_snippets,
args.stdout,
)
.unwrap_or_else(|err| {
// Exit with a non-zero exit code
Expand All @@ -50,6 +52,7 @@ pub fn drive(args: Args) {
MarkdownReportPrinter,
root_rel_path,
args.no_snippets,
args.stdout,
)
.unwrap_or_else(|err| {
// Exit with a non-zero exit code
Expand All @@ -74,6 +77,7 @@ pub fn drive_with(args: Args, detectors: Vec<Box<dyn IssueDetector>>) {
JsonPrinter,
root_rel_path,
args.no_snippets,
args.stdout,
detectors,
)
.unwrap_or_else(|err| {
Expand All @@ -90,6 +94,7 @@ pub fn drive_with(args: Args, detectors: Vec<Box<dyn IssueDetector>>) {
MarkdownReportPrinter,
root_rel_path,
args.no_snippets,
args.stdout,
detectors,
)
.unwrap_or_else(|err| {
Expand Down
1 change: 1 addition & 0 deletions aderyn_py/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ fn main() {
no_snippets: false, // TODO support this later
scope: None, // TODO support this later
exclude: None, // TODO support this later
stdout: false, // TODO support this later
};
driver::drive(args);
}
Expand Down
1 change: 1 addition & 0 deletions bot/src/bot_brain/custom_detectors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ pub fn generate_report_for_judge(
exclude,
scope,
no_snippets,
stdout: false,
},
custom_detectors(),
)
Expand Down
1 change: 1 addition & 0 deletions bot/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub fn run() {
exclude: None,
scope: None,
no_snippets: false,
stdout: false,
},
subscriptions,
)
Expand Down
3 changes: 3 additions & 0 deletions bot_example/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ fn main() {
output: format!("bot_reports/{}default_analysis_report.md", cmd_args.prefix).to_string(),
exclude: None,
no_snippets: false,
stdout: false,
scope: None,
});

Expand All @@ -54,6 +55,7 @@ fn main() {
.to_string(),
exclude: None,
no_snippets: false,
stdout: false,
scope: None,
},
subscribe_to, // inject subscriptions here
Expand All @@ -80,6 +82,7 @@ fn main() {
.to_string(),
exclude: None,
no_snippets: false,
stdout: false,
scope: None,
},
subscribe_to_hybrid, // inject subscriptions here
Expand Down
Binary file modified nyth/archive.zip
Binary file not shown.

0 comments on commit 0730c16

Please sign in to comment.