From 54003d69e2373840dcc7c2600b2a7552c5595c41 Mon Sep 17 00:00:00 2001 From: Jason Date: Fri, 5 Sep 2025 16:39:12 +0800 Subject: [PATCH] refactor(archive): remove archive on provider switches/updates - Remove file archiving when switching providers or updating current provider - Keep archive functionality only for initial migration (one-time operation) - Retain simple .bak backup for config.json on each save - Simplify code by removing unnecessary archive operations from daily workflows This change prevents unlimited archive growth while maintaining data safety through: 1. Initial migration archives for historical data preservation 2. Single .bak file for basic rollback capability 3. All provider configs stored in SSOT (config.json) --- src-tauri/src/app_config.rs | 11 ++++++++- src-tauri/src/commands.rs | 45 +++++-------------------------------- src-tauri/src/config.rs | 1 + 3 files changed, 17 insertions(+), 40 deletions(-) diff --git a/src-tauri/src/app_config.rs b/src-tauri/src/app_config.rs index e60bbc3..c6f1cc6 100644 --- a/src-tauri/src/app_config.rs +++ b/src-tauri/src/app_config.rs @@ -107,7 +107,16 @@ impl MultiAppConfig { /// 保存配置到文件 pub fn save(&self) -> Result<(), String> { let config_path = get_app_config_path(); - write_json_file(&config_path, self) + // 先备份旧版(若存在)到 ~/.cc-switch/config.json.bak,再写入新内容 + if config_path.exists() { + let backup_path = get_app_config_dir().join("config.json.bak"); + if let Err(e) = copy_file(&config_path, &backup_path) { + log::warn!("备份 config.json 到 .bak 失败: {}", e); + } + } + + write_json_file(&config_path, self)?; + Ok(()) } /// 获取指定应用的管理器 diff --git a/src-tauri/src/commands.rs b/src-tauri/src/commands.rs index 43a34dd..16434e9 100644 --- a/src-tauri/src/commands.rs +++ b/src-tauri/src/commands.rs @@ -97,12 +97,7 @@ pub async fn add_provider( match app_type { AppType::Claude => { let settings_path = crate::config::get_claude_settings_path(); - // 归档当前 live 文件 - let ts = std::time::SystemTime::now() - .duration_since(std::time::UNIX_EPOCH) - .unwrap_or_default() - .as_secs(); - let _ = crate::config::archive_file(ts, "claude", &settings_path); + // 直接写入(不做归档) crate::config::write_json_file(&settings_path, &provider.settings_config)?; } AppType::Codex => { @@ -112,13 +107,7 @@ pub async fn add_provider( std::fs::create_dir_all(parent) .map_err(|e| format!("创建 Codex 目录失败: {}", e))?; } - // 归档当前 live 文件 - let ts = std::time::SystemTime::now() - .duration_since(std::time::UNIX_EPOCH) - .unwrap_or_default() - .as_secs(); - let _ = crate::config::archive_file(ts, "codex", &auth_path); - let _ = crate::config::archive_file(ts, "codex", &config_path); + // 直接写入(不做归档) let auth = provider .settings_config .get("auth") @@ -190,12 +179,7 @@ pub async fn update_provider( match app_type { AppType::Claude => { let settings_path = crate::config::get_claude_settings_path(); - // 归档当前 live 文件 - let ts = std::time::SystemTime::now() - .duration_since(std::time::UNIX_EPOCH) - .unwrap_or_default() - .as_secs(); - let _ = crate::config::archive_file(ts, "claude", &settings_path); + // 直接写入(不做归档) crate::config::write_json_file(&settings_path, &provider.settings_config)?; } AppType::Codex => { @@ -205,13 +189,7 @@ pub async fn update_provider( std::fs::create_dir_all(parent) .map_err(|e| format!("创建 Codex 目录失败: {}", e))?; } - // 归档当前 live 文件 - let ts = std::time::SystemTime::now() - .duration_since(std::time::UNIX_EPOCH) - .unwrap_or_default() - .as_secs(); - let _ = crate::config::archive_file(ts, "codex", &auth_path); - let _ = crate::config::archive_file(ts, "codex", &config_path); + // 直接写入(不做归档) let auth = provider .settings_config @@ -368,13 +346,7 @@ pub async fn switch_provider( .map_err(|e| format!("创建 Codex 目录失败: {}", e))?; } - // 备份当前 live 文件到归档(单一数据源:以 cc-switch 配置为主,但保护用户手改历史) - let ts = std::time::SystemTime::now() - .duration_since(std::time::UNIX_EPOCH) - .unwrap_or_default() - .as_secs(); - let _ = crate::config::archive_file(ts, "codex", &auth_path); - let _ = crate::config::archive_file(ts, "codex", &config_path); + // 不做归档,直接写入 // 写 auth.json(必需) let auth = provider @@ -422,12 +394,7 @@ pub async fn switch_provider( std::fs::create_dir_all(parent).map_err(|e| format!("创建目录失败: {}", e))?; } - // 备份当前 live 文件到归档 - let ts = std::time::SystemTime::now() - .duration_since(std::time::UNIX_EPOCH) - .unwrap_or_default() - .as_secs(); - let _ = crate::config::archive_file(ts, "claude", &settings_path); + // 不做归档,直接写入 write_json_file(&settings_path, &provider.settings_config)?; } } diff --git a/src-tauri/src/config.rs b/src-tauri/src/config.rs index 10b39c4..5c18ce5 100644 --- a/src-tauri/src/config.rs +++ b/src-tauri/src/config.rs @@ -88,6 +88,7 @@ pub fn archive_file(ts: u64, category: &str, src: &Path) -> Result String { name.chars()