diff --git a/server/src/config.rs b/server/src/config.rs index 9172b2e..96655d0 100644 --- a/server/src/config.rs +++ b/server/src/config.rs @@ -27,6 +27,7 @@ pub struct Config { fn resolve_datasource(config: &config::Config) -> anyhow::Result { if config.get::("prometheus.url").is_ok() { + println!("using Prometheus"); return Ok(DatasourceConfig::Prometheus(PrometheusConfig { url: config.get("prometheus.url")?, query: config.get("prometheus.query")?, @@ -34,6 +35,7 @@ fn resolve_datasource(config: &config::Config) -> anyhow::Result("influx.url").is_ok() { + println!("using Influx"); return Ok(DatasourceConfig::Influx(InfluxConfig { url: config.get("influx.url")?, org_id: config.get("influx.org_id")?, @@ -42,7 +44,7 @@ fn resolve_datasource(config: &config::Config) -> anyhow::Result anyhow::Result> { @@ -80,18 +82,32 @@ fn resolve_alerting(config: &config::Config) -> anyhow::Result alerting.type pub fn update_from_env(config: &mut config::Config) { let overrides = env::vars().filter_map(|(key, value)| parse_env(&key).map(|index| (index, value))); for (key, value) in overrides { - println!("{} => {}", key, value); config.set(&key, value).unwrap(); } } +pub fn print_config(config: config::Config) { + // TODO: support any nesting level + let sections = config.to_owned().cache.into_table().unwrap(); + for (section_name, values) in sections { + match values.clone().into_table() { + Err(_) => println!("{} => {}", section_name, values), + Ok(section) => { + for (key, value) in section { + println!("{}.{} => {}", section_name, key, value); + } + } + } + } +} + fn parse_env(key: &str) -> Option { const PREFIX: &str = "HASTIC_"; @@ -117,7 +133,8 @@ impl Config { config.set("port", 4347).unwrap(); } - // TODO: print resulted config (perfectly, it needs adding `derive(Debug)` in `subbeat`'s `DatasourceConfig`) + print_config(config.clone()); + Ok(Config { port: config.get::("port").unwrap(), datasource_config: resolve_datasource(&config)?,