Skip to content

Commit

Permalink
refactor save results
Browse files Browse the repository at this point in the history
  • Loading branch information
jonaylor89 committed Aug 26, 2024
1 parent cfd082e commit 116121f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 26 deletions.
22 changes: 11 additions & 11 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use color_eyre::Result;
use sherlock::{
checker::{check_username, CheckOptions},
get_data::{get_default_data, get_json_data},
output::save_results,
output::{save_results, SaveOptions},
sherlock_target_manifest::SherlockTargetManifest,
utils::create_username_variants,
};
Expand Down Expand Up @@ -130,18 +130,18 @@ async fn main() -> Result<()> {
browse: cli.browse,
};

let save_options = SaveOptions {
output_file: cli.output_file,
output_folder: cli.output_folder,
csv: cli.csv,
xlsx: cli.xlsx,
print_all: cli.print_all,
print_found: cli.print_found,
};

for username in username_variants {
let results = check_username(&username, filtered_targets.clone(), &check_options).await?;
save_results(
&username,
results,
cli.output_file.as_ref(),
cli.output_folder.as_ref(),
cli.csv,
cli.xlsx,
cli.print_all,
cli.print_found,
)?;
save_results(&username, results, &save_options)?;
}

Ok(())
Expand Down
41 changes: 26 additions & 15 deletions src/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ use rust_xlsxwriter::Workbook;
use std::fs::File;
use std::io::Write;

/// Options for saving results
pub struct SaveOptions {
pub output_file: Option<String>,
pub output_folder: Option<String>,
pub csv: bool,
pub xlsx: bool,
pub print_all: bool,
pub print_found: bool,
}

/// Save the results to a file.
///
/// # Arguments
Expand All @@ -25,12 +35,7 @@ use std::io::Write;
pub fn save_results(
username: &str,
results: Vec<QueryResult>,
output_file: Option<&String>,
output_folder: Option<&String>,
csv: bool,
xlsx: bool,
print_all: bool,
print_found: bool,
options: &SaveOptions,
) -> Result<()> {
let total_hits = results
.iter()
Expand All @@ -39,12 +44,12 @@ pub fn save_results(

println!("total of {}/{} hits", total_hits, results.len());

if let Some(output_folder) = output_folder {
if let Some(ref output_folder) = options.output_folder {
// make sure the output folder exists
std::fs::create_dir_all(output_folder)?;
}

let output_file = match (output_file, output_folder) {
let output_file = match (&options.output_file, &options.output_folder) {
(Some(output_file), _) => output_file.to_string(),
(None, Some(output_folder)) => format!("{}/{}.txt", output_folder, username),
(None, None) => format!("{}.txt", username),
Expand All @@ -59,11 +64,17 @@ pub fn save_results(

writeln!(file, "Total Websites Username Detected On: {}", total_hits)?;

if csv {
write_csv(username, &results, output_folder, print_all, print_found)?;
if options.csv {
write_csv(
username,
&results,
options.output_folder.as_deref(),
options.print_all,
options.print_found,
)?;
}

if xlsx {
if options.xlsx {
#[cfg(feature = "xlsx")]
write_xlsx(&username, &results, output_folder, print_all, print_found)?;

Expand All @@ -88,7 +99,7 @@ pub fn save_results(
pub fn write_xlsx(
username: &str,
results: &Vec<QueryResult>,
output_folder: Option<&String>,
output_folder: Option<&str>,
print_all: bool,
print_found: bool,
) -> color_eyre::Result<()> {
Expand Down Expand Up @@ -146,7 +157,7 @@ pub fn write_xlsx(
pub fn write_csv(
username: &str,
results: &Vec<QueryResult>,
output_folder: Option<&String>,
output_folder: Option<&str>,
print_all: bool,
print_found: bool,
) -> color_eyre::Result<()> {
Expand All @@ -173,12 +184,12 @@ pub fn write_csv(

writeln!(
csv_report,
"{},{},{},{},{},{},{}",
"{},{},{},{},{:?},{},{}",
username,
result.site_name,
result.url_main,
result.site_url_user,
format!("{:?}", result.status),
result.status,
result.http_status.as_ref().unwrap_or(&0),
response_time_s
)?;
Expand Down

0 comments on commit 116121f

Please sign in to comment.