From e6677c306126cdb865d4fd513224d79c74e561ef Mon Sep 17 00:00:00 2001 From: Ferdinand Schober Date: Fri, 1 Dec 2023 11:16:56 +0100 Subject: [PATCH] Respect XDG_CONFIG_HOME for config.toml location (#41) * Respect XDG_CONFIG_HOME for config.toml location * add option to specify config via commandline closes #39 --- src/config.rs | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/config.rs b/src/config.rs index ea131cc..aac8283 100644 --- a/src/config.rs +++ b/src/config.rs @@ -44,6 +44,7 @@ impl Error for MissingParameter {} impl ConfigToml { pub fn new(path: &str) -> Result> { let config = fs::read_to_string(path)?; + log::info!("using config: \"{path}\""); Ok(toml::from_str::<_>(&config)?) } } @@ -77,10 +78,25 @@ pub struct Config { impl Config { pub fn new() -> Result> { - let config_path = "config.toml"; - let config_toml = match ConfigToml::new(config_path) { + let config_file = "config.toml"; + #[cfg(unix)] let config_path = { + let xdg_config_home = env::var("XDG_CONFIG_HOME") + .unwrap_or(format!("{}/.config", env::var("HOME")?)); + format!("{xdg_config_home}/lan-mouse/{config_file}") + }; + + #[cfg(not(unix))] let config_path = { + let app_data = env::var("LOCALAPPDATA") + .unwrap_or(format!("{}/.config", env::var("USERPROFILE")?)); + format!("{app_data}\\lan-mouse\\{config_file}") + }; + + // --config overrules default location + let config_path = find_arg("--config")?.unwrap_or(config_path); + + let config_toml = match ConfigToml::new(config_path.as_str()) { Err(e) => { - log::error!("config.toml: {e}"); + log::error!("{config_path}: {e}"); log::warn!("Continuing without config file ..."); None },