use clap::Args;
use std::path::PathBuf;
#[derive(Debug, Args)]
pub struct AnalyzeArgs {
pub file: PathBuf,
#[arg(long)]
pub detailed: bool,
#[arg(long, default_value = "text")]
pub format: String,
#[arg(short, long)]
pub output: Option<PathBuf>,
#[arg(long, default_value = "default")]
pub threshold_profile: String,
#[arg(long)]
pub sample: Option<usize>,
#[arg(long, value_name = "PACK")]
pub metrics: Vec<String>,
#[arg(long)]
pub locale: Option<String>,
#[command(flatten)]
pub common: super::CommonAnalysisOptions,
}
use anyhow::Result;
use dataprof::OutputFormat;
use std::fs;
use crate::cli::{AnalysisOptions, analyze_file_with_options};
pub fn execute(args: &AnalyzeArgs) -> Result<()> {
let metric_packs = if args.metrics.is_empty() {
None
} else {
let packs: Result<Vec<dataprof::MetricPack>, _> = args
.metrics
.iter()
.map(|s| s.parse::<dataprof::MetricPack>())
.collect();
Some(packs.map_err(|e| anyhow::anyhow!("{}", e))?)
};
let options = AnalysisOptions {
progress: args.common.progress,
chunk_size: args.common.chunk_size,
config: args.common.config.clone(),
sample: args.sample,
verbosity: Some(args.common.verbosity),
metric_packs,
locale: args.locale.clone(),
};
let report = analyze_file_with_options(&args.file, options)?;
let output_format = match args.format.as_str() {
"json" => Some(OutputFormat::Json),
"csv" => Some(OutputFormat::Csv),
"plain" => Some(OutputFormat::Plain),
"text" => Some(OutputFormat::Text),
_ => None, };
dataprof::output::output_with_adaptive_formatter(&report, output_format)?;
if let Some(output_path) = &args.output {
save_output_to_file(output_path, &report, &args.format)?;
}
Ok(())
}
fn save_output_to_file(
path: &std::path::Path,
report: &dataprof::types::ProfileReport,
format: &str,
) -> Result<()> {
let output_format = match format {
"json" => OutputFormat::Json,
"csv" => OutputFormat::Csv,
"plain" => OutputFormat::Plain,
_ => OutputFormat::Text,
};
let formatter = dataprof::output::create_adaptive_formatter_with_format(output_format);
let content = formatter.format_report(report)?;
fs::write(path, content)?;
log::info!("Results saved to: {}", path.display());
Ok(())
}