use super::common::{BEHAVIOR, FILTERING, INPUT_OUTPUT, MODE, OUTPUT_FORMAT};
use crate::redactor::{RedactConfig, RedactStats, Redactor};
use clap::{Args, ValueHint};
use std::path::PathBuf;
#[derive(Args)]
pub struct RedactArgs {
#[arg(value_hint = ValueHint::FilePath, help_heading = INPUT_OUTPUT)]
file: PathBuf,
#[arg(short, long, value_hint = ValueHint::FilePath, help_heading = INPUT_OUTPUT)]
output: Option<PathBuf>,
#[arg(short, long, help_heading = INPUT_OUTPUT)]
dialect: Option<String>,
#[arg(short, long, value_hint = ValueHint::FilePath, help_heading = INPUT_OUTPUT)]
config: Option<PathBuf>,
#[arg(long, help_heading = MODE)]
generate_config: bool,
#[arg(long, value_delimiter = ',', help_heading = "Inline Strategies")]
null: Vec<String>,
#[arg(long, value_delimiter = ',', help_heading = "Inline Strategies")]
hash: Vec<String>,
#[arg(long, value_delimiter = ',', help_heading = "Inline Strategies")]
fake: Vec<String>,
#[arg(long, value_delimiter = ',', help_heading = "Inline Strategies")]
mask: Vec<String>,
#[arg(long, value_delimiter = ',', help_heading = "Inline Strategies")]
constant: Vec<String>,
#[arg(long, help_heading = MODE)]
seed: Option<u64>,
#[arg(long, default_value = "en", help_heading = MODE)]
locale: String,
#[arg(short, long, value_delimiter = ',', help_heading = FILTERING)]
tables: Vec<String>,
#[arg(short = 'x', long, value_delimiter = ',', help_heading = FILTERING)]
exclude: Vec<String>,
#[arg(long, help_heading = BEHAVIOR)]
strict: bool,
#[arg(short, long, help_heading = OUTPUT_FORMAT)]
progress: bool,
#[arg(long, help_heading = BEHAVIOR)]
dry_run: bool,
#[arg(long, help_heading = OUTPUT_FORMAT)]
json: bool,
#[arg(long, help_heading = BEHAVIOR)]
validate: bool,
}
pub fn run(args: RedactArgs) -> anyhow::Result<()> {
let RedactArgs {
file,
output,
dialect,
config,
generate_config,
null: null_patterns,
hash: hash_patterns,
fake: fake_patterns,
mask: mask_patterns,
constant: constant_patterns,
seed,
locale,
tables,
exclude,
strict,
progress,
dry_run,
json,
validate: validate_only,
} = args;
let output = super::common::dash_is_stdout(output);
let dialect = super::common::resolve_dialect(&file, dialect.as_deref(), json)?;
let redact_config = RedactConfig::builder()
.input(file)
.output(output)
.dialect(dialect)
.config_file(config)
.null_patterns(null_patterns)
.hash_patterns(hash_patterns)
.fake_patterns(fake_patterns)
.mask_patterns(mask_patterns)
.constant_patterns(constant_patterns)
.seed(seed)
.locale(locale)
.tables_filter(if tables.is_empty() {
None
} else {
Some(tables)
})
.exclude(exclude)
.strict(strict)
.progress(progress)
.dry_run(dry_run)
.build()?;
if validate_only {
redact_config.validate()?;
if !json {
eprintln!("Configuration is valid");
} else {
println!(r#"{{"valid": true}}"#);
}
return Ok(());
}
if generate_config {
return crate::redactor::generate_config(&redact_config);
}
let mut redactor = Redactor::new(redact_config)?;
let stats = redactor.run()?;
output_stats(&stats, json);
Ok(())
}
fn output_stats(stats: &RedactStats, json: bool) {
if json {
println!("{}", serde_json::to_string_pretty(stats).unwrap());
} else {
println!("\nRedaction complete:");
println!(" Tables processed: {}", stats.tables_processed);
println!(" Rows redacted: {}", stats.rows_redacted);
println!(" Columns redacted: {}", stats.columns_redacted);
if !stats.warnings.is_empty() {
eprintln!("\nWarnings:");
for warning in &stats.warnings {
eprintln!(" - {}", warning);
}
}
}
}