Files
keep/src/meta_plugin/user.rs
Andrew Phillips fdcccc844e refactor: remove new_simple methods and replace with new(None, None)
Co-authored-by: aider (openai/andrew/openrouter/deepseek/deepseek-chat-v3.1) <aider@aider.chat>
2025-08-27 13:59:37 -03:00

99 lines
3.0 KiB
Rust

use crate::meta_plugin::MetaPlugin;
#[derive(Debug, Clone, Default)]
pub struct UserMetaPlugin {
base: crate::meta_plugin::BaseMetaPlugin,
}
impl UserMetaPlugin {
pub fn new(
options: Option<std::collections::HashMap<String, serde_yaml::Value>>,
outputs: Option<std::collections::HashMap<String, serde_yaml::Value>>,
) -> UserMetaPlugin {
let mut base = crate::meta_plugin::BaseMetaPlugin::new();
base.meta_name = "user".to_string();
// Initialize with helper function
base.initialize_plugin(
&["user_uid", "user_gid", "user_name", "user_group"],
options,
outputs,
);
UserMetaPlugin {
base,
}
}
fn get_current_username() -> Option<String> {
uzers::get_user_by_uid(uzers::get_current_uid())
.map(|user| user.name().to_string_lossy().to_string())
}
fn get_current_groupname() -> Option<String> {
uzers::get_group_by_gid(uzers::get_current_gid())
.map(|group| group.name().to_string_lossy().to_string())
}
}
impl MetaPlugin for UserMetaPlugin {
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.base.meta_name.clone()
}
fn outputs(&self) -> &std::collections::HashMap<String, serde_yaml::Value> {
self.base.outputs()
}
fn outputs_mut(&mut self) -> &mut std::collections::HashMap<String, serde_yaml::Value> {
self.base.outputs_mut()
}
fn default_outputs(&self) -> Vec<String> {
vec!["user_uid".to_string(), "user_gid".to_string(), "user_name".to_string(), "user_group".to_string()]
}
fn options(&self) -> &std::collections::HashMap<String, serde_yaml::Value> {
self.base.options()
}
fn options_mut(&mut self) -> &mut std::collections::HashMap<String, serde_yaml::Value> {
self.base.options_mut()
}
}