diff --git a/src/meta_plugin/hostname.rs b/src/meta_plugin/hostname.rs index d0e9548..f33de9d 100644 --- a/src/meta_plugin/hostname.rs +++ b/src/meta_plugin/hostname.rs @@ -15,8 +15,12 @@ impl HostnameMetaPlugin { ) -> HostnameMetaPlugin { // Start with default options let mut final_options = std::collections::HashMap::new(); - // Add default value for "full" option - final_options.insert("full".to_string(), serde_yaml::Value::Bool(true)); + // Set default values + final_options.insert("hostname".to_string(), serde_yaml::Value::String("true".to_string())); + final_options.insert("hostname_full".to_string(), serde_yaml::Value::Bool(true)); + final_options.insert("hostname_short".to_string(), serde_yaml::Value::Bool(true)); + + // Override with provided options if let Some(opts) = options { for (key, value) in opts { final_options.insert(key, value); @@ -25,10 +29,16 @@ impl HostnameMetaPlugin { // Start with default outputs let mut final_outputs = std::collections::HashMap::new(); - let default_outputs = Self::default().default_outputs(); + 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)); } + + // Override with provided outputs if let Some(outs) = outputs { for (key, value) in outs { final_outputs.insert(key, value); @@ -168,32 +178,78 @@ impl MetaPlugin for HostnameMetaPlugin { let mut metadata = Vec::new(); - // Check if we should use full hostname or short hostname - let use_full = self.options.get("full") - .and_then(|v| v.as_bool()) - .unwrap_or(true); // Default to true - - log::debug!("HOSTNAME: use_full option: {:?}", use_full); - + // Get the full hostname let full_hostname = self.get_hostname(); - log::debug!("HOSTNAME: full hostname from system: {:?}", full_hostname); + let short_hostname = full_hostname.split('.').next().unwrap_or(&full_hostname).to_string(); - let hostname = if use_full { - full_hostname.clone() - } else { - // Get short hostname (first part before the first dot) - full_hostname.split('.').next().unwrap_or(&full_hostname).to_string() + // Determine which hostnames to include based on options + let hostname_enabled = match self.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, }; - log::debug!("HOSTNAME: final hostname to use: {:?}", hostname); + let hostname_full_enabled = self.options.get("hostname_full") + .and_then(|v| v.as_bool()) + .unwrap_or(true); + + let hostname_short_enabled = self.options.get("hostname_short") + .and_then(|v| v.as_bool()) + .unwrap_or(true); - // Use process_metadata_outputs to handle output mapping - if let Some(meta_data) = crate::meta_plugin::process_metadata_outputs( - "hostname", - hostname, - &self.outputs - ) { - metadata.push(meta_data); + // Determine which hostname value to use for the 'hostname' output + let hostname_value = match self.options.get("hostname") { + Some(serde_yaml::Value::String(s)) => match s.as_str() { + "full" => full_hostname.clone(), + "short" => short_hostname.clone(), + _ => { + // For 'true' or any other value, use gethostname() + gethostname::gethostname().into_string().unwrap_or_else(|_| "unknown".to_string()) + } + }, + _ => { + // Default behavior + gethostname::gethostname().into_string().unwrap_or_else(|_| "unknown".to_string()) + } + }; + + // Add hostname output if enabled + if hostname_enabled { + if let Some(meta_data) = crate::meta_plugin::process_metadata_outputs( + "hostname", + hostname_value, + &self.outputs + ) { + metadata.push(meta_data); + } + } + + // Add hostname_full output if enabled + if hostname_full_enabled { + if let Some(meta_data) = crate::meta_plugin::process_metadata_outputs( + "hostname_full", + full_hostname.clone(), + &self.outputs + ) { + metadata.push(meta_data); + } + } + + // Add hostname_short output if enabled + if hostname_short_enabled { + if let Some(meta_data) = crate::meta_plugin::process_metadata_outputs( + "hostname_short", + short_hostname, + &self.outputs + ) { + metadata.push(meta_data); + } } // Mark as finalized since this plugin only needs to run once @@ -214,7 +270,11 @@ impl MetaPlugin for HostnameMetaPlugin { } fn default_outputs(&self) -> Vec { - vec!["hostname".to_string()] + vec![ + "hostname".to_string(), + "hostname_full".to_string(), + "hostname_short".to_string(), + ] }