| 1 | use crate::path_io::reject_path_symlink; |
| 2 | use crate::persistence; |
| 3 | use crate::provider_toml::CONFIG_FILE_NAME; |
| 4 | use crate::state_dirs::{codewhale_home, codewhale_home_is_explicit, legacy_deepseek_home}; |
| 5 | use anyhow::Context as _; |
| 6 | use anyhow::Result; |
| 7 | use std::path::{Path, PathBuf}; |
| 8 | |
| 9 | pub fn default_config_path() -> Result<PathBuf> { |
| 10 | // Prefer ~/.codewhale/config.toml when it exists (fresh install or |
| 11 | // migrated), otherwise fall back to ~/.deepseek/config.toml. |
| 12 | let primary = codewhale_home()?.join(CONFIG_FILE_NAME); |
| 13 | if codewhale_home_is_explicit() || primary.exists() { |
| 14 | return Ok(primary); |
| 15 | } |
| 16 | let legacy = legacy_deepseek_home()?.join(CONFIG_FILE_NAME); |
| 17 | if legacy.exists() { |
| 18 | return Ok(legacy); |
| 19 | } |
| 20 | // Neither exists — return primary so first write creates it there. |
| 21 | Ok(primary) |
| 22 | } |
| 23 | |
| 24 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 25 | pub struct ConfigMigration { |
| 26 | pub legacy_path: PathBuf, |
| 27 | pub primary_path: PathBuf, |
| 28 | } |
| 29 | |
| 30 | impl ConfigMigration { |
| 31 | pub fn user_notice(&self) -> String { |
| 32 | format!( |
| 33 | "Migrated legacy config from {} to {}. Use the .codewhale path for future edits; the .deepseek file remains only as a compatibility fallback.", |
| 34 | self.legacy_path.display(), |
| 35 | self.primary_path.display() |
| 36 | ) |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | /// v0.8.44: one-time migration from `~/.deepseek/config.toml` to |
| 41 | /// `~/.codewhale/config.toml`. Called on first launch after the config |
| 42 | /// is loaded; copies the legacy file if the primary doesn't exist yet. |
| 43 | /// Never overwrites an existing primary config. |
| 44 | pub fn migrate_config_if_needed() -> Result<Option<ConfigMigration>> { |
| 45 | if codewhale_home_is_explicit() { |
| 46 | return Ok(None); |
| 47 | } |
| 48 | let primary = codewhale_home()?.join(CONFIG_FILE_NAME); |
| 49 | let legacy = legacy_deepseek_home()?.join(CONFIG_FILE_NAME); |
| 50 | migrate_config_between(&legacy, &primary, || {}) |
| 51 | } |
| 52 | |
| 53 | fn migrate_config_between( |
| 54 | legacy: &Path, |
| 55 | primary: &Path, |
| 56 | before_publish: impl FnOnce(), |
| 57 | ) -> Result<Option<ConfigMigration>> { |
| 58 | // `exists()` follows links, so a dangling link would read as "absent" and |
| 59 | // the copy below would create whatever it points at. A link at the primary |
| 60 | // name is refused instead, dangling or not. |
| 61 | reject_path_symlink(primary)?; |
| 62 | if primary.exists() { |
| 63 | return Ok(None); |
| 64 | } |
| 65 | if !legacy.exists() { |
| 66 | return Ok(None); |
| 67 | } |
| 68 | // Copy the config to the new home, owner-only, through a temporary file |
| 69 | // published without clobbering (the primary name is never overwritten). |
| 70 | let contents = std::fs::read(legacy).context("failed to read legacy deepseek config")?; |
| 71 | before_publish(); |
| 72 | if !persistence::atomic_write_if_absent(primary, &contents) |
| 73 | .context("failed to migrate config from deepseek to codewhale home")? |
| 74 | { |
| 75 | return Ok(None); |
| 76 | } |
| 77 | tracing::info!( |
| 78 | "Migrated config from {} to {}", |
| 79 | legacy.display(), |
| 80 | primary.display() |
| 81 | ); |
| 82 | Ok(Some(ConfigMigration { |
| 83 | legacy_path: legacy.to_path_buf(), |
| 84 | primary_path: primary.to_path_buf(), |
| 85 | })) |
| 86 | } |
| 87 | |
| 88 | #[cfg(test)] |
| 89 | mod tests { |
| 90 | use super::*; |
| 91 | |
| 92 | #[test] |
| 93 | fn migrate_config_does_not_overwrite_primary_created_after_exists_check() { |
| 94 | let tmp = tempfile::tempdir().expect("tempdir"); |
| 95 | let legacy = tmp.path().join("legacy").join(CONFIG_FILE_NAME); |
| 96 | let primary = tmp.path().join("primary").join(CONFIG_FILE_NAME); |
| 97 | std::fs::create_dir_all(legacy.parent().expect("legacy parent")).expect("legacy dir"); |
| 98 | std::fs::write(&legacy, b"provider = \"deepseek\"\n").expect("legacy config"); |
| 99 | |
| 100 | let created_concurrently = "provider = \"openai\"\n"; |
| 101 | let outcome = migrate_config_between(&legacy, &primary, || { |
| 102 | std::fs::create_dir_all(primary.parent().expect("primary parent")) |
| 103 | .expect("primary dir"); |
| 104 | std::fs::write(&primary, created_concurrently).expect("concurrent primary write"); |
| 105 | }) |
| 106 | .expect("migration check"); |
| 107 | |
| 108 | assert!(outcome.is_none()); |
| 109 | assert_eq!( |
| 110 | std::fs::read_to_string(&primary).expect("primary config"), |
| 111 | created_concurrently |
| 112 | ); |
| 113 | } |
| 114 | } |
| 115 |