diff --git a/src/meta_plugin/digest.rs b/src/meta_plugin/digest.rs index 37170d0..425cb8f 100644 --- a/src/meta_plugin/digest.rs +++ b/src/meta_plugin/digest.rs @@ -87,16 +87,6 @@ impl DigestMetaPlugin { ) -> DigestMetaPlugin { let mut plugin = DigestMetaPlugin::default(); - // Set default outputs - let default_outputs = vec![ - ("digest_md5".to_string(), serde_yaml::Value::String("digest_md5".to_string())), - ("digest_sha256".to_string(), serde_yaml::Value::String("digest_sha256".to_string())), - ("digest_sha512".to_string(), serde_yaml::Value::String("digest_sha512".to_string())), - ]; - for (key, value) in default_outputs { - plugin.outputs.insert(key, value); - } - // Apply provided options if let Some(opts) = options { for (key, value) in opts { @@ -104,32 +94,56 @@ impl DigestMetaPlugin { } } - // Apply provided outputs + // Determine the selected method + let method = if let Some(method_value) = plugin.options.get("method") { + if let Some(method_str) = method_value.as_str() { + match method_str { + "md5" => "md5", + "sha256" => "sha256", + "sha512" => "sha512", + _ => "sha256", + } + } else { + "sha256" + } + } else { + "sha256" + }; + + // Initialize the hasher based on the method + plugin.hasher = match method { + "md5" => Some(Hasher::Md5(md5::Context::new())), + "sha256" => Some(Hasher::Sha256(Sha256::new())), + "sha512" => Some(Hasher::Sha512(Sha512::new())), + _ => Some(Hasher::Sha256(Sha256::new())), + }; + + // Add the method to options so it shows up in the status + plugin.options.insert("method".to_string(), serde_yaml::Value::String(method.to_string())); + + // Set outputs based on the selected method + // Only the selected method's output should be enabled, others should be None + let all_outputs = vec!["digest_md5", "digest_sha256", "digest_sha512"]; + for output_name in all_outputs { + if output_name == format!("digest_{}", method) { + plugin.outputs.insert(output_name.to_string(), serde_yaml::Value::String(output_name.to_string())); + } else { + plugin.outputs.insert(output_name.to_string(), serde_yaml::Value::Null); + } + } + + // Apply provided outputs, but only for enabled outputs if let Some(outs) = outputs { for (key, value) in outs { - plugin.outputs.insert(key, value); + // Only update if the output is not disabled (not None) + if let Some(current_value) = plugin.outputs.get_mut(&key) { + if !current_value.is_null() { + *current_value = value; + } + } } } - // Initialize the hasher based on the method option - if let Some(method_value) = plugin.options.get("method") { - if let Some(method_str) = method_value.as_str() { - plugin.hasher = match method_str { - "md5" => Some(Hasher::Md5(md5::Context::new())), - "sha256" => Some(Hasher::Sha256(Sha256::new())), - "sha512" => Some(Hasher::Sha512(Sha512::new())), - _ => None, - }; - } - } - - // Default to sha256 if no valid method is specified - if plugin.hasher.is_none() { - plugin.hasher = Some(Hasher::Sha256(Sha256::new())); - // Add the default method to options so it shows up in the status - plugin.options.insert("method".to_string(), serde_yaml::Value::String("sha256".to_string())); - } - plugin } diff --git a/src/meta_plugin/hostname.rs b/src/meta_plugin/hostname.rs index 69820f4..bb7e493 100644 --- a/src/meta_plugin/hostname.rs +++ b/src/meta_plugin/hostname.rs @@ -39,21 +39,69 @@ impl HostnameMetaPlugin { } } - // Start with default outputs + // Determine which outputs are enabled based on options + let hostname_enabled = match final_options.get("hostname") { + Some(serde_yaml::Value::Bool(b)) => *b, + Some(serde_yaml::Value::String(s)) => match s.as_str() { + "true" => true, + "false" => false, + "full" => true, + "short" => true, + _ => true, + }, + _ => true, + }; + + let hostname_full_enabled = final_options.get("hostname_full") + .and_then(|v| v.as_bool()) + .unwrap_or(true); + + let hostname_short_enabled = final_options.get("hostname_short") + .and_then(|v| v.as_bool()) + .unwrap_or(true); + + // Start with default outputs, setting disabled ones to None let mut final_outputs = std::collections::HashMap::new(); - let default_outputs = vec![ - "hostname".to_string(), - "hostname_full".to_string(), - "hostname_short".to_string(), - ]; - for output_name in default_outputs { - final_outputs.insert(output_name.clone(), serde_yaml::Value::String(output_name)); + + // Handle hostname output + if hostname_enabled { + final_outputs.insert("hostname".to_string(), serde_yaml::Value::String("hostname".to_string())); + } else { + final_outputs.insert("hostname".to_string(), serde_yaml::Value::Null); } - // Override with provided outputs + // Handle hostname_full output + if hostname_full_enabled { + final_outputs.insert("hostname_full".to_string(), serde_yaml::Value::String("hostname_full".to_string())); + } else { + final_outputs.insert("hostname_full".to_string(), serde_yaml::Value::Null); + } + + // Handle hostname_short output + if hostname_short_enabled { + final_outputs.insert("hostname_short".to_string(), serde_yaml::Value::String("hostname_short".to_string())); + } else { + final_outputs.insert("hostname_short".to_string(), serde_yaml::Value::Null); + } + + // Override with provided outputs, but only if they're enabled if let Some(outs) = outputs { for (key, value) in outs { - final_outputs.insert(key, value); + // Only add if the output is enabled + match key.as_str() { + "hostname" => if hostname_enabled { + final_outputs.insert(key, value); + }, + "hostname_full" => if hostname_full_enabled { + final_outputs.insert(key, value); + }, + "hostname_short" => if hostname_short_enabled { + final_outputs.insert(key, value); + }, + _ => { + final_outputs.insert(key, value); + } + } } }