diff --git a/src/meta_plugin.rs b/src/meta_plugin.rs index fb32484..e43dddf 100644 --- a/src/meta_plugin.rs +++ b/src/meta_plugin.rs @@ -34,6 +34,8 @@ pub struct MetaPluginResponse { pub struct BaseMetaPlugin { pub outputs: std::collections::HashMap, pub options: std::collections::HashMap, + pub meta_name: String, + pub is_finalized: bool, } impl BaseMetaPlugin { @@ -56,6 +58,27 @@ impl BaseMetaPlugin { pub fn options_mut(&mut self) -> &mut std::collections::HashMap { &mut self.options } + + /// Helper function to initialize plugin options and outputs + pub fn initialize_plugin( + &mut self, + default_outputs: &[&str], + options: Option>, + outputs: Option>, + ) { + // Set default outputs + for output_name in default_outputs { + self.outputs.insert(output_name.to_string(), serde_yaml::Value::String(output_name.to_string())); + } + + // Apply provided options and outputs + if let Some(opts) = options { + self.options.extend(opts); + } + if let Some(outs) = outputs { + self.outputs.extend(outs); + } + } } impl MetaPlugin for BaseMetaPlugin { @@ -87,10 +110,7 @@ pub enum MetaPluginType { WordCount, Cwd, Binary, - Uid, - User, - Gid, - Group, + User, // Consolidated Uid, User, Gid, Group into User Shell, ShellPid, KeepPid, @@ -234,10 +254,10 @@ pub fn get_meta_plugin(meta_plugin_type: MetaPluginType) -> Box MetaPluginType::WordCount => Box::new(MetaPluginProgram::new_simple("wc", vec!["-w"], "word_count".to_string(), true)), MetaPluginType::Cwd => Box::new(CwdMetaPlugin::new_simple()), MetaPluginType::Binary => Box::new(BinaryMetaPlugin::new_simple()), - MetaPluginType::Uid => Box::new(UidMetaPlugin::new_simple()), + MetaPluginType::Uid => Box::new(UserMetaPlugin::new_simple()), // Consolidated into UserMetaPlugin MetaPluginType::User => Box::new(UserMetaPlugin::new_simple()), - MetaPluginType::Gid => Box::new(GidMetaPlugin::new_simple()), - MetaPluginType::Group => Box::new(GroupMetaPlugin::new_simple()), + MetaPluginType::Gid => Box::new(UserMetaPlugin::new_simple()), // Consolidated into UserMetaPlugin + MetaPluginType::Group => Box::new(UserMetaPlugin::new_simple()), // Consolidated into UserMetaPlugin MetaPluginType::Shell => Box::new(ShellMetaPlugin::new_simple()), MetaPluginType::ShellPid => Box::new(ShellPidMetaPlugin::new_simple()), MetaPluginType::KeepPid => Box::new(KeepPidMetaPlugin::new_simple()), diff --git a/src/meta_plugin/binary.rs b/src/meta_plugin/binary.rs index 78714fd..f20d486 100644 --- a/src/meta_plugin/binary.rs +++ b/src/meta_plugin/binary.rs @@ -7,11 +7,8 @@ use std::collections::HashMap; #[derive(Debug, Clone, Default)] pub struct BinaryMetaPlugin { - meta_name: String, buffer: Vec, max_buffer_size: usize, - is_saved: bool, - item_id: Option, base: crate::meta_plugin::BaseMetaPlugin, } @@ -20,41 +17,23 @@ impl BinaryMetaPlugin { options: Option>, outputs: Option>, ) -> BinaryMetaPlugin { - // Start with default options - let mut final_options = std::collections::HashMap::new(); - final_options.insert("max_buffer_size".to_string(), serde_yaml::Value::Number(4096.into())); - if let Some(opts) = options { - for (key, value) in opts { - final_options.insert(key, value); - } - } + let mut base = crate::meta_plugin::BaseMetaPlugin::new(); + base.meta_name = "binary".to_string(); - // Start with default outputs - let mut final_outputs = std::collections::HashMap::new(); - let default_outputs = vec!["binary".to_string()]; - 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); - } - } + // Initialize with helper function + base.initialize_plugin( + &["binary"], + options, + outputs, + ); - let max_buffer_size = final_options.get("max_buffer_size") + let max_buffer_size = base.options.get("max_buffer_size") .and_then(|v| v.as_u64()) .unwrap_or(PIPESIZE as u64) as usize; - let mut base = crate::meta_plugin::BaseMetaPlugin::new(); - base.outputs = final_outputs; - base.options = final_options; - BinaryMetaPlugin { - meta_name: "binary".to_string(), buffer: Vec::new(), max_buffer_size, - is_saved: false, - item_id: None, base, } } @@ -62,7 +41,6 @@ impl BinaryMetaPlugin { pub fn new_simple() -> BinaryMetaPlugin { Self::new(None, None) } - } impl MetaPlugin for BinaryMetaPlugin { @@ -123,7 +101,7 @@ impl MetaPlugin for BinaryMetaPlugin { } fn meta_name(&self) -> String { - self.meta_name.clone() + self.base.meta_name.clone() } fn outputs(&self) -> &std::collections::HashMap { diff --git a/src/meta_plugin/system.rs b/src/meta_plugin/system.rs index 6dbb982..198a2a4 100644 --- a/src/meta_plugin/system.rs +++ b/src/meta_plugin/system.rs @@ -87,147 +87,27 @@ impl MetaPlugin for CwdMetaPlugin { } -#[derive(Debug, Clone, Default)] -pub struct UidMetaPlugin { - meta_name: String, - is_saved: bool, - base: crate::meta_plugin::BaseMetaPlugin, -} - -impl UidMetaPlugin { - pub fn new( - _options: Option>, - outputs: Option>, - ) -> UidMetaPlugin { - // 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 = vec!["uid".to_string()]; - 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); - } - } - - let mut base = crate::meta_plugin::BaseMetaPlugin::new(); - base.outputs = final_outputs; - base.options = final_options; - - UidMetaPlugin { - meta_name: "uid".to_string(), - is_saved: false, - base, - } - } - - pub fn new_simple() -> UidMetaPlugin { - Self::new(None, None) - } -} - -impl MetaPlugin for UidMetaPlugin { - - fn finalize(&mut self, _conn: &Connection) -> Result<()> { - // Since we save during initialize(), return Ok to avoid duplicate saves - Ok(()) - } - - fn update(&mut self, _data: &[u8], _conn: &Connection) { - // No update needed - } - - fn meta_name(&self) -> String { - self.meta_name.clone() - } - - fn initialize(&mut self, item_id: i64) -> Result { - let mut metadata = Vec::new(); - let uid = get_current_uid().to_string(); - - if let Some(meta) = self.create_meta(item_id, "uid", uid) { - metadata.push(meta); - } - - Ok(MetaPluginResponse { - metadata: Some(metadata), - is_finalized: true, - }) - } - - fn outputs(&self) -> &std::collections::HashMap { - &self.base.outputs - } - - fn outputs_mut(&mut self) -> &mut std::collections::HashMap { - &mut self.base.outputs - } - - fn default_outputs(&self) -> Vec { - vec!["uid".to_string()] - } - - fn default_options(&self) -> std::collections::HashMap { - std::collections::HashMap::new() - } - - fn options(&self) -> &std::collections::HashMap { - &self.base.options - } - - fn options_mut(&mut self) -> &mut std::collections::HashMap { - &mut self.options - } -} - #[derive(Debug, Clone, Default)] pub struct UserMetaPlugin { - meta_name: String, - is_saved: bool, base: crate::meta_plugin::BaseMetaPlugin, } impl UserMetaPlugin { pub fn new( - _options: Option>, + options: Option>, outputs: Option>, ) -> UserMetaPlugin { - // 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 = vec!["user".to_string()]; - 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); - } - } - let mut base = crate::meta_plugin::BaseMetaPlugin::new(); - base.outputs = final_outputs; - base.options = final_options; + base.meta_name = "user".to_string(); + + // Initialize with helper function + base.initialize_plugin( + &["user_uid", "user_gid", "user_name", "user_group"], + options, + outputs, + ); UserMetaPlugin { - meta_name: "user".to_string(), - is_saved: false, base, } } @@ -235,31 +115,54 @@ impl UserMetaPlugin { pub fn new_simple() -> UserMetaPlugin { Self::new(None, None) } + + fn get_current_username() -> Option { + uzers::get_user_by_uid(uzers::get_current_uid()) + .map(|user| user.name().to_string_lossy().to_string()) + } + + fn get_current_groupname() -> Option { + uzers::get_group_by_gid(uzers::get_current_gid()) + .map(|group| group.name().to_string_lossy().to_string()) + } } impl MetaPlugin for UserMetaPlugin { - - fn finalize(&mut self, _conn: &Connection) -> Result<()> { - // Since we save during initialize(), return Ok to avoid duplicate saves - Ok(()) - } - - fn update(&mut self, _data: &[u8], _conn: &Connection) { - // No update needed + fn initialize(&mut self) -> crate::meta_plugin::MetaPluginResponse { + let mut metadata = Vec::new(); + + // Get user info + let uid = uzers::get_current_uid().to_string(); + let gid = uzers::get_current_gid().to_string(); + let username = Self::get_current_username().unwrap_or_else(|| "unknown".to_string()); + let groupname = Self::get_current_groupname().unwrap_or_else(|| "unknown".to_string()); + + // Process each output + let values = [ + ("user_uid", uid), + ("user_gid", gid), + ("user_name", username), + ("user_group", groupname), + ]; + + for (name, value) in values { + if let Some(meta_data) = crate::meta_plugin::process_metadata_outputs( + name, + value, + self.base.outputs() + ) { + metadata.push(meta_data); + } + } + + crate::meta_plugin::MetaPluginResponse { + metadata, + is_finalized: true, + } } fn meta_name(&self) -> String { - self.meta_name.clone() - } - - fn initialize(&mut self, conn: &Connection, item_id: i64) -> Result<()> { - let user = match get_current_username() { - Some(username) => username.to_string_lossy().to_string(), - None => "unknown".to_string(), - }; - self.save_meta(conn, item_id, "user", user)?; - self.is_saved = true; - Ok(()) + self.base.meta_name.clone() } fn outputs(&self) -> &std::collections::HashMap { @@ -271,200 +174,7 @@ impl MetaPlugin for UserMetaPlugin { } fn default_outputs(&self) -> Vec { - vec!["user".to_string()] - } - - fn default_options(&self) -> std::collections::HashMap { - std::collections::HashMap::new() - } - - fn options(&self) -> &std::collections::HashMap { - self.base.options() - } - - fn options_mut(&mut self) -> &mut std::collections::HashMap { - self.base.options_mut() - } -} - -#[derive(Debug, Clone, Default)] -pub struct GidMetaPlugin { - meta_name: String, - is_saved: bool, - base: crate::meta_plugin::BaseMetaPlugin, -} - -impl GidMetaPlugin { - pub fn new( - _options: Option>, - outputs: Option>, - ) -> GidMetaPlugin { - // 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 = vec!["gid".to_string()]; - 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); - } - } - - let mut base = crate::meta_plugin::BaseMetaPlugin::new(); - base.outputs = final_outputs; - base.options = final_options; - - GidMetaPlugin { - meta_name: "gid".to_string(), - is_saved: false, - base, - } - } - - pub fn new_simple() -> GidMetaPlugin { - Self::new(None, None) - } -} - -impl MetaPlugin for GidMetaPlugin { - - fn finalize(&mut self, _conn: &Connection) -> Result<()> { - // Since we save during initialize(), return Ok to avoid duplicate saves - Ok(()) - } - - fn update(&mut self, _data: &[u8], _conn: &Connection) { - // No update needed - } - - fn meta_name(&self) -> String { - self.meta_name.clone() - } - - fn initialize(&mut self, conn: &Connection, item_id: i64) -> Result<()> { - let gid = get_current_gid().to_string(); - self.save_meta(conn, item_id, "gid", gid)?; - self.is_saved = true; - Ok(()) - } - - fn outputs(&self) -> &std::collections::HashMap { - self.base.outputs() - } - - fn outputs_mut(&mut self) -> &mut std::collections::HashMap { - self.base.outputs_mut() - } - - fn default_outputs(&self) -> Vec { - vec!["gid".to_string()] - } - - fn default_options(&self) -> std::collections::HashMap { - std::collections::HashMap::new() - } - - fn options(&self) -> &std::collections::HashMap { - self.base.options() - } - - fn options_mut(&mut self) -> &mut std::collections::HashMap { - self.base.options_mut() - } -} - -#[derive(Debug, Clone, Default)] -pub struct GroupMetaPlugin { - meta_name: String, - is_saved: bool, - base: crate::meta_plugin::BaseMetaPlugin, -} - -impl GroupMetaPlugin { - pub fn new( - _options: Option>, - outputs: Option>, - ) -> GroupMetaPlugin { - // 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 = vec!["group".to_string()]; - 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); - } - } - - let mut base = crate::meta_plugin::BaseMetaPlugin::new(); - base.outputs = final_outputs; - base.options = final_options; - - GroupMetaPlugin { - meta_name: "group".to_string(), - is_saved: false, - base, - } - } - - pub fn new_simple() -> GroupMetaPlugin { - Self::new(None, None) - } -} - -impl MetaPlugin for GroupMetaPlugin { - - fn finalize(&mut self, _conn: &Connection) -> Result<()> { - // Since we save during initialize(), return Ok to avoid duplicate saves - Ok(()) - } - - fn update(&mut self, _data: &[u8], _conn: &Connection) { - // No update needed - } - - fn meta_name(&self) -> String { - self.meta_name.clone() - } - - fn initialize(&mut self, conn: &Connection, item_id: i64) -> Result<()> { - let group = match get_current_groupname() { - Some(groupname) => groupname.to_string_lossy().to_string(), - None => "unknown".to_string(), - }; - self.save_meta(conn, item_id, "group", group)?; - self.is_saved = true; - Ok(()) - } - - fn outputs(&self) -> &std::collections::HashMap { - self.base.outputs() - } - - fn outputs_mut(&mut self) -> &mut std::collections::HashMap { - self.base.outputs_mut() - } - - fn default_outputs(&self) -> Vec { - vec!["group".to_string()] + vec!["user_uid".to_string(), "user_gid".to_string(), "user_name".to_string(), "user_group".to_string()] } fn default_options(&self) -> std::collections::HashMap {