From 6b4d088b28f8bff69ac9e3f5ce9bdd5d7c8accab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jon=20Grythe=20St=C3=B8dle?= Date: Wed, 29 Jul 2020 20:11:43 +0200 Subject: [PATCH] Add config migration With the upgrade to version 3 of `dirs`, the `config_dir` function returns a new path for macOS. This migrates the file present at the old location (now available through the `preferences_dir` function) to the new location. --- src/main.rs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/main.rs b/src/main.rs index a43ee919..523fc342 100644 --- a/src/main.rs +++ b/src/main.rs @@ -79,6 +79,12 @@ fn main() -> Result<()> { return Ok(()); } + // TODO: Remove this when upgrading from v0.8.x is unlikely + // Only run this migration on macOS, as it's the only platform where the config needs to be moved + if cfg!(target_os = "macos") { + migrate_config()?; + } + setup_terminal()?; defer! { shutdown_terminal().expect("shutdown failed"); @@ -244,6 +250,29 @@ fn get_app_config_path() -> Result { Ok(path) } +fn migrate_config() -> Result<()> { + let mut path = dirs::preference_dir().ok_or_else(|| { + anyhow!("failed to find os preference dir.") + })?; + + path.push("gitui"); + if !path.exists() { + return Ok(()); + } + + let config_path = get_app_config_path()?; + let entries = path.read_dir()?.flatten(); + for entry in entries { + let mut config_path = config_path.clone(); + config_path.push(entry.file_name()); + fs::rename(entry.path(), config_path)?; + } + + let _ = fs::remove_dir(path); + + Ok(()) +} + fn setup_logging() -> Result<()> { let mut path = get_app_cache_path()?; path.push("gitui.log");