配置: 兼容 YAML/JSON 保存与状态识别

This commit is contained in:
jiesen
2025-12-21 01:23:24 +07:00
parent 484e34977a
commit 1295f48eaf
4 changed files with 26 additions and 18 deletions
+12 -3
View File
@@ -38,7 +38,7 @@ pub fn get_config_status(app_type: String) -> Result<ConfigStatus, String> {
AppType::Claude => config_dir.join("settings.json"),
AppType::Codex => config_dir.join("auth.json"),
AppType::Gemini => config_dir.join(".env"),
AppType::ProxyCast => config_dir.join("config.json"),
AppType::ProxyCast => config_dir.join("config.yaml"),
};
let has_env = match app {
@@ -50,11 +50,20 @@ pub fn get_config_status(app_type: String) -> Result<ConfigStatus, String> {
}
AppType::Codex => config_dir.join("auth.json").exists(),
AppType::Gemini => config_dir.join(".env").exists(),
AppType::ProxyCast => config_dir.join("config.json").exists(),
AppType::ProxyCast => {
config_dir.join("config.yaml").exists() || config_dir.join("config.json").exists()
}
};
let exists = match app {
AppType::ProxyCast => {
config_dir.join("config.yaml").exists() || config_dir.join("config.json").exists()
}
_ => main_config.exists(),
};
Ok(ConfigStatus {
exists: main_config.exists(),
exists,
path: config_dir.to_string_lossy().to_string(),
has_env,
})
+5 -1
View File
@@ -678,8 +678,12 @@ pub fn load_config() -> Result<Config, Box<dyn std::error::Error>> {
Ok(Config::default())
}
/// 保存配置(向后兼容,使用 JSON 格式)
/// 保存配置(同时写入 YAML 与 JSON,兼容旧版)
pub fn save_config(config: &Config) -> Result<(), Box<dyn std::error::Error>> {
// 主配置优先写入 YAML
save_config_yaml(config)?;
// 兼容旧版 JSON 配置
let path = json_config_path();
if let Some(parent) = path.parent() {
std::fs::create_dir_all(parent)?;
+8 -13
View File
@@ -17,16 +17,11 @@ pub fn migrate_from_json(conn: &Connection) -> Result<(), String> {
return Ok(());
}
// 读取旧配置文件(兼容历史路径)
let mut legacy_paths = Vec::new();
if let Some(config_dir) = dirs::config_dir() {
legacy_paths.push(config_dir.join("proxycast").join("config.json"));
}
if let Some(home) = dirs::home_dir() {
legacy_paths.push(home.join(".proxycast").join("config.json"));
}
// 读取旧配置文件(历史路径)
let home = dirs::home_dir().ok_or_else(|| "无法获取主目录".to_string())?;
let config_path = home.join(".proxycast").join("config.json");
if let Some(config_path) = legacy_paths.into_iter().find(|path| path.exists()) {
if config_path.exists() {
// 备份旧配置,避免误覆盖
let backup_path = config_path.with_file_name("config.json.backup");
if !backup_path.exists() {
@@ -34,10 +29,10 @@ pub fn migrate_from_json(conn: &Connection) -> Result<(), String> {
.map_err(|e| format!("备份旧配置失败: {}", e))?;
}
return Err(format!(
"检测到旧版 config.json({}),当前版本尚未支持自动迁移。请手动导出/重建配置后再启动。",
config_path.display()
));
return Err(
"检测到旧版 config.json(~/.proxycast/config.json),当前版本尚未支持自动迁移。请手动导出/重建配置后再启动。"
.to_string(),
);
}
// 标记迁移完成
+1 -1
View File
@@ -1365,7 +1365,7 @@ pub fn run() {
if config.server.api_key == config::DEFAULT_API_KEY {
let new_key = generate_api_key();
config.server.api_key = new_key.clone();
if let Err(err) = config::save_config_yaml(&config) {
if let Err(err) = config::save_config(&config) {
tracing::error!("自动生成 API key 失败,无法保存配置,已中止启动: {}", err);
eprintln!("自动生成 API key 失败,无法保存配置,已中止启动: {}", err);
return;