1use crate::mcp::models::{ClaudeSettings, McpServerConfig};
2use anyhow::{Context, Result};
3use std::collections::HashMap;
4use std::path::Path;
5
6impl ClaudeSettings {
7 pub fn load_or_default(path: &Path) -> Result<Self> {
11 if path.exists() {
12 crate::utils::read_json_file(path).with_context(|| {
13 format!(
14 "Failed to parse settings file: {}\n\
15 The file may be malformed or contain invalid JSON.",
16 path.display()
17 )
18 })
19 } else {
20 Ok(Self::default())
21 }
22 }
23
24 pub fn save(&self, path: &Path) -> Result<()> {
28 if path.exists() {
30 let backup_path = crate::utils::generate_backup_path(path, "claude-code")?;
32
33 if let Some(backup_dir) = backup_path.parent() {
35 if !backup_dir.exists() {
36 std::fs::create_dir_all(backup_dir).with_context(|| {
37 format!("Failed to create directory: {}", backup_dir.display())
38 })?;
39 }
40 }
41
42 std::fs::copy(path, &backup_path).with_context(|| {
43 format!("Failed to create backup of settings at: {}", backup_path.display())
44 })?;
45 }
46
47 crate::utils::write_json_file(path, self, true)
49 .with_context(|| format!("Failed to write settings to: {}", path.display()))?;
50
51 Ok(())
52 }
53
54 pub fn update_mcp_servers(&mut self, mcp_servers_dir: &Path) -> Result<()> {
59 if !mcp_servers_dir.exists() {
60 return Ok(());
61 }
62
63 let mut agpm_servers = HashMap::new();
64
65 for entry in std::fs::read_dir(mcp_servers_dir).with_context(|| {
67 format!("Failed to read MCP servers directory: {}", mcp_servers_dir.display())
68 })? {
69 let entry = entry?;
70 let path = entry.path();
71
72 if path.extension().is_some_and(|ext| ext == "json") {
73 let server_config: McpServerConfig = crate::utils::read_json_file(&path)
74 .with_context(|| {
75 format!("Failed to parse MCP server file: {}", path.display())
76 })?;
77
78 if let Some(name) = path.file_stem().and_then(|s| s.to_str()) {
80 agpm_servers.insert(name.to_string(), server_config);
81 }
82 }
83 }
84
85 if self.mcp_servers.is_none() {
87 self.mcp_servers = Some(HashMap::new());
88 }
89
90 if let Some(servers) = &mut self.mcp_servers {
92 servers
94 .retain(|_, config| config.agpm_metadata.as_ref().is_none_or(|meta| !meta.managed));
95
96 servers.extend(agpm_servers);
98 }
99
100 Ok(())
101 }
102}