Skip to main content

lean_ctx/cli/
init_cmd.rs

1use crate::hooks::to_bash_compatible_path;
2
3pub(crate) fn quiet_enabled() -> bool {
4    matches!(std::env::var("LEAN_CTX_QUIET"), Ok(v) if v.trim() == "1")
5}
6
7macro_rules! qprintln {
8    ($($t:tt)*) => {
9        if !quiet_enabled() {
10            println!($($t)*);
11        }
12    };
13}
14
15pub fn cmd_init(args: &[String]) {
16    let global = args.iter().any(|a| a == "--global" || a == "-g");
17    let project = args.iter().any(|a| a == "--project");
18    let dry_run = args.iter().any(|a| a == "--dry-run");
19    let no_hook = args.iter().any(|a| a == "--no-shell-hook")
20        || crate::core::config::Config::load().shell_hook_disabled_effective();
21
22    let explicit_mode = args
23        .windows(2)
24        .find(|w| w[0] == "--mode")
25        .and_then(|w| crate::hooks::HookMode::from_str_loose(&w[1]));
26
27    if args.windows(2).any(|w| w[0] == "--mode")
28        && !args
29            .windows(2)
30            .any(|w| w[0] == "--mode" && crate::hooks::HookMode::from_str_loose(&w[1]).is_some())
31    {
32        let bad = args
33            .windows(2)
34            .find(|w| w[0] == "--mode")
35            .map_or("?", |w| w[1].as_str());
36        eprintln!("Unknown hook mode: '{bad}'. Valid: mcp, cli-redirect, hybrid");
37        std::process::exit(1);
38    }
39
40    let agents: Vec<&str> = args
41        .windows(2)
42        .filter(|w| w[0] == "--agent")
43        .map(|w| w[1].as_str())
44        .collect();
45
46    if !agents.is_empty() {
47        let cwd = std::env::current_dir().unwrap_or_default();
48        for agent_name in &agents {
49            let mode =
50                explicit_mode.unwrap_or_else(|| crate::hooks::recommend_hook_mode(agent_name));
51            crate::hooks::install_agent_hook_with_mode(agent_name, global, mode);
52            if matches!(mode, crate::hooks::HookMode::CliRedirect) {
53                if let Err(e) = crate::setup::disable_agent_mcp(agent_name, false) {
54                    eprintln!("MCP config for '{agent_name}' not disabled: {e}");
55                }
56            } else if let Err(e) = crate::setup::configure_agent_mcp(agent_name) {
57                eprintln!("MCP config for '{agent_name}' not updated: {e}");
58            }
59            if project {
60                crate::hooks::install_agent_project_hooks(agent_name, &cwd);
61            }
62        }
63        if !global {
64            crate::hooks::install_project_rules_for_agents(&agents);
65        }
66        qprintln!("\nRun 'lean-ctx gain' after using some commands to see your savings.");
67        return;
68    }
69
70    let eval_shell = args
71        .iter()
72        .find(|a| matches!(a.as_str(), "bash" | "zsh" | "fish" | "powershell" | "pwsh"));
73    if let Some(shell) = eval_shell {
74        if !global {
75            super::shell_init::print_hook_stdout(shell);
76            return;
77        }
78    }
79
80    let shell_name = std::env::var("SHELL").unwrap_or_default();
81    let is_zsh = shell_name.contains("zsh");
82    let is_fish = shell_name.contains("fish");
83    let is_powershell = cfg!(windows) && shell_name.is_empty();
84
85    let binary = crate::core::portable_binary::resolve_portable_binary();
86
87    if dry_run {
88        let rc = if is_powershell {
89            "Documents/PowerShell/Microsoft.PowerShell_profile.ps1".to_string()
90        } else if is_fish {
91            "~/.config/fish/config.fish".to_string()
92        } else if is_zsh {
93            "~/.zshrc".to_string()
94        } else {
95            "~/.bashrc".to_string()
96        };
97        qprintln!("\nlean-ctx init --dry-run\n");
98        qprintln!("  Would modify:  {rc}");
99        qprintln!("  Would backup:  {rc}.lean-ctx.bak");
100        qprintln!("  Would alias:   git npm pnpm yarn cargo docker docker-compose kubectl");
101        qprintln!("                 gh pip pip3 ruff go golangci-lint eslint prettier tsc");
102        qprintln!("                 curl wget php composer (24 commands + k)");
103        let data_dir = crate::core::data_dir::lean_ctx_data_dir().map_or_else(
104            |_| "~/.config/lean-ctx/".to_string(),
105            |p| p.to_string_lossy().to_string(),
106        );
107        qprintln!("  Would create:  {data_dir}");
108        qprintln!("  Binary:        {binary}");
109        qprintln!("\n  Safety: aliases auto-fallback to original command if lean-ctx is removed.");
110        qprintln!("\n  Run without --dry-run to apply.");
111        return;
112    }
113
114    if no_hook {
115        qprintln!("Shell hook disabled (--no-shell-hook or shell_hook_disabled config).");
116        qprintln!("MCP tools remain active. Set LEAN_CTX_NO_HOOK=1 to disable at runtime.");
117    } else if is_powershell {
118        super::shell_init::init_powershell(&binary);
119    } else {
120        let bash_binary = to_bash_compatible_path(&binary);
121        if is_fish {
122            super::shell_init::init_fish(&bash_binary);
123        } else {
124            super::shell_init::init_posix(is_zsh, &bash_binary);
125        }
126    }
127
128    if let Ok(lean_dir) = crate::core::data_dir::lean_ctx_data_dir() {
129        if !lean_dir.exists() {
130            let _ = std::fs::create_dir_all(&lean_dir);
131            qprintln!("Created {}", lean_dir.display());
132        }
133    }
134
135    let rc = if is_powershell {
136        "$PROFILE"
137    } else if is_fish {
138        "config.fish"
139    } else if is_zsh {
140        ".zshrc"
141    } else {
142        ".bashrc"
143    };
144
145    qprintln!("\nlean-ctx init complete (24 aliases installed)");
146    qprintln!();
147    qprintln!("  Disable temporarily:  lean-ctx-off");
148    qprintln!("  Re-enable:            lean-ctx-on");
149    qprintln!("  Check status:         lean-ctx-status");
150    qprintln!("  Full uninstall:       lean-ctx uninstall");
151    qprintln!("  Diagnose issues:      lean-ctx doctor");
152    qprintln!("  Preview changes:      lean-ctx init --global --dry-run");
153    qprintln!();
154    if is_powershell {
155        qprintln!("  Restart PowerShell or run: . {rc}");
156    } else {
157        qprintln!("  Restart your shell or run: source ~/{rc}");
158    }
159    qprintln!();
160    qprintln!("For AI tool integration: lean-ctx init --agent <tool> [--mode <mode>]");
161    qprintln!("  Supported: aider, amazonq, amp, antigravity, claude, cline, codex,");
162    qprintln!("    continue, copilot, crush, cursor, emacs, gemini, hermes, jetbrains,");
163    qprintln!("    kiro, neovim, opencode, pi, qoder, qoderwork, qwen, roo, sublime,");
164    qprintln!("    trae, verdent, vscode, windsurf, zed");
165    qprintln!(
166        "  Modes: mcp, cli-redirect, hybrid  (auto-detected per agent, override with --mode)"
167    );
168}
169
170pub fn cmd_init_quiet(args: &[String]) {
171    std::env::set_var("LEAN_CTX_QUIET", "1");
172    cmd_init(args);
173    std::env::remove_var("LEAN_CTX_QUIET");
174}