From 221d8b3b271e99a87d3333e6606d10432b88178b Mon Sep 17 00:00:00 2001 From: Andrew Phillips Date: Tue, 26 Aug 2025 18:45:14 -0300 Subject: [PATCH] feat: add full hostname option to hostname plugin and remove full_hostname plugin Co-authored-by: aider (openai/andrew/openrouter/qwen/qwen3-coder) --- src/meta_plugin.rs | 2 - src/meta_plugin/system.rs | 185 +++++++------------------------------- 2 files changed, 31 insertions(+), 156 deletions(-) diff --git a/src/meta_plugin.rs b/src/meta_plugin.rs index 0b9cbb9..7af6c31 100644 --- a/src/meta_plugin.rs +++ b/src/meta_plugin.rs @@ -121,7 +121,6 @@ pub enum MetaPluginType { ReadTime, ReadRate, Hostname, - FullHostname, } /// Central function to handle metadata output with name mapping @@ -284,6 +283,5 @@ pub fn get_meta_plugin(meta_plugin_type: MetaPluginType) -> Box MetaPluginType::ReadTime => Box::new(ReadTimeMetaPlugin::new_simple()), MetaPluginType::ReadRate => Box::new(ReadRateMetaPlugin::new_simple()), MetaPluginType::Hostname => Box::new(HostnameMetaPlugin::new_simple()), - MetaPluginType::FullHostname => Box::new(FullHostnameMetaPlugin::new_simple()), } } diff --git a/src/meta_plugin/system.rs b/src/meta_plugin/system.rs index a19404e..f33d475 100644 --- a/src/meta_plugin/system.rs +++ b/src/meta_plugin/system.rs @@ -693,12 +693,14 @@ pub struct HostnameMetaPlugin { impl HostnameMetaPlugin { pub fn new( - _options: Option>, + options: Option>, outputs: Option>, ) -> HostnameMetaPlugin { // Start with default options let mut final_options = std::collections::HashMap::new(); - if let Some(opts) = _options { + // Add default value for "full" option + final_options.insert("full".to_string(), serde_yaml::Value::Bool(true)); + if let Some(opts) = options { for (key, value) in opts { final_options.insert(key, value); } @@ -727,6 +729,13 @@ impl HostnameMetaPlugin { pub fn new_simple() -> HostnameMetaPlugin { Self::new(None, None) } + + fn get_hostname(&self) -> String { + match gethostname().into_string() { + Ok(hostname) => hostname, + Err(_) => "unknown".to_string(), + } + } } impl MetaPlugin for HostnameMetaPlugin { @@ -785,9 +794,18 @@ impl MetaPlugin for HostnameMetaPlugin { } let mut metadata = Vec::new(); - let hostname = match gethostname().into_string() { - Ok(hostname) => hostname, - Err(_) => "unknown".to_string(), + + // 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 + + let hostname = if use_full { + self.get_hostname() + } else { + // Get short hostname (first part before the first dot) + let full_hostname = self.get_hostname(); + full_hostname.split('.').next().unwrap_or(&full_hostname).to_string() }; // Use process_metadata_outputs to handle output mapping @@ -821,7 +839,9 @@ impl MetaPlugin for HostnameMetaPlugin { } fn default_options(&self) -> std::collections::HashMap { - std::collections::HashMap::new() + let mut options = std::collections::HashMap::new(); + options.insert("full".to_string(), serde_yaml::Value::Bool(true)); + options } fn options(&self) -> &std::collections::HashMap { @@ -831,155 +851,12 @@ impl MetaPlugin for HostnameMetaPlugin { fn options_mut(&mut self) -> &mut std::collections::HashMap { &mut self.options } -} - -#[derive(Debug, Clone, Default)] -pub struct FullHostnameMetaPlugin { - meta_name: String, - is_finalized: bool, - outputs: std::collections::HashMap, - options: std::collections::HashMap, -} - -impl FullHostnameMetaPlugin { - pub fn new( - _options: Option>, - outputs: Option>, - ) -> FullHostnameMetaPlugin { - // Start with default options - let mut final_options = std::collections::HashMap::new(); - if let Some(opts) = _options { - for (key, value) in opts { - final_options.insert(key, value); - } - } - - // Start with default outputs - let mut final_outputs = std::collections::HashMap::new(); - let default_outputs = Self::default().default_outputs(); - for output_name in default_outputs { - final_outputs.insert(output_name.clone(), serde_yaml::Value::String(output_name)); - } - if let Some(outs) = outputs { - for (key, value) in outs { - final_outputs.insert(key, value); - } - } - - FullHostnameMetaPlugin { - meta_name: "full_hostname".to_string(), - is_finalized: false, - outputs: final_outputs, - options: final_options, - } - } - pub fn new_simple() -> FullHostnameMetaPlugin { - Self::new(None, None) + fn configure_options(&mut self, options: &std::collections::HashMap) -> anyhow::Result<()> { + for (key, value) in options { + self.options.insert(key.clone(), value.clone()); + } + Ok(()) } } -impl MetaPlugin for FullHostnameMetaPlugin { - fn is_finalized(&self) -> bool { - self.is_finalized - } - - fn set_finalized(&mut self, finalized: bool) { - self.is_finalized = finalized; - } - - fn finalize(&mut self) -> crate::meta_plugin::MetaPluginResponse { - // If already finalized, don't process again - if self.is_finalized { - return crate::meta_plugin::MetaPluginResponse { - metadata: Vec::new(), - is_finalized: true, - }; - } - - // Mark as finalized - self.is_finalized = true; - - crate::meta_plugin::MetaPluginResponse { - metadata: Vec::new(), - is_finalized: true, - } - } - - fn update(&mut self, _data: &[u8]) -> crate::meta_plugin::MetaPluginResponse { - // If already finalized, don't process more data - if self.is_finalized { - return crate::meta_plugin::MetaPluginResponse { - metadata: Vec::new(), - is_finalized: true, - }; - } - - crate::meta_plugin::MetaPluginResponse { - metadata: Vec::new(), - is_finalized: false, - } - } - - fn meta_name(&self) -> String { - self.meta_name.clone() - } - - fn initialize(&mut self) -> crate::meta_plugin::MetaPluginResponse { - // If already finalized, don't process again - if self.is_finalized { - return crate::meta_plugin::MetaPluginResponse { - metadata: Vec::new(), - is_finalized: true, - }; - } - - let mut metadata = Vec::new(); - // For now, use regular hostname since local_ip and lookup_addr aren't available - let hostname = match gethostname().into_string() { - Ok(hostname) => hostname, - Err(_) => "unknown".to_string(), - }; - - // Use process_metadata_outputs to handle output mapping - if let Some(meta_data) = crate::meta_plugin::process_metadata_outputs( - "full_hostname", - hostname, - &self.outputs - ) { - metadata.push(meta_data); - } - - // Mark as finalized since this plugin only needs to run once - self.is_finalized = true; - - crate::meta_plugin::MetaPluginResponse { - metadata, - is_finalized: true, - } - } - - fn outputs(&self) -> &std::collections::HashMap { - &self.outputs - } - - fn outputs_mut(&mut self) -> &mut std::collections::HashMap { - &mut self.outputs - } - - fn default_outputs(&self) -> Vec { - vec!["full_hostname".to_string()] - } - - fn default_options(&self) -> std::collections::HashMap { - std::collections::HashMap::new() - } - - fn options(&self) -> &std::collections::HashMap { - &self.options - } - - fn options_mut(&mut self) -> &mut std::collections::HashMap { - &mut self.options - } -}