exarch-cli 0.2.9

Command-line utility for secure archive extraction and creation
//! Create command implementation.

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<()> {
    // Check if output exists
    if args.output.exists() && !args.force {
        anyhow::bail!(
            "Output file already exists: {}. Use --force to overwrite",
            args.output.display()
        );
    }

    // Remove existing file if --force
    if args.output.exists() && args.force {
        std::fs::remove_file(&args.output).with_context(|| {
            format!("Failed to remove existing file: {}", args.output.display())
        })?;
    }

    // Build config
    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()
    };

    // Add user exclude patterns to defaults
    config.exclude_patterns.extend(args.exclude.iter().cloned());

    // Create archive with progress if TTY is detected
    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(())
}