From f993de132b0e8416eb02e198e2961a7bf38c1f99 Mon Sep 17 00:00:00 2001 From: Mike Hommey Date: Wed, 6 Jan 2021 10:14:38 +0900 Subject: [PATCH] Still read config file from Library/Preferences if it exists --- src/config.rs | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/src/config.rs b/src/config.rs index 3e89289e75..10ab80299b 100644 --- a/src/config.rs +++ b/src/config.rs @@ -546,6 +546,19 @@ fn config_from_env() -> EnvConfig { EnvConfig { cache } } +fn config_file(env_var: &str, leaf: &str) -> PathBuf { + if let Some(env_value) = env::var_os(env_var) { + return env_value.into(); + } + let dirs = + ProjectDirs::from("", ORGANIZATION, APP_NAME).expect("Unable to get config directory"); + let path = dirs.preference_dir().join(leaf); + if path.exists() { + return path; + } + dirs.config_dir().join(leaf) +} + #[derive(Debug, Default, PartialEq, Eq)] pub struct Config { pub caches: Vec, @@ -557,13 +570,7 @@ impl Config { pub fn load() -> Result { let env_conf = config_from_env(); - let file_conf_path = env::var_os("SCCACHE_CONF") - .map(PathBuf::from) - .unwrap_or_else(|| { - let dirs = ProjectDirs::from("", ORGANIZATION, APP_NAME) - .expect("Unable to get config directory"); - dirs.config_dir().join("config") - }); + let file_conf_path = config_file("SCCACHE_CONF", "config"); let file_conf = try_read_config_file(&file_conf_path) .context("Failed to load config file")? .unwrap_or_default(); @@ -641,13 +648,7 @@ impl CachedConfig { } fn file_config_path() -> PathBuf { - env::var_os("SCCACHE_CACHED_CONF") - .map(PathBuf::from) - .unwrap_or_else(|| { - let dirs = ProjectDirs::from("", ORGANIZATION, APP_NAME) - .expect("Unable to get config directory"); - dirs.config_dir().join("cached-config") - }) + config_file("SCCACHE_CACHED_CONF", "cached-config") } fn load_file_config() -> Result { let file_conf_path = &*CACHED_CONFIG_PATH;