From f313e98db31e1a566fd0bdab767345b1a2b7927a Mon Sep 17 00:00:00 2001 From: "Sam A. Horvath-Hunt" Date: Mon, 20 Dec 2021 12:12:49 +0000 Subject: [PATCH] Respect $XDG_DATA_HOME (#416) --- src/config.rs | 45 +++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 41 insertions(+), 4 deletions(-) diff --git a/src/config.rs b/src/config.rs index f23619e..2a08d0d 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,10 +1,15 @@ use crate::arch::Arch; use crate::log_level::LogLevel; +use crate::outln; use crate::path_ext::PathExt; -use dirs::home_dir; +use colored::Colorize; +use dirs::{data_dir, home_dir}; +use std::sync::atomic::{AtomicBool, Ordering}; use structopt::StructOpt; use url::Url; +static HAS_WARNED_DEPRECATED_BASE_DIR: AtomicBool = AtomicBool::new(false); + #[derive(StructOpt, Debug)] pub struct FnmConfig { /// https://nodejs.org/dist/ mirror @@ -85,9 +90,41 @@ impl FnmConfig { } pub fn base_dir_with_default(&self) -> std::path::PathBuf { - self.base_dir - .clone() - .unwrap_or_else(|| home_dir().expect("Can't get home directory").join(".fnm")) + let user_pref = self.base_dir.clone(); + if let Some(dir) = user_pref { + return dir; + } + + let legacy = home_dir() + .map(|dir| dir.join(".fnm")) + .filter(|dir| dir.exists()); + + let modern = data_dir().map(|dir| dir.join("fnm")); + + if let Some(dir) = legacy { + if !HAS_WARNED_DEPRECATED_BASE_DIR.load(Ordering::SeqCst) { + HAS_WARNED_DEPRECATED_BASE_DIR.store(true, Ordering::SeqCst); + + let legacy_str = dir.display().to_string(); + let modern_str = modern.map_or("$XDG_DATA_HOME/fnm".to_string(), |path| { + path.display().to_string() + }); + + outln!( + self#Error, + "{}\n It looks like you have the {} directory on your disk.\n fnm is migrating its default storage location for application data to {}.\n You can read more about it here: {}\n", + "warning:".yellow().bold(), + legacy_str.italic(), + modern_str.italic(), + "https://github.com/schniz/fnm/issues/357".italic() + ); + } + + return dir; + } + + modern + .expect("Can't get data directory") .ensure_exists_silently() }