Add ServerConf::merge_with_opt for when Opt/ServerConf are used together

This commit is contained in:
ewang 2024-03-29 13:11:03 -07:00
parent 414b0f082f
commit ff6f0bbdf6
2 changed files with 11 additions and 7 deletions

View file

@ -167,9 +167,7 @@ impl ServerConf {
pub fn load_yaml_with_opt_override(opt: &Opt) -> Result<Self> {
if let Some(path) = &opt.conf {
let mut conf = Self::load_from_yaml(path)?;
if opt.daemon {
conf.daemon = true;
}
conf.merge_with_opt(opt);
Ok(conf)
} else {
Error::e_explain(ReadError, "No path specified")
@ -184,9 +182,7 @@ impl ServerConf {
let conf = Self::new();
match conf {
Some(mut c) => {
if opt.daemon {
c.daemon = true;
}
c.merge_with_opt(opt);
Some(c)
}
None => None,
@ -211,6 +207,12 @@ impl ServerConf {
// TODO: do the validation
Ok(self)
}
pub fn merge_with_opt(&mut self, opt: &Opt) {
if opt.daemon {
self.daemon = true;
}
}
}
#[cfg(test)]

View file

@ -175,10 +175,12 @@ impl Server {
///
/// If a configuration file path is provided as part of `opt`, it will be ignored
/// and a warning will be logged.
pub fn new_with_opt_and_conf(opt: Opt, conf: ServerConf) -> Server {
pub fn new_with_opt_and_conf(opt: Opt, mut conf: ServerConf) -> Server {
if let Some(c) = opt.conf.as_ref() {
warn!("Ignoring command line argument using '{c}' as configuration, and using provided configuration instead.");
}
conf.merge_with_opt(&opt);
let (tx, rx) = watch::channel(false);
Server {