Skip to content

Commit

Permalink
Merge pull request #106 from projectsyn/feat/controllable-config-load…
Browse files Browse the repository at this point in the history
…ing-verbosity

Allow disabling reclass-rs diagnostic messages for unknown config options
  • Loading branch information
simu authored Feb 26, 2024
2 parents 0fff69b + 6715520 commit ffb0d96
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 17 deletions.
32 changes: 23 additions & 9 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ impl Config {
cfg_path: &std::path::Path,
k: &str,
v: &serde_yaml::Value,
verbose: bool,
) -> Result<()> {
let vstr = serde_yaml::to_string(v)?;
let vstr = vstr.trim();
Expand Down Expand Up @@ -192,7 +193,11 @@ impl Config {
}
}
_ => {
eprintln!("reclass-config.yml entry '{k}={vstr}' not implemented yet, ignoring...");
if verbose {
eprintln!(
"reclass-config.yml entry '{k}={vstr}' not implemented yet, ignoring..."
);
}
}
};

Expand All @@ -203,8 +208,9 @@ impl Config {
///
/// This method assumes that you've created a Config object with a suitable `inventory_path`.
///
/// The method will print diagnostic messages for config options which aren't implemented yet.
pub fn load_from_file(&mut self, config_file: &str) -> Result<()> {
/// If `verbose` is true, the method will print diagnostic messages for config options which
/// aren't implemented yet.
pub fn load_from_file(&mut self, config_file: &str, verbose: bool) -> Result<()> {
let mut cfg_path = PathBuf::from(&self.inventory_path);
cfg_path.push(config_file);

Expand All @@ -216,7 +222,7 @@ impl Config {
{
let kstr = serde_yaml::to_string(k)?;
let kstr = kstr.trim();
self.set_option(&cfg_path, kstr, v)?;
self.set_option(&cfg_path, kstr, v, verbose)?;
}
self.compile_ignore_class_notfound_patterns()?;
Ok(())
Expand Down Expand Up @@ -268,11 +274,18 @@ impl Config {
}

/// Creates a Config object based on the provided `inventory_path` and the config options
/// passed in the `config` Python dict.
/// passed in the `config` Python dict. If `verbose` is set to `true`, reclass-rs will print
/// diagnostic messages for unknown config options.
///
/// Returns a `Config` object or raises a `ValueError`.
#[classmethod]
fn from_dict(_cls: &PyType, inventory_path: &str, config: &PyDict) -> PyResult<Self> {
#[pyo3(signature = (inventory_path, config, verbose=false))]
fn from_dict(
_cls: &PyType,
inventory_path: &str,
config: &PyDict,
verbose: bool,
) -> PyResult<Self> {
let mut cfg = Config::new(Some(inventory_path), None, None, None).map_err(|e| {
PyValueError::new_err(format!(
"Failed to initialize reclass-rs config object: {e}"
Expand All @@ -288,9 +301,10 @@ impl Config {
for (k, v) in config {
let kstr = k.extract::<&str>()?;
let val: crate::types::Value = TryInto::try_into(v)?;
cfg.set_option(&cfg_path, kstr, &val.into()).map_err(|e| {
PyValueError::new_err(format!("Error while setting option {kstr}: {e}"))
})?;
cfg.set_option(&cfg_path, kstr, &val.into(), verbose)
.map_err(|e| {
PyValueError::new_err(format!("Error while setting option {kstr}: {e}"))
})?;
}

Ok(cfg)
Expand Down
7 changes: 4 additions & 3 deletions src/inventory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ mod inventory_tests {
#[test]
fn test_render() {
let mut c = crate::Config::new(Some("./tests/inventory"), None, None, None).unwrap();
c.load_from_file("reclass-config.yml").unwrap();
c.load_from_file("reclass-config.yml", false).unwrap();
let r = Reclass::new_from_config(c).unwrap();
let inv = Inventory::render(&r).unwrap();

Expand Down Expand Up @@ -263,7 +263,8 @@ mod inventory_tests {
None,
)
.unwrap();
c.load_from_file("reclass-config-compat.yml").unwrap();
c.load_from_file("reclass-config-compat.yml", false)
.unwrap();
let r = Reclass::new_from_config(c).unwrap();

let inv = Inventory::render(&r).unwrap();
Expand Down Expand Up @@ -312,7 +313,7 @@ mod inventory_tests {
None,
)
.unwrap();
c.load_from_file("reclass-config.yml").unwrap();
c.load_from_file("reclass-config.yml", false).unwrap();
let r = Reclass::new_from_config(c).unwrap();

let inv = Inventory::render(&r).unwrap();
Expand Down
12 changes: 9 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,10 +293,16 @@ impl Reclass {
///
/// Returns a `Reclass` instance or raises a `ValueError`
#[classmethod]
fn from_config_file(cls: &PyType, inventory_path: &str, config_file: &str) -> PyResult<Self> {
#[pyo3(signature = (inventory_path, config_file, verbose=false))]
fn from_config_file(
cls: &PyType,
inventory_path: &str,
config_file: &str,
verbose: bool,
) -> PyResult<Self> {
let mut c = Config::new(Some(inventory_path), None, None, None)
.map_err(|e| PyValueError::new_err(format!("{e}")))?;
c.load_from_file(config_file)
c.load_from_file(config_file, verbose)
.map_err(|e| PyValueError::new_err(format!("{e}")))?;
Self::from_config(cls, c)
}
Expand Down Expand Up @@ -447,7 +453,7 @@ mod tests {
None,
)
.unwrap();
c.load_from_file("reclass-config.yml").unwrap();
c.load_from_file("reclass-config.yml", true).unwrap();
let r = Reclass::new_from_config(c).unwrap();
assert_eq!(r.nodes.len(), 8);
let mut nodes = r.nodes.keys().collect::<Vec<_>>();
Expand Down
4 changes: 2 additions & 2 deletions src/node/node_render_tests_ignore_class_notfound_regexp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ fn test_render_n1() {
None,
)
.unwrap();
c.load_from_file("reclass-config.yml").unwrap();
c.load_from_file("reclass-config.yml", false).unwrap();
let r = Reclass::new_from_config(c).unwrap();

let n1 = r.render_node("n1").unwrap();
Expand All @@ -33,7 +33,7 @@ fn test_render_n2() {
None,
)
.unwrap();
c.load_from_file("reclass-config.yml").unwrap();
c.load_from_file("reclass-config.yml", false).unwrap();
let r = Reclass::new_from_config(c).unwrap();

let n2 = r.render_node("n2");
Expand Down

0 comments on commit ffb0d96

Please sign in to comment.