Skip to main content

lean_ctx/core/editor_registry/writers/
mod.rs

1// Auto-split from the former monolithic writers.rs. Grouped by operation
2// (install/uninstall) + shared helpers; behavior is unchanged.
3
4use super::types::{ConfigType, EditorTarget};
5
6mod install;
7mod shared;
8mod uninstall;
9
10pub use shared::auto_approve_tools;
11pub use uninstall::remove_lean_ctx_mcp_server;
12// Routers below dispatch to every install/uninstall writer; a glob keeps the
13// dispatch table readable and lets the test module reach them via `super::*`.
14#[allow(clippy::wildcard_imports)]
15use install::*;
16#[allow(clippy::wildcard_imports)]
17use uninstall::*;
18
19#[derive(Debug, Clone, Copy, PartialEq, Eq)]
20pub enum WriteAction {
21    Created,
22    Updated,
23    Already,
24}
25
26#[derive(Debug, Clone, Copy, Default)]
27pub struct WriteOptions {
28    pub overwrite_invalid: bool,
29}
30
31#[derive(Debug, Clone, PartialEq, Eq)]
32pub struct WriteResult {
33    pub action: WriteAction,
34    pub note: Option<String>,
35}
36
37pub fn write_config(target: &EditorTarget, binary: &str) -> Result<WriteResult, String> {
38    write_config_with_options(target, binary, WriteOptions::default())
39}
40
41pub fn write_config_with_options(
42    target: &EditorTarget,
43    binary: &str,
44    opts: WriteOptions,
45) -> Result<WriteResult, String> {
46    if let Some(parent) = target.config_path.parent() {
47        std::fs::create_dir_all(parent).map_err(|e| e.to_string())?;
48    }
49
50    match target.config_type {
51        ConfigType::McpJson => write_mcp_json(target, binary, opts),
52        ConfigType::Zed => write_zed_config(target, binary, opts),
53        ConfigType::Codex => write_codex_config(target, binary),
54        ConfigType::VsCodeMcp => write_vscode_mcp(target, binary, opts),
55        ConfigType::CopilotCli => write_copilot_cli(target, binary, opts),
56        ConfigType::OpenCode => write_opencode_config(target, binary, opts),
57        ConfigType::Crush => write_crush_config(target, binary, opts),
58        ConfigType::JetBrains => write_jetbrains_config(target, binary, opts),
59        ConfigType::Amp => write_amp_config(target, binary, opts),
60        ConfigType::HermesYaml => write_hermes_yaml(target, binary, opts),
61        ConfigType::GeminiSettings => write_gemini_settings(target, binary, opts),
62        ConfigType::QoderSettings => write_qoder_settings(target, binary, opts),
63        ConfigType::AugmentVsCode => write_augment_vscode(target, binary, opts),
64    }
65}
66
67pub fn remove_lean_ctx_server(
68    target: &EditorTarget,
69    opts: WriteOptions,
70) -> Result<WriteResult, String> {
71    match target.config_type {
72        ConfigType::McpJson
73        | ConfigType::JetBrains
74        | ConfigType::GeminiSettings
75        | ConfigType::QoderSettings => remove_lean_ctx_mcp_server(&target.config_path, opts),
76        ConfigType::VsCodeMcp | ConfigType::CopilotCli => {
77            remove_lean_ctx_vscode_server(&target.config_path, opts)
78        }
79        ConfigType::Codex => remove_lean_ctx_codex_server(&target.config_path),
80        ConfigType::OpenCode | ConfigType::Crush => {
81            remove_lean_ctx_named_json_server(&target.config_path, "mcp", opts)
82        }
83        ConfigType::Zed => {
84            remove_lean_ctx_named_json_server(&target.config_path, "context_servers", opts)
85        }
86        ConfigType::Amp => remove_lean_ctx_amp_server(&target.config_path, opts),
87        ConfigType::HermesYaml => remove_lean_ctx_hermes_yaml_server(&target.config_path),
88        ConfigType::AugmentVsCode => {
89            remove_lean_ctx_augment_vscode_server(&target.config_path, opts)
90        }
91    }
92}
93
94#[cfg(test)]
95mod tests;