refactor: remove system directory from meta_plugin
This commit is contained in:
committed by
Andrew Phillips (aider)
parent
29079ccb24
commit
73f23ff036
105
src/meta_plugin/user.rs
Normal file
105
src/meta_plugin/user.rs
Normal file
@@ -0,0 +1,105 @@
|
||||
use std::env;
|
||||
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,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new_simple() -> UserMetaPlugin {
|
||||
Self::new(None, None)
|
||||
}
|
||||
|
||||
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 default_options(&self) -> std::collections::HashMap<String, serde_yaml::Value> {
|
||||
std::collections::HashMap::new()
|
||||
}
|
||||
|
||||
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()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user