From 545ff4f7ba3bb85e069f55cb893f0c65ef92e3eb Mon Sep 17 00:00:00 2001 From: "B. Petersen" Date: Tue, 23 Jun 2020 00:33:41 +0200 Subject: [PATCH] apply config_defaults only for unset values instead of applying all config_defaults unconditionally after the first configure, a config_defaults for a given key is now applied when this key has never been set before. this way, you can set some keys before calling configure() and also, later versions can add new defaults for new keys (that would require another call to configure() then, however) --- src/config.rs | 4 ++++ src/configure/mod.rs | 12 ++++++++---- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/config.rs b/src/config.rs index df67e217d..2c59d6435 100644 --- a/src/config.rs +++ b/src/config.rs @@ -120,6 +120,10 @@ pub enum Config { } impl Context { + pub async fn config_exists(&self, key: Config) -> bool { + self.sql.get_raw_config(self, key).await.is_some() + } + /// Get a configuration key. Returns `None` if no value is set, and no default value found. pub async fn get_config(&self, key: Config) -> Option { let value = match key { diff --git a/src/configure/mod.rs b/src/configure/mod.rs index 1002cd7cb..7b45e5386 100644 --- a/src/configure/mod.rs +++ b/src/configure/mod.rs @@ -70,16 +70,20 @@ impl Context { async fn inner_configure(&self) -> Result<()> { info!(self, "Configure ..."); - let was_configured_before = self.is_configured().await; let mut param = LoginParam::from_database(self, "").await; let success = configure(self, &mut param).await; if let Some(provider) = provider::get_provider_info(¶m.addr) { - if !was_configured_before { - if let Some(config_defaults) = &provider.config_defaults { - for def in config_defaults.iter() { + if let Some(config_defaults) = &provider.config_defaults { + for def in config_defaults.iter() { + if !self.config_exists(def.key).await { info!(self, "apply config_defaults {}={}", def.key, def.value); self.set_config(def.key, Some(def.value)).await?; + } else { + info!( + self, + "skip already set config_defaults {}={}", def.key, def.value + ); } } }