From b6db4f7b8c586588d8eaa2400bced3c858e6d90f Mon Sep 17 00:00:00 2001 From: John Smith Date: Thu, 10 Mar 2022 11:10:11 -0500 Subject: [PATCH] fix settings and tests --- veilid-server/src/cmdline.rs | 13 ++++---- veilid-server/src/settings.rs | 56 ++++++++++++++++------------------- 2 files changed, 33 insertions(+), 36 deletions(-) diff --git a/veilid-server/src/cmdline.rs b/veilid-server/src/cmdline.rs index 84239f0e..8fe1d8b8 100644 --- a/veilid-server/src/cmdline.rs +++ b/veilid-server/src/cmdline.rs @@ -1,10 +1,10 @@ use crate::settings::*; -use clap::{App, Arg, ArgMatches}; +use clap::{Arg, ArgMatches, Command}; use std::ffi::OsStr; use std::str::FromStr; fn do_clap_matches(default_config_path: &OsStr) -> Result { - let matches = App::new("veilid-server") + let matches = Command::new("veilid-server") .version("0.1") .about("Veilid Server") .color(clap::ColorChoice::Auto) @@ -111,10 +111,11 @@ pub fn process_command_line() -> Result<(Settings, ArgMatches), String> { } // Attempt to load configuration - let settings = Settings::new( - matches.occurrences_of("config-file") == 0, - matches.value_of_os("config-file").unwrap(), - ) + let settings = Settings::new(if matches.occurrences_of("config-file") == 0 { + None + } else { + Some(matches.value_of_os("config-file").unwrap()) + }) .map_err(|e| format!("configuration is invalid: {}", e))?; // write lock the settings diff --git a/veilid-server/src/settings.rs b/veilid-server/src/settings.rs index 07a56160..4f6b80cc 100644 --- a/veilid-server/src/settings.rs +++ b/veilid-server/src/settings.rs @@ -13,7 +13,7 @@ use std::sync::Arc; use url::Url; use veilid_core::xx::*; -pub fn load_default_config(cfg: &mut config::Config) -> Result<(), config::ConfigError> { +pub fn load_default_config() -> Result { let default_config = String::from( r#"--- daemon: false @@ -138,20 +138,23 @@ core: "%INSECURE_FALLBACK_DIRECTORY%", &Settings::get_default_protected_store_insecure_fallback_directory().to_string_lossy(), ); - cfg.merge(config::File::from_str( - &default_config, - config::FileFormat::Yaml, - )) - .map(drop) + config::Config::builder() + .add_source(config::File::from_str( + &default_config, + config::FileFormat::Yaml, + )) + .build() } pub fn load_config( - cfg: &mut config::Config, + cfg: config::Config, config_file: &Path, -) -> Result<(), config::ConfigError> { +) -> Result { if let Some(config_file_str) = config_file.to_str() { - cfg.merge(config::File::new(config_file_str, config::FileFormat::Yaml)) - .map(drop) + config::Config::builder() + .add_source(cfg) + .add_source(config::File::new(config_file_str, config::FileFormat::Yaml)) + .build() } else { Err(config::ConfigError::Message( "config file path is not valid UTF-8".to_owned(), @@ -584,25 +587,19 @@ pub struct Settings { } impl Settings { - pub fn new( - config_file_is_default: bool, - config_file: &OsStr, - ) -> Result { - // Create a config - let mut cfg = config::Config::default(); - + pub fn new(config_file: Option<&OsStr>) -> Result { // Load the default config - load_default_config(&mut cfg)?; + let mut cfg = load_default_config()?; // Merge in the config file if we have one - let config_file_path = Path::new(config_file); - if !config_file_is_default || config_file_path.exists() { + if let Some(config_file) = config_file { + let config_file_path = Path::new(config_file); // If the user specifies a config file on the command line then it must exist - load_config(&mut cfg, config_file_path)?; + cfg = load_config(cfg, config_file_path)?; } // Generate config - let inner: SettingsInner = cfg.try_into()?; + let inner: SettingsInner = cfg.try_deserialize()?; // Ok(Self { @@ -1087,16 +1084,15 @@ mod tests { #[test] #[serial] fn test_default_config() { - let mut cfg = config::Config::default(); - load_default_config(&mut cfg).unwrap(); - let inner = cfg.try_into::().unwrap(); + let cfg = load_default_config().unwrap(); + let inner = cfg.try_deserialize::().unwrap(); println!("default settings: {:?}", inner); } #[test] #[serial] fn test_default_config_settings() { - let settings = Settings::new(true, OsStr::new("!!!")).unwrap(); + let settings = Settings::new(None).unwrap(); let s = settings.read(); assert_eq!(s.daemon, false); @@ -1214,7 +1210,7 @@ mod tests { // assert_eq!(s.core.network.protocol.udp.enabled, true); assert_eq!(s.core.network.protocol.udp.socket_pool_size, 0); - assert_eq!(s.core.network.protocol.udp.listen_address.name, "[::]:5150"); + assert_eq!(s.core.network.protocol.udp.listen_address.name, ":5150"); assert_eq!( s.core.network.protocol.udp.listen_address.addrs, listen_address_to_socket_addrs(":5150").unwrap() @@ -1225,7 +1221,7 @@ mod tests { assert_eq!(s.core.network.protocol.tcp.connect, true); assert_eq!(s.core.network.protocol.tcp.listen, true); assert_eq!(s.core.network.protocol.tcp.max_connections, 32); - assert_eq!(s.core.network.protocol.tcp.listen_address.name, "[::]:5150"); + assert_eq!(s.core.network.protocol.tcp.listen_address.name, ":5150"); assert_eq!( s.core.network.protocol.tcp.listen_address.addrs, listen_address_to_socket_addrs(":5150").unwrap() @@ -1236,7 +1232,7 @@ mod tests { assert_eq!(s.core.network.protocol.ws.connect, true); assert_eq!(s.core.network.protocol.ws.listen, true); assert_eq!(s.core.network.protocol.ws.max_connections, 16); - assert_eq!(s.core.network.protocol.ws.listen_address.name, "[::]:5150"); + assert_eq!(s.core.network.protocol.ws.listen_address.name, ":5150"); assert_eq!( s.core.network.protocol.ws.listen_address.addrs, listen_address_to_socket_addrs(":5150").unwrap() @@ -1250,7 +1246,7 @@ mod tests { assert_eq!(s.core.network.protocol.wss.connect, true); assert_eq!(s.core.network.protocol.wss.listen, false); assert_eq!(s.core.network.protocol.wss.max_connections, 16); - assert_eq!(s.core.network.protocol.wss.listen_address.name, "[::]:5150"); + assert_eq!(s.core.network.protocol.wss.listen_address.name, ":5150"); assert_eq!( s.core.network.protocol.wss.listen_address.addrs, listen_address_to_socket_addrs(":5150").unwrap()