配置加载器。 职责: 1. 发现所有可能的配置文件路径 2. 读取存在的配置文件 3. 按优先级合并它们 对应源码: config.rs:159-247
| 64 | # ============================================================ |
| 65 | |
| 66 | class ConfigLoader: |
| 67 | """ |
| 68 | 配置加载器。 |
| 69 | |
| 70 | 职责: |
| 71 | 1. 发现所有可能的配置文件路径 |
| 72 | 2. 读取存在的配置文件 |
| 73 | 3. 按优先级合并它们 |
| 74 | |
| 75 | 对应源码: config.rs:159-247 |
| 76 | """ |
| 77 | |
| 78 | def __init__(self, cwd: str, config_home: str): |
| 79 | """ |
| 80 | 参数: |
| 81 | cwd: 当前工作目录(项目根目录) |
| 82 | config_home: 用户配置目录(通常是 ~/.claude) |
| 83 | """ |
| 84 | self.cwd = cwd |
| 85 | self.config_home = config_home |
| 86 | |
| 87 | @classmethod |
| 88 | def default_for(cls, cwd: str) -> "ConfigLoader": |
| 89 | """使用默认路径创建加载器""" |
| 90 | # 配置目录的查找顺序: |
| 91 | # 1. 环境变量 CLAUDE_CONFIG_HOME |
| 92 | # 2. ~/.claude |
| 93 | config_home = os.environ.get("CLAUDE_CONFIG_HOME") |
| 94 | if not config_home: |
| 95 | home = os.path.expanduser("~") |
| 96 | config_home = os.path.join(home, ".claude") |
| 97 | return cls(cwd, config_home) |
| 98 | |
| 99 | def discover(self) -> list[ConfigEntry]: |
| 100 | """ |
| 101 | 发现所有可能的配置文件路径。 |
| 102 | |
| 103 | 注意:返回的是所有"可能"的路径,不管文件是否存在。 |
| 104 | 实际加载时会跳过不存在的文件。 |
| 105 | |
| 106 | 对应源码: config.rs:185-212 |
| 107 | """ |
| 108 | # ~/.claude.json(旧版路径,向后兼容) |
| 109 | user_legacy = os.path.join(os.path.dirname(self.config_home), ".claude.json") |
| 110 | |
| 111 | return [ |
| 112 | # 用户全局配置(两个位置) |
| 113 | ConfigEntry(ConfigSource.USER, user_legacy), |
| 114 | ConfigEntry(ConfigSource.USER, os.path.join(self.config_home, "settings.json")), |
| 115 | # 项目配置(两个位置) |
| 116 | ConfigEntry(ConfigSource.PROJECT, os.path.join(self.cwd, ".claude.json")), |
| 117 | ConfigEntry(ConfigSource.PROJECT, os.path.join(self.cwd, ".claude", "settings.json")), |
| 118 | # 本地配置(一个位置) |
| 119 | ConfigEntry(ConfigSource.LOCAL, os.path.join(self.cwd, ".claude", "settings.local.json")), |
| 120 | ] |
| 121 | |
| 122 | def load(self) -> "RuntimeConfig": |
| 123 | """ |