lean_ctx_cli/cli/
rules_cmd.rs1use lean_ctx_ext::core::contextops::{self, ContextOps};
3
4pub fn cmd_rules(args: &[String]) {
5 let action = args.first().map_or("help", String::as_str);
6
7 let Some(home) = lean_ctx::core::home::resolve_home_dir() else {
8 eprintln!("Error: could not determine home directory");
9 std::process::exit(1);
10 };
11
12 let project_root = std::env::current_dir().unwrap_or_else(|_| home.clone());
13 let ops = ContextOps::new(&home, &project_root);
14
15 match action {
16 "sync" => cmd_sync(&ops, args),
17 "diff" => cmd_diff(&ops),
18 "lint" => cmd_lint(&ops),
19 "status" => cmd_status(&ops),
20 "init" => cmd_init(&ops),
21 "dedup" => {
22 let apply = args.iter().any(|a| a == "--apply");
23 std::process::exit(crate::cli::rules_dedup::run(apply));
24 }
25 "help" | "--help" | "-h" => print_help(),
26 _ => {
27 eprintln!("Unknown rules action: {action}");
28 print_help();
29 std::process::exit(1);
30 }
31 }
32}
33
34fn cmd_sync(ops: &ContextOps, args: &[String]) {
35 let agent = args.get(1).map(String::as_str);
36
37 let report = if let Some(agent_name) = agent {
38 println!("Syncing rules for {agent_name}...");
39 ops.sync_agent(agent_name)
40 } else {
41 println!("Syncing rules to all detected agents...");
42 ops.sync_all()
43 };
44
45 println!("{}", contextops::format_sync(&report));
46
47 if !report.errors.is_empty() {
48 std::process::exit(1);
49 }
50}
51
52fn cmd_diff(ops: &ContextOps) {
53 let reports = ops.detect_drift();
56 println!("{}", contextops::format_drift(&reports));
57
58 let drifted = reports
59 .iter()
60 .filter(|r| r.status == contextops::DriftStatus::Drifted)
61 .count();
62 if drifted > 0 {
63 println!("\n{drifted} target(s) drifted. Run `lean-ctx rules sync` to fix.");
64 }
65}
66
67fn cmd_lint(ops: &ContextOps) {
68 match ops.lint() {
69 Ok(warnings) => {
70 println!("{}", contextops::format_lint(&warnings));
71 let errors = warnings
72 .iter()
73 .filter(|w| w.severity == contextops::LintSeverity::Error)
74 .count();
75 if errors > 0 {
76 std::process::exit(1);
77 }
78 }
79 Err(e) => {
80 eprintln!("Error: {e}");
81 eprintln!("Run `lean-ctx rules init` first to create .lean-ctx/rules.toml");
82 std::process::exit(1);
83 }
84 }
85}
86
87fn cmd_status(ops: &ContextOps) {
88 let statuses = ops.status();
89 println!("{}", contextops::format_status(&statuses));
90
91 let has_config = ops.has_config();
92 println!();
93 if has_config {
94 println!("Central config: ✓ (.lean-ctx/rules.toml)");
95 } else {
96 println!("Central config: ✗ (run `lean-ctx rules init` to create)");
97 }
98}
99
100fn cmd_init(ops: &ContextOps) {
101 if ops.has_config() {
102 eprintln!("Config already exists at .lean-ctx/rules.toml");
103 eprintln!("Delete it first if you want to reinitialize.");
104 std::process::exit(1);
105 }
106
107 match ops.init() {
108 Ok(_config) => {
109 println!("Created .lean-ctx/rules.toml from existing rules.");
110 println!();
111 println!("Note: rules.toml is consumed by `lean-ctx rules lint` (cross-agent");
112 println!("consistency) and is a user-editable inventory. It is NOT the source");
113 println!("for `rules sync`/`diff` — those (re)generate from lean-ctx's built-in");
114 println!("canonical rules and preserve your own text around the markers.");
115 println!();
116 println!("Next steps:");
117 println!(" 1. Review .lean-ctx/rules.toml");
118 println!(" 2. Run `lean-ctx rules lint` to check consistency");
119 println!(" 3. Run `lean-ctx rules sync` to (re)write the canonical rules block");
120 }
121 Err(e) => {
122 eprintln!("Error: {e}");
123 std::process::exit(1);
124 }
125 }
126}
127
128fn print_help() {
129 println!(
130 "lean-ctx rules — Cross-agent rules governance (ContextOps)\n\
131 \n\
132 USAGE:\n \
133 lean-ctx rules <action> [args]\n\
134 \n\
135 ACTIONS:\n \
136 sync [agent] (Re)write the canonical lean-ctx rules block into all (or one) agent config(s)\n \
137 diff Show drift between the canonical rules and each agent's on-disk block\n \
138 lint Check .lean-ctx/rules.toml for consistency and completeness\n \
139 status Show sync status for all targets\n \
140 init Create .lean-ctx/rules.toml from existing rules\n \
141 dedup [--apply] Remove duplicated lean-ctx rules (#578); dry-run by default\n \
142 help Show this help\n\
143 \n\
144 NOTES:\n \
145 `sync` and `diff` use lean-ctx's built-in canonical rules as the source\n \
146 of truth and preserve your own text around the `<!-- lean-ctx-rules -->`\n \
147 markers. They do NOT read `.lean-ctx/rules.toml` — that file is the input\n \
148 for `lint` and a user-editable inventory created by `init`."
149 );
150}