From cbcbd0561310a93e15dc399256ec02b58890cbd0 Mon Sep 17 00:00:00 2001 From: Dreaded_X Date: Fri, 6 Jan 2023 04:14:39 +0100 Subject: [PATCH] Error on failed env substitution --- src/config.rs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/config.rs b/src/config.rs index 14ebfed..651aef4 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,7 +1,7 @@ use std::{fs, error::Error, collections::HashMap, net::{Ipv4Addr, SocketAddr}}; use regex::{Regex, Captures}; -use tracing::{debug, trace, warn}; +use tracing::{debug, trace, error}; use rumqttc::AsyncClient; use serde::Deserialize; @@ -158,19 +158,24 @@ impl Config { // Substitute in environment variables let re = Regex::new(r"\$\{(.*)\}").unwrap(); + let mut failure = false; let file = re.replace_all(&file, |caps: &Captures| { let key = caps.get(1).unwrap().as_str(); debug!("Substituting '{key}' in config"); match std::env::var(key) { Ok(value) => value, Err(_) => { - // @TODO Would be nice if we could propagate this error upwards - warn!("Environment variable '{key}' is not set, using empty string as default"); + failure = true; + error!("Environment variable '{key}' is not set"); "".to_string() } } }); + if failure { + return Err("Missing environment variables".into()); + } + let config = toml::from_str(&file)?; Ok(config) }