diff options
Diffstat (limited to 'src/config.rs')
-rw-r--r-- | src/config.rs | 39 |
1 files changed, 25 insertions, 14 deletions
diff --git a/src/config.rs b/src/config.rs index 9a9e44b..769269c 100644 --- a/src/config.rs +++ b/src/config.rs | |||
@@ -1,19 +1,30 @@ | |||
1 | use config::Config; | 1 | use serde::Deserialize; |
2 | use once_cell::sync::Lazy; | ||
3 | 2 | ||
4 | pub static SETTINGS: Lazy<Config> = Lazy::new(setup); | 3 | #[derive(Deserialize)] |
4 | pub struct Config { | ||
5 | pub apikey: String, | ||
6 | pub server: String, | ||
7 | } | ||
8 | |||
9 | impl Config { | ||
10 | pub fn load() -> Result<Config, config::ConfigError> { | ||
11 | let config_dir = dirs::config_dir(); | ||
12 | |||
13 | let builder = config::Config::builder(); | ||
5 | 14 | ||
6 | fn setup() -> Config { | 15 | let builder = if let Some(conf) = config_dir { |
7 | #[cfg(not(debug_assertions))] | 16 | let dir = conf.to_string_lossy(); |
8 | let builder = Config::builder().add_source(config::File::with_name( | 17 | builder.add_source(config::File::with_name(format!("{dir}/webol-cli").as_str()).required(false)) |
9 | format!("{}/webol-cli.toml", dirs::config_dir().unwrap().to_string_lossy()).as_str(), | 18 | } else { |
10 | )); | 19 | println!("!No config dir found"); |
20 | builder | ||
21 | }; | ||
11 | 22 | ||
12 | #[cfg(debug_assertions)] | 23 | let build = builder |
13 | let builder = Config::builder().add_source(config::File::with_name("webol-cli.toml")); | 24 | .add_source(config::File::with_name("webol-cli").required(false)) |
25 | .add_source(config::Environment::with_prefix("WEBOL_CLI").separator("_")) | ||
26 | .build()?; | ||
14 | 27 | ||
15 | builder | 28 | build.try_deserialize() |
16 | .add_source(config::Environment::with_prefix("WEBOL_CLI_").separator("_")) | 29 | } |
17 | .build() | ||
18 | .unwrap() | ||
19 | } | 30 | } |