From afbdfa674686e2e64bfa45a5555bbd013d4b5650 Mon Sep 17 00:00:00 2001 From: Danilo Bargen Date: Thu, 16 Jun 2022 20:57:03 +0200 Subject: [PATCH] Bring back path source for cache directory --- src/config.rs | 23 +++++++++++++++++++---- src/main.rs | 12 ++++++++---- src/types.rs | 10 ++++------ 3 files changed, 31 insertions(+), 14 deletions(-) diff --git a/src/config.rs b/src/config.rs index f5e996a..28c15ef 100644 --- a/src/config.rs +++ b/src/config.rs @@ -240,9 +240,15 @@ pub struct UpdatesConfig { pub auto_update_interval: Duration, } +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct PathWithSource { + pub path: PathBuf, + pub source: PathSource, +} + #[derive(Clone, Debug, PartialEq, Eq)] pub struct DirectoriesConfig { - pub cache_dir: PathBuf, + pub cache_dir: PathWithSource, pub custom_pages_dir: Option, } @@ -290,13 +296,22 @@ impl Config { // overridden using an env variable. This is deprecated and will be // phased out in the future. eprintln!("Warning: The ${} env variable is deprecated, use the `cache_dir` option in the config file instead.", cache_dir_env_var); - PathBuf::from(env_var) + PathWithSource { + path: PathBuf::from(env_var), + source: PathSource::EnvVar, + } } else if let Some(config_value) = raw_config.directories.cache_dir { // If the user explicitly configured a cache directory, use that. - config_value + PathWithSource { + path: config_value, + source: PathSource::ConfigFile, + } } else if let Ok(default_dir) = get_app_root(AppDataType::UserCache, &crate::APP_INFO) { // Otherwise, fall back to the default user cache directory. - default_dir + PathWithSource { + path: default_dir, + source: PathSource::OsConvention, + } } else { // If everything fails, give up bail!("Could not determine user cache directory"); diff --git a/src/main.rs b/src/main.rs index 83cb673..c1151bf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -161,9 +161,13 @@ fn show_paths(config: &Config) { |e| format!("[Error: {}]", e), |(path, _)| path.display().to_string(), ); - let cache_dir = config.directories.cache_dir.display(); + let cache_dir = format!( + "{} ({})", + config.directories.cache_dir.path.display(), + config.directories.cache_dir.source + ); let pages_dir = { - let mut path = config.directories.cache_dir.clone(); + let mut path = config.directories.cache_dir.path.clone(); path.push(TLDR_PAGES_DIR); path.push(""); // Trailing path separator path.display().to_string() @@ -328,8 +332,8 @@ fn main() { } // Initialize cache - let cache = - Cache::new(ARCHIVE_URL, platform, &config.directories.cache_dir).unwrap_or_else(|e| { + let cache = Cache::new(ARCHIVE_URL, platform, &config.directories.cache_dir.path) + .unwrap_or_else(|e| { print_error(enable_styles, &e.context("Could not initialize cache")); process::exit(1); }); diff --git a/src/types.rs b/src/types.rs index 683a81a..f3318a9 100644 --- a/src/types.rs +++ b/src/types.rs @@ -185,16 +185,14 @@ impl LineType { } /// The reason why a certain path (e.g. config path or cache dir) was chosen. -#[derive(Debug, PartialEq, Eq)] +#[derive(Debug, PartialEq, Eq, Copy, Clone)] pub enum PathSource { /// OS convention (e.g. XDG on Linux) OsConvention, /// Env variable (TEALDEER_*) EnvVar, - - #[allow(dead_code)] // Waiting for Pull Request #141 - /// Config file variable - ConfigVar, + /// Config file + ConfigFile, } impl fmt::Display for PathSource { @@ -205,7 +203,7 @@ impl fmt::Display for PathSource { match self { Self::OsConvention => "OS convention", Self::EnvVar => "env variable", - Self::ConfigVar => "config file variable", + Self::ConfigFile => "config file", } ) }