lean_ctx/core/editor_registry/writers/
mod.rs1use super::types::{ConfigType, EditorTarget};
5
6mod install;
7mod shared;
8mod uninstall;
9
10pub use install::{COMMANDCODE_MCP_INSTRUCTIONS, validate_claude_binary};
11pub use shared::auto_approve_tools;
12pub use uninstall::remove_lean_ctx_mcp_server;
13#[allow(clippy::wildcard_imports)]
16use install::*;
17#[allow(clippy::wildcard_imports)]
18use uninstall::*;
19
20#[derive(Debug, Clone, Copy, PartialEq, Eq)]
21pub enum WriteAction {
22 Created,
23 Updated,
24 Already,
25}
26
27#[derive(Debug, Clone, Copy, Default)]
28pub struct WriteOptions {
29 pub overwrite_invalid: bool,
30}
31
32#[derive(Debug, Clone, PartialEq, Eq)]
33pub struct WriteResult {
34 pub action: WriteAction,
35 pub note: Option<String>,
36}
37
38pub fn write_config(target: &EditorTarget, binary: &str) -> Result<WriteResult, String> {
39 write_config_with_options(target, binary, WriteOptions::default())
40}
41
42pub fn write_config_with_options(
43 target: &EditorTarget,
44 binary: &str,
45 opts: WriteOptions,
46) -> Result<WriteResult, String> {
47 let allow_rule_steering = crate::core::config::Config::load().rules_injection_effective()
48 != crate::core::config::RulesInjection::Off;
49
50 write_config_with_options_and_rule_steering(target, binary, opts, allow_rule_steering)
51}
52
53pub fn write_config_with_options_and_rule_steering(
54 target: &EditorTarget,
55 binary: &str,
56 opts: WriteOptions,
57 allow_rule_steering: bool,
58) -> Result<WriteResult, String> {
59 if crate::core::governor_hosts::suppresses_mcp(&target.agent_key, &target.config_path) {
62 return Ok(WriteResult {
63 action: WriteAction::Already,
64 note: Some("governor setup: no MCP server".to_string()),
65 });
66 }
67 if let Some(parent) = target.config_path.parent() {
68 std::fs::create_dir_all(parent).map_err(|e| e.to_string())?;
69 }
70
71 match target.config_type {
72 ConfigType::McpJson => write_mcp_json(target, binary, opts),
73 ConfigType::Zed => write_zed_config(target, binary, opts),
74 ConfigType::Codex => write_codex_config(target, binary),
75 ConfigType::VsCodeMcp => write_vscode_mcp(target, binary, opts),
76 ConfigType::CopilotCli => write_copilot_cli(target, binary, opts),
77 ConfigType::OpenCode => write_opencode_config(target, binary, opts),
78 ConfigType::Crush => write_crush_config(target, binary, opts),
79 ConfigType::JetBrains => write_jetbrains_config(target, binary, opts),
80 ConfigType::Amp => write_amp_config(target, binary, opts),
81 ConfigType::HermesYaml => write_hermes_yaml(target, binary, opts),
82 ConfigType::GeminiSettings => write_gemini_settings(target, binary, opts),
83 ConfigType::QoderSettings => write_qoder_settings(target, binary, opts),
84 ConfigType::AugmentVsCode => write_augment_vscode(target, binary, opts),
85 ConfigType::OpenClaw => write_openclaw_config(target, binary, opts),
86 ConfigType::VibeToml => write_vibe_toml(target, binary, opts),
87 ConfigType::CommandCode => {
88 write_commandcode_config_with_rule_steering(target, binary, opts, allow_rule_steering)
89 }
90 ConfigType::ClineCli => write_cline_cli_config(target, binary, opts),
91 ConfigType::OmpMcp => write_omp_mcp(target, binary, opts),
92 ConfigType::CodeWhale => write_codewhale_config(target, binary, opts),
93 }
94}
95
96pub fn remove_lean_ctx_server(
97 target: &EditorTarget,
98 opts: WriteOptions,
99) -> Result<WriteResult, String> {
100 match target.config_type {
101 ConfigType::McpJson
102 | ConfigType::JetBrains
103 | ConfigType::GeminiSettings
104 | ConfigType::QoderSettings
105 | ConfigType::CommandCode
106 | ConfigType::ClineCli
107 | ConfigType::OmpMcp
108 | ConfigType::CopilotCli => remove_lean_ctx_mcp_server(&target.config_path, opts),
111 ConfigType::VsCodeMcp => remove_lean_ctx_vscode_server(&target.config_path, opts),
112 ConfigType::Codex => remove_lean_ctx_codex_server(&target.config_path),
113 ConfigType::OpenCode | ConfigType::Crush => {
114 remove_lean_ctx_named_json_server(&target.config_path, "mcp", opts)
115 }
116 ConfigType::Zed => {
117 remove_lean_ctx_named_json_server(&target.config_path, "context_servers", opts)
118 }
119 ConfigType::Amp => remove_lean_ctx_amp_server(&target.config_path, opts),
120 ConfigType::HermesYaml => remove_lean_ctx_hermes_yaml_server(&target.config_path),
121 ConfigType::AugmentVsCode => {
122 remove_lean_ctx_augment_vscode_server(&target.config_path, opts)
123 }
124 ConfigType::OpenClaw => remove_lean_ctx_openclaw_server(&target.config_path, opts),
125 ConfigType::VibeToml => remove_lean_ctx_vibe_toml_server(&target.config_path, opts),
126 ConfigType::CodeWhale => {
130 let servers = remove_lean_ctx_named_json_server(&target.config_path, "servers", opts)?;
131 let mcp_servers =
132 remove_lean_ctx_named_json_server(&target.config_path, "mcpServers", opts)?;
133 Ok(if servers.action == WriteAction::Updated {
134 servers
135 } else {
136 mcp_servers
137 })
138 }
139 }
140}
141
142#[cfg(test)]
143mod tests;