From df4fa9e073efd91a9acbc7391fec5fc35c5e9872 Mon Sep 17 00:00:00 2001
From: librelois <c@elo.tf>
Date: Sat, 8 May 2021 21:40:36 +0200
Subject: [PATCH] fix(conf): load module conf from env var must use uppercased
 module name

---
 Cargo.lock      |  1 +
 conf/Cargo.toml |  1 +
 conf/src/lib.rs | 34 ++++++++++++++++++++--------------
 3 files changed, 22 insertions(+), 14 deletions(-)

diff --git a/Cargo.lock b/Cargo.lock
index 5a73626..2af0f7e 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -788,6 +788,7 @@ dependencies = [
  "dubp",
  "envy",
  "futures-util",
+ "log",
  "serde",
  "serde_json",
 ]
diff --git a/conf/Cargo.toml b/conf/Cargo.toml
index 35501c0..9843943 100644
--- a/conf/Cargo.toml
+++ b/conf/Cargo.toml
@@ -10,5 +10,6 @@ anyhow = "1.0.34"
 dubp = { version = "0.53.1", features = ["duniter"] }
 envy = "0.4.2"
 futures-util = "0.3"
+log = "0.4"
 serde = { version = "1.0.105", features = ["derive"] }
 serde_json = "1.0.53"
diff --git a/conf/src/lib.rs b/conf/src/lib.rs
index 24e9d20..77de2fa 100644
--- a/conf/src/lib.rs
+++ b/conf/src/lib.rs
@@ -57,20 +57,26 @@ pub fn load_module_conf<C: Default + DeserializeOwned>(
     profile_path_opt: &Option<PathBuf>,
 ) -> anyhow::Result<C> {
     if let Some(ref profile_path) = profile_path_opt {
-        if let Ok(conf) = envy::prefixed(format!("DUNITER_{}_", module_name)).from_env::<C>() {
-            Ok(conf)
-        } else {
-            let conf_file_path = find_module_conf_file_path(module_name, profile_path)?;
-            if conf_file_path.exists() {
-                let mut file = std::fs::File::open(conf_file_path)?;
-                let mut contents = String::new();
-                use std::io::Read as _;
-                file.read_to_string(&mut contents)?;
-                Ok(serde_json::from_str::<C>(&contents).with_context(|| {
-                    format!("Invalid configuration for module '{}'", module_name)
-                })?)
-            } else {
-                Ok(C::default())
+        match envy::prefixed(format!("DUNITER_{}_", module_name.to_uppercase())).from_env::<C>() {
+            Ok(conf) => Ok(conf),
+            Err(e) => {
+                log::debug!(
+                    "load_module_conf(module_name: {}): envy error={}",
+                    module_name,
+                    e
+                );
+                let conf_file_path = find_module_conf_file_path(module_name, profile_path)?;
+                if conf_file_path.exists() {
+                    let mut file = std::fs::File::open(conf_file_path)?;
+                    let mut contents = String::new();
+                    use std::io::Read as _;
+                    file.read_to_string(&mut contents)?;
+                    Ok(serde_json::from_str::<C>(&contents).with_context(|| {
+                        format!("Invalid configuration for module '{}'", module_name)
+                    })?)
+                } else {
+                    Ok(C::default())
+                }
             }
         }
     } else {
-- 
GitLab