Files
keep/src/meta_plugin/user.rs
Andrew Phillips fed3722ef9 fix: Resolve compilation errors by refactoring imports and type annotations
Co-authored-by: aider (openai/andrew/openrouter/deepseek/deepseek-chat-v3.1) <aider@aider.chat>
2025-09-03 09:39:22 -03:00

107 lines
3.2 KiB
Rust

use crate::meta_plugin::{MetaPlugin, MetaPluginType};
#[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();
// 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,
serde_yaml::Value::String(value),
self.base.outputs()
) {
metadata.push(meta_data);
}
}
crate::meta_plugin::MetaPluginResponse {
metadata,
is_finalized: true,
}
}
fn meta_type(&self) -> MetaPluginType {
MetaPluginType::User
}
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()
}
}
use crate::meta_plugin::register_meta_plugin;
// Register the plugin at module initialization time
#[ctor::ctor]
fn register_user_plugin() {
register_meta_plugin(MetaPluginType::User, |options, outputs| {
Box::new(UserMetaPlugin::new(options, outputs))
});
}