use crate::cli::CreateArgs;
use crate::output::OutputFormatter;
use crate::progress::CliProgress;
use anyhow::Context;
use anyhow::Result;
use exarch_core::CreationConfig;
use exarch_core::NoopProgress;
use exarch_core::create_archive_with_progress;
pub fn execute(args: &CreateArgs, formatter: &dyn OutputFormatter, quiet: bool) -> Result<()> {
if args.output.exists() && !args.force {
anyhow::bail!(
"Output file already exists: {}. Use --force to overwrite",
args.output.display()
);
}
if args.output.exists() && args.force {
std::fs::remove_file(&args.output).with_context(|| {
format!("Failed to remove existing file: {}", args.output.display())
})?;
}
let mut config = CreationConfig {
follow_symlinks: args.follow_symlinks,
include_hidden: args.include_hidden,
compression_level: args.compression_level,
strip_prefix: args.strip_prefix.clone(),
..Default::default()
};
config.exclude_patterns.extend(args.exclude.iter().cloned());
let report = if !quiet && CliProgress::should_show() {
let mut progress = CliProgress::new(100, "Creating");
create_archive_with_progress(&args.output, &args.sources, &config, &mut progress)
.with_context(|| format!("Failed to create archive: {}", args.output.display()))?
} else {
let mut noop = NoopProgress;
create_archive_with_progress(&args.output, &args.sources, &config, &mut noop)
.with_context(|| format!("Failed to create archive: {}", args.output.display()))?
};
if !quiet {
formatter.format_creation_result(&args.output, &report)?;
}
Ok(())
}