From ffedfd00a46147b225c834187fc298e88e60d0c5 Mon Sep 17 00:00:00 2001 From: FxQnLr Date: Mon, 26 Feb 2024 12:43:35 +0100 Subject: Closes #6. Should autodetect config dir --- Cargo.toml | 2 +- src/config.rs | 19 +++++++++++++++---- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 4791c6c..36b08c5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,6 +20,6 @@ indicatif = "0.17" reqwest = { version = "0.11", features = ["blocking"] } serde = "1.0" serde_json = "1.0" -thiserror = "1.0.57" +thiserror = "1.0" tokio = { version = "1.36", features = ["macros", "rt-multi-thread", "io-std"] } tokio-tungstenite = "0.21" diff --git a/src/config.rs b/src/config.rs index d28e111..769269c 100644 --- a/src/config.rs +++ b/src/config.rs @@ -8,12 +8,23 @@ pub struct Config { impl Config { pub fn load() -> Result { - let builder = config::Config::builder() - .add_source(config::File::with_name("~/.config/webol-cli").required(false)) + let config_dir = dirs::config_dir(); + + let builder = config::Config::builder(); + + let builder = if let Some(conf) = config_dir { + let dir = conf.to_string_lossy(); + builder.add_source(config::File::with_name(format!("{dir}/webol-cli").as_str()).required(false)) + } else { + println!("!No config dir found"); + builder + }; + + let build = builder .add_source(config::File::with_name("webol-cli").required(false)) - .add_source(config::Environment::with_prefix("WEBOL_CLI_").separator("_")) + .add_source(config::Environment::with_prefix("WEBOL_CLI").separator("_")) .build()?; - builder.try_deserialize() + build.try_deserialize() } } -- cgit v1.2.3