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)
This commit is contained in:
Jason
2025-09-05 16:39:12 +08:00
parent ab6be1d510
commit 54003d69e2
3 changed files with 17 additions and 40 deletions

View File

@@ -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(())
}
/// 获取指定应用的管理器

View File

@@ -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)?;
}
}

View File

@@ -88,6 +88,7 @@ pub fn archive_file(ts: u64, category: &str, src: &Path) -> Result<Option<PathBu
Ok(Some(dest))
}
/// 清理供应商名称,确保文件名安全
pub fn sanitize_provider_name(name: &str) -> String {
name.chars()