es_fluent_cli/commands/
clean.rs

1//! Clean command implementation.
2
3use crate::commands::{
4    WorkspaceArgs, WorkspaceCrates, parallel_generate, render_generation_results,
5};
6use crate::core::{CliError, GenerationAction};
7use crate::utils::ui;
8use clap::Parser;
9use colored::Colorize as _;
10
11/// Arguments for the clean command.
12#[derive(Parser)]
13pub struct CleanArgs {
14    #[command(flatten)]
15    pub workspace: WorkspaceArgs,
16
17    /// Clean all locales, not just the fallback language.
18    #[arg(long)]
19    pub all: bool,
20
21    /// Dry run - show what would be cleaned without making changes.
22    #[arg(long)]
23    pub dry_run: bool,
24
25    /// Force rebuild of the runner, ignoring the staleness cache.
26    #[arg(long)]
27    pub force_run: bool,
28}
29
30/// Run the clean command.
31pub fn run_clean(args: CleanArgs) -> Result<(), CliError> {
32    let workspace = WorkspaceCrates::discover(args.workspace)?;
33
34    if !workspace.print_discovery(ui::print_header) {
35        return Ok(());
36    }
37
38    let action = GenerationAction::Clean {
39        all_locales: args.all,
40        dry_run: args.dry_run,
41    };
42
43    let results = parallel_generate(
44        &workspace.workspace_info,
45        &workspace.valid,
46        &action,
47        args.force_run,
48    );
49    let has_errors = render_generation_results(
50        &results,
51        |result| {
52            if args.dry_run {
53                if let Some(output) = &result.output {
54                    print!("{}", output);
55                } else if result.changed {
56                    println!(
57                        "{} {} ({} resources)",
58                        format!("{} would be cleaned in", result.name).yellow(),
59                        humantime::format_duration(result.duration)
60                            .to_string()
61                            .green(),
62                        result.resource_count.to_string().cyan()
63                    );
64                } else {
65                    println!("{} {}", "Unchanged:".dimmed(), result.name.bold());
66                }
67            } else if result.changed {
68                ui::print_cleaned(&result.name, result.duration, result.resource_count);
69            } else {
70                println!("{} {}", "Unchanged:".dimmed(), result.name.bold());
71            }
72        },
73        |result| ui::print_generation_error(&result.name, result.error.as_ref().unwrap()),
74    );
75
76    if has_errors {
77        std::process::exit(1);
78    }
79
80    Ok(())
81}