From 03bea24f9de698375033af92a08762446d0e20cc Mon Sep 17 00:00:00 2001 From: FxQnLr Date: Sun, 25 Feb 2024 16:14:56 +0100 Subject: Closes #2. Config and setup stuff --- src/config.rs | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) (limited to 'src/config.rs') diff --git a/src/config.rs b/src/config.rs index 9a9e44b..78795a3 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,19 +1,19 @@ -use config::Config; -use once_cell::sync::Lazy; +use serde::Deserialize; -pub static SETTINGS: Lazy = Lazy::new(setup); - -fn setup() -> Config { - #[cfg(not(debug_assertions))] - let builder = Config::builder().add_source(config::File::with_name( - format!("{}/webol-cli.toml", dirs::config_dir().unwrap().to_string_lossy()).as_str(), - )); +#[derive(Deserialize)] +pub struct Config { + pub apikey: String, + pub server: String, +} - #[cfg(debug_assertions)] - let builder = Config::builder().add_source(config::File::with_name("webol-cli.toml")); +impl Config { + pub fn load() -> Result { + let builder = config::Config::builder() + .add_source(config::File::with_name("~/.config/webol-cli.toml")) + .add_source(config::File::with_name("webol-cli.toml")) + .add_source(config::Environment::with_prefix("WEBOL_CLI_").separator("_")) + .build()?; - builder - .add_source(config::Environment::with_prefix("WEBOL_CLI_").separator("_")) - .build() - .unwrap() + builder.try_deserialize() + } } -- cgit v1.2.3