Skip to main content

lean_ctx_cli/cli/
rules_dedup.rs

1// SPDX-License-Identifier: Apache-2.0
2//! `lean-ctx rules dedup [--apply]`: report or apply the dedup plan of
3//! [`lean_ctx::core::rules_dedup`].
4
5use lean_ctx::core::rules_dedup::{Action, apply, plan};
6
7pub use lean_ctx::core::rules_dedup::auto_apply;
8
9/// CLI entry: `lean-ctx rules dedup [--apply]`.
10pub fn run(apply_changes: bool) -> i32 {
11    let Some(home) = dirs::home_dir() else {
12        eprintln!("Error: could not determine home directory");
13        return 1;
14    };
15    let project = std::env::current_dir().unwrap_or_else(|_| home.clone());
16    let actions = plan(&home, &project);
17
18    if actions.is_empty() {
19        println!("No duplicated lean-ctx rules found — every client pays once.");
20        return 0;
21    }
22
23    println!(
24        "{} (project: {})\n",
25        if apply_changes {
26            "Deduplicating lean-ctx rules"
27        } else {
28            "Dedup plan (dry-run — pass --apply to execute)"
29        },
30        project.display()
31    );
32
33    let mut fixable = 0usize;
34    for action in &actions {
35        match action {
36            Action::DeleteFile { path, reason } => {
37                fixable += 1;
38                if apply_changes {
39                    println!("  {}", apply(action));
40                } else {
41                    println!("  delete    {}\n            ({reason})", path.display());
42                }
43            }
44            Action::StripBlocks { path, reason } => {
45                fixable += 1;
46                if apply_changes {
47                    println!("  {}", apply(action));
48                } else {
49                    println!("  strip     {}\n            ({reason})", path.display());
50                }
51            }
52            Action::StripCompression { path, reason } => {
53                fixable += 1;
54                if apply_changes {
55                    println!("  {}", apply(action));
56                } else {
57                    println!("  thin      {}\n            ({reason})", path.display());
58                }
59            }
60            Action::Report { .. } => println!("  {}", apply(action)),
61        }
62    }
63
64    if !apply_changes && fixable > 0 {
65        println!("\nRun `lean-ctx rules dedup --apply` to fix {fixable} duplicate(s).");
66    }
67    if apply_changes && fixable > 0 {
68        println!("\nDone. Verify with `lean-ctx doctor overhead`.");
69    }
70    0
71}