From 1b947ec188bfac145be2149398163941280c6804 Mon Sep 17 00:00:00 2001 From: Federico Terzi Date: Sun, 3 Jul 2022 14:03:45 +0200 Subject: [PATCH] fix(core): add multiple possible backup locations to fix. #1051 (#1282) --- espanso/src/cli/migrate.rs | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/espanso/src/cli/migrate.rs b/espanso/src/cli/migrate.rs index 22ddb8f..a5a5cc6 100644 --- a/espanso/src/cli/migrate.rs +++ b/espanso/src/cli/migrate.rs @@ -177,9 +177,8 @@ fn find_available_backup_dir() -> PathBuf { "".to_string() }; - let target_backup_dir = dirs::document_dir() - .expect("unable to generate backup directory") - .join(format!("espanso-migrate-backup{}", num)); + let target_backup_dir = + find_backup_dir_location().join(format!("espanso-migrate-backup{}", num)); if !target_backup_dir.is_dir() { return target_backup_dir; @@ -189,6 +188,22 @@ fn find_available_backup_dir() -> PathBuf { panic!("could not generate valid backup directory"); } +fn find_backup_dir_location() -> PathBuf { + if let Some(documents_dir) = dirs::document_dir() { + return documents_dir; + } + + if let Some(home_dir) = dirs::home_dir() { + return home_dir; + } + + if let Ok(current_dir) = std::env::current_dir() { + return current_dir; + } + + panic!("unable to generate suitable backup location directory"); +} + fn error_print_and_log(msg: &str) { error!("{}", msg); eprintln!("{}", msg);