Skip to content

Commit

Permalink
--save-json-results support
Browse files Browse the repository at this point in the history
  • Loading branch information
abdolence committed Sep 1, 2024
1 parent 682db2b commit 806b23c
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
7 changes: 7 additions & 0 deletions src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::redacters::{
};
use clap::*;
use std::fmt::Display;
use std::path::PathBuf;
use url::Url;

#[derive(Parser, Debug)]
Expand Down Expand Up @@ -51,6 +52,12 @@ pub enum CliCommand {

#[arg(long, help = "Override media type detection using glob patterns such as 'text/plain=*.md'", value_parser = CliCommand::parse_key_val::<mime::Mime, globset::Glob>)]
mime_override: Vec<(mime::Mime, globset::Glob)>,

#[arg(
long,
help = "Save redacted results in JSON format to the specified file"
)]
save_json_results: Option<PathBuf>,
},
#[command(about = "List files in the source")]
Ls {
Expand Down
23 changes: 22 additions & 1 deletion src/commands/copy_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@ use console::{pad_str, Alignment, Style, Term};
use futures::Stream;
use gcloud_sdk::prost::bytes;
use indicatif::*;
use serde::Serialize;
use std::error::Error;
use std::time::{Duration, Instant};

#[derive(Debug, Clone, Serialize)]
pub struct CopyCommandResult {
pub files_copied: usize,
pub files_redacted: usize,
pub files_skipped: usize,
}

Expand Down Expand Up @@ -121,6 +124,7 @@ pub async fn command_copy(
bar.set_length(files_found as u64);

let mut total_files_copied = 0;
let mut total_files_redacted = 0;
let mut total_files_skipped = source_files_result.skipped;
for source_file in source_files {
match transfer_and_redact_file(
Expand All @@ -137,11 +141,16 @@ pub async fn command_copy(
.await?
{
TransferFileResult::Copied => total_files_copied += 1,
TransferFileResult::RedactedAndCopied => {
total_files_redacted += 1;
total_files_copied += 1;
}
TransferFileResult::Skipped => total_files_skipped += 1,
}
}
Ok(CopyCommandResult {
files_copied: total_files_copied,
files_redacted: total_files_redacted,
files_skipped: total_files_skipped,
})
} else {
Expand All @@ -161,10 +170,17 @@ pub async fn command_copy(
{
TransferFileResult::Copied => CopyCommandResult {
files_copied: 1,
files_redacted: 0,
files_skipped: 0,
},
TransferFileResult::RedactedAndCopied => CopyCommandResult {
files_copied: 1,
files_redacted: 1,
files_skipped: 0,
},
TransferFileResult::Skipped => CopyCommandResult {
files_copied: 0,
files_redacted: 0,
files_skipped: 1,
},
},
Expand Down Expand Up @@ -237,6 +253,7 @@ async fn report_copy_info(

enum TransferFileResult {
Copied,
RedactedAndCopied,
Skipped,
}

Expand Down Expand Up @@ -396,7 +413,11 @@ async fn redact_upload_file<
destination_fs
.upload(redacted_result.stream, Some(dest_file_ref))
.await?;
Ok(TransferFileResult::Copied)
if redacted_result.number_of_redactions > 0 {
Ok(TransferFileResult::RedactedAndCopied)
} else {
Ok(TransferFileResult::Copied)
}
}
Ok(_) => {
bar.println(
Expand Down
19 changes: 18 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ async fn handle_args(cli: CliArgs, term: &Term) -> AppResult<()> {
filename_filter,
redacter_args,
mime_override,
save_json_results,
} => {
let options = CopyCommandOptions::new(
filename_filter,
Expand All @@ -87,17 +88,33 @@ async fn handle_args(cli: CliArgs, term: &Term) -> AppResult<()> {
.await?;
term.write_line(
format!(
"\n{} -> {}: {} files processed. {} files skipped.",
"\n{} -> {}: {} files copied ({} redacted). {} files skipped.",
source,
destination,
Style::new()
.bold()
.green()
.apply_to(copy_result.files_copied),
Style::new()
.green()
.dim()
.apply_to(copy_result.files_redacted),
Style::new().yellow().apply_to(copy_result.files_skipped),
)
.as_str(),
)?;
if let Some(json_path) = save_json_results {
let json_result = serde_json::to_string_pretty(&copy_result)?;
let mut file = tokio::fs::File::create(&json_path).await?;
tokio::io::AsyncWriteExt::write_all(&mut file, json_result.as_bytes()).await?;
term.write_line(
format!(
"Results saved to JSON file: {}",
Style::new().bold().apply_to(json_path.display())
)
.as_str(),
)?;
}
}
CliCommand::Ls {
source,
Expand Down

0 comments on commit 806b23c

Please sign in to comment.