From fc614d7ce2c9bb921f923461f8d43234965ddff4 Mon Sep 17 00:00:00 2001 From: Kai Kriegel Date: Fri, 27 Aug 2021 21:51:44 +0200 Subject: [PATCH 1/3] added config check --- src/main.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/main.rs b/src/main.rs index fb6057a..6b42341 100644 --- a/src/main.rs +++ b/src/main.rs @@ -40,6 +40,7 @@ use error::Error; use slog::Logger; use paho_mqtt::AsyncClient; +use crate::config::Config; fn main() { use clap::{crate_version, crate_description, crate_name}; @@ -59,6 +60,11 @@ fn main() { .help("Print a default config to stdout instead of running") .long("print-default") ) + .arg(Arg::with_name("check config") + .help("Check config for validity") + .long("check") + .requires("config") + ) .arg(Arg::with_name("dump") .help("Dump all databases into the given directory") .long("dump") @@ -87,6 +93,24 @@ fn main() { // Early return to exit. return; + } else if matches.is_present("check config") { + let configpath = matches.value_of("config").unwrap_or("/etc/diflouroborane.dhall"); + match config::read(&PathBuf::from_str(configpath).unwrap()) { + Ok(cfg) => { + let encoded = serde_dhall::serialize(&cfg).to_string().unwrap(); + + // Direct writing to fd 1 is faster but also prevents any print-formatting that could + // invalidate the generated TOML + let stdout = io::stdout(); + let mut handle = stdout.lock(); + handle.write_all(&encoded.as_bytes()).unwrap(); + std::process::exit(0); + } + Err(e) => { + eprintln!("{}", e); + std::process::exit(-1); + } + } } let retval; From 8d337248a8c7b16484edc867cbcf28be38ba5eeb Mon Sep 17 00:00:00 2001 From: Kai Kriegel Date: Sat, 28 Aug 2021 23:03:57 +0200 Subject: [PATCH 3/3] only do a basic config check Printing of normalized dhall will have to wait until i figure out how to supply the needed type info to the serializer --- src/main.rs | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/src/main.rs b/src/main.rs index 6b42341..2333e35 100644 --- a/src/main.rs +++ b/src/main.rs @@ -63,7 +63,6 @@ fn main() { .arg(Arg::with_name("check config") .help("Check config for validity") .long("check") - .requires("config") ) .arg(Arg::with_name("dump") .help("Dump all databases into the given directory") @@ -97,13 +96,8 @@ fn main() { let configpath = matches.value_of("config").unwrap_or("/etc/diflouroborane.dhall"); match config::read(&PathBuf::from_str(configpath).unwrap()) { Ok(cfg) => { - let encoded = serde_dhall::serialize(&cfg).to_string().unwrap(); - - // Direct writing to fd 1 is faster but also prevents any print-formatting that could - // invalidate the generated TOML - let stdout = io::stdout(); - let mut handle = stdout.lock(); - handle.write_all(&encoded.as_bytes()).unwrap(); + //TODO: print a normalized version of the supplied config + println!("config is valid"); std::process::exit(0); } Err(e) => {