Files
keep/src/meta_plugin/system.rs
Andrew Phillips a0fcd3f3e7 fix: remove redundant update and default_options methods
Co-authored-by: aider (openai/andrew/openrouter/deepseek/deepseek-chat-v3.1) <aider@aider.chat>
2025-08-26 17:18:56 -03:00

1020 lines
30 KiB
Rust

use anyhow::Result;
use gethostname::gethostname;
use std::env;
use std::process;
use uzers::{get_current_uid, get_current_gid, get_user_by_uid, get_group_by_gid};
use crate::meta_plugin::{MetaPlugin, MetaPluginResponse, MetaData};
use std::collections::HashMap;
#[derive(Debug, Clone, Default)]
pub struct CwdMetaPlugin {
meta_name: String,
is_saved: bool,
outputs: std::collections::HashMap<String, serde_yaml::Value>,
options: std::collections::HashMap<String, serde_yaml::Value>,
}
impl CwdMetaPlugin {
pub fn new(
_options: Option<std::collections::HashMap<String, serde_yaml::Value>>,
outputs: Option<std::collections::HashMap<String, serde_yaml::Value>>,
) -> CwdMetaPlugin {
// 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!["cwd".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);
}
}
CwdMetaPlugin {
meta_name: "cwd".to_string(),
is_saved: false,
outputs: final_outputs,
options: final_options,
}
}
pub fn new_simple() -> CwdMetaPlugin {
Self::new(None, None)
}
}
impl MetaPlugin for CwdMetaPlugin {
fn is_internal(&self) -> bool {
true
}
fn finalize(&mut self) -> MetaPluginResponse {
MetaPluginResponse {
metadata: Vec::new(),
is_finalized: true,
}
}
fn meta_name(&self) -> String {
self.meta_name.clone()
}
fn initialize(&mut self) -> MetaPluginResponse {
let mut metadata = Vec::new();
let cwd = match env::current_dir() {
Ok(path) => path.to_string_lossy().to_string(),
Err(_) => "unknown".to_string(),
};
// Use process_metadata_outputs to handle output mapping
if let Some(meta_data) = crate::meta_plugin::process_metadata_outputs(
"cwd",
cwd,
&self.outputs
) {
metadata.push(meta_data);
}
MetaPluginResponse {
metadata,
is_finalized: false,
}
}
fn outputs(&self) -> &std::collections::HashMap<String, serde_yaml::Value> {
&self.outputs
}
fn outputs_mut(&mut self) -> &mut std::collections::HashMap<String, serde_yaml::Value> {
&mut self.outputs
}
fn default_outputs(&self) -> Vec<String> {
vec!["cwd".to_string()]
}
fn options(&self) -> &std::collections::HashMap<String, serde_yaml::Value> {
&self.options
}
fn options_mut(&mut self) -> &mut std::collections::HashMap<String, serde_yaml::Value> {
&mut self.options
}
}
#[derive(Debug, Clone, Default)]
pub struct UidMetaPlugin {
meta_name: String,
is_saved: bool,
outputs: std::collections::HashMap<String, serde_yaml::Value>,
options: std::collections::HashMap<String, serde_yaml::Value>,
}
impl UidMetaPlugin {
pub fn new(
_options: Option<std::collections::HashMap<String, serde_yaml::Value>>,
outputs: Option<std::collections::HashMap<String, serde_yaml::Value>>,
) -> 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 = 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);
}
}
UidMetaPlugin {
meta_name: "uid".to_string(),
is_saved: false,
outputs: final_outputs,
options: final_options,
}
}
pub fn new_simple() -> UidMetaPlugin {
Self::new(None, None)
}
}
impl MetaPlugin for UidMetaPlugin {
fn is_internal(&self) -> bool {
true
}
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<PluginResponse> {
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<String, serde_yaml::Value> {
&self.outputs
}
fn outputs_mut(&mut self) -> &mut std::collections::HashMap<String, serde_yaml::Value> {
&mut self.outputs
}
fn default_outputs(&self) -> Vec<String> {
vec!["uid".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.options
}
fn options_mut(&mut self) -> &mut std::collections::HashMap<String, serde_yaml::Value> {
&mut self.options
}
}
#[derive(Debug, Clone, Default)]
pub struct UserMetaPlugin {
meta_name: String,
is_saved: bool,
outputs: std::collections::HashMap<String, serde_yaml::Value>,
options: std::collections::HashMap<String, serde_yaml::Value>,
}
impl UserMetaPlugin {
pub fn new(
_options: Option<std::collections::HashMap<String, serde_yaml::Value>>,
outputs: Option<std::collections::HashMap<String, serde_yaml::Value>>,
) -> 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 = 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);
}
}
UserMetaPlugin {
meta_name: "user".to_string(),
is_saved: false,
outputs: final_outputs,
options: final_options,
}
}
pub fn new_simple() -> UserMetaPlugin {
Self::new(None, None)
}
}
impl MetaPlugin for UserMetaPlugin {
fn is_internal(&self) -> bool {
true
}
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 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(())
}
fn outputs(&self) -> &std::collections::HashMap<String, serde_yaml::Value> {
&self.outputs
}
fn outputs_mut(&mut self) -> &mut std::collections::HashMap<String, serde_yaml::Value> {
&mut self.outputs
}
fn default_outputs(&self) -> Vec<String> {
vec!["user".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.options
}
fn options_mut(&mut self) -> &mut std::collections::HashMap<String, serde_yaml::Value> {
&mut self.options
}
}
#[derive(Debug, Clone, Default)]
pub struct GidMetaPlugin {
meta_name: String,
is_saved: bool,
outputs: std::collections::HashMap<String, serde_yaml::Value>,
options: std::collections::HashMap<String, serde_yaml::Value>,
}
impl GidMetaPlugin {
pub fn new(
_options: Option<std::collections::HashMap<String, serde_yaml::Value>>,
outputs: Option<std::collections::HashMap<String, serde_yaml::Value>>,
) -> 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 = 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);
}
}
GidMetaPlugin {
meta_name: "gid".to_string(),
is_saved: false,
outputs: final_outputs,
options: final_options,
}
}
pub fn new_simple() -> GidMetaPlugin {
Self::new(None, None)
}
}
impl MetaPlugin for GidMetaPlugin {
fn is_internal(&self) -> bool {
true
}
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<String, serde_yaml::Value> {
&self.outputs
}
fn outputs_mut(&mut self) -> &mut std::collections::HashMap<String, serde_yaml::Value> {
&mut self.outputs
}
fn default_outputs(&self) -> Vec<String> {
vec!["gid".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.options
}
fn options_mut(&mut self) -> &mut std::collections::HashMap<String, serde_yaml::Value> {
&mut self.options
}
}
#[derive(Debug, Clone, Default)]
pub struct GroupMetaPlugin {
meta_name: String,
is_saved: bool,
outputs: std::collections::HashMap<String, serde_yaml::Value>,
options: std::collections::HashMap<String, serde_yaml::Value>,
}
impl GroupMetaPlugin {
pub fn new(
_options: Option<std::collections::HashMap<String, serde_yaml::Value>>,
outputs: Option<std::collections::HashMap<String, serde_yaml::Value>>,
) -> 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 = 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);
}
}
GroupMetaPlugin {
meta_name: "group".to_string(),
is_saved: false,
outputs: final_outputs,
options: final_options,
}
}
pub fn new_simple() -> GroupMetaPlugin {
Self::new(None, None)
}
}
impl MetaPlugin for GroupMetaPlugin {
fn is_internal(&self) -> bool {
true
}
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<String, serde_yaml::Value> {
&self.outputs
}
fn outputs_mut(&mut self) -> &mut std::collections::HashMap<String, serde_yaml::Value> {
&mut self.outputs
}
fn default_outputs(&self) -> Vec<String> {
vec!["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.options
}
fn options_mut(&mut self) -> &mut std::collections::HashMap<String, serde_yaml::Value> {
&mut self.options
}
}
#[derive(Debug, Clone, Default)]
pub struct ShellMetaPlugin {
meta_name: String,
is_saved: bool,
outputs: std::collections::HashMap<String, serde_yaml::Value>,
options: std::collections::HashMap<String, serde_yaml::Value>,
}
impl ShellMetaPlugin {
pub fn new(
_options: Option<std::collections::HashMap<String, serde_yaml::Value>>,
outputs: Option<std::collections::HashMap<String, serde_yaml::Value>>,
) -> ShellMetaPlugin {
// 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);
}
}
ShellMetaPlugin {
meta_name: "shell".to_string(),
is_saved: false,
outputs: final_outputs,
options: final_options,
}
}
pub fn new_simple() -> ShellMetaPlugin {
Self::new(None, None)
}
}
impl MetaPlugin for ShellMetaPlugin {
fn is_internal(&self) -> bool {
true
}
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 shell = match env::var("SHELL") {
Ok(shell) => shell,
Err(_) => "unknown".to_string(),
};
self.save_meta(conn, item_id, "shell", shell)?;
self.is_saved = true;
Ok(())
}
fn outputs(&self) -> &std::collections::HashMap<String, serde_yaml::Value> {
&self.outputs
}
fn outputs_mut(&mut self) -> &mut std::collections::HashMap<String, serde_yaml::Value> {
&mut self.outputs
}
fn default_outputs(&self) -> Vec<String> {
vec!["shell".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.options
}
fn options_mut(&mut self) -> &mut std::collections::HashMap<String, serde_yaml::Value> {
&mut self.options
}
}
#[derive(Debug, Clone, Default)]
pub struct ShellPidMetaPlugin {
meta_name: String,
is_saved: bool,
outputs: std::collections::HashMap<String, serde_yaml::Value>,
options: std::collections::HashMap<String, serde_yaml::Value>,
}
impl ShellPidMetaPlugin {
pub fn new(
_options: Option<std::collections::HashMap<String, serde_yaml::Value>>,
outputs: Option<std::collections::HashMap<String, serde_yaml::Value>>,
) -> ShellPidMetaPlugin {
// 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);
}
}
ShellPidMetaPlugin {
meta_name: "shell_pid".to_string(),
is_saved: false,
outputs: final_outputs,
options: final_options,
}
}
pub fn new_simple() -> ShellPidMetaPlugin {
Self::new(None, None)
}
}
impl MetaPlugin for ShellPidMetaPlugin {
fn is_internal(&self) -> bool {
true
}
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 pid = match env::var("PPID") {
Ok(ppid) => ppid,
Err(_) => process::id().to_string(),
};
self.save_meta(conn, item_id, "shell_pid", pid)?;
self.is_saved = true;
Ok(())
}
fn outputs(&self) -> &std::collections::HashMap<String, serde_yaml::Value> {
&self.outputs
}
fn outputs_mut(&mut self) -> &mut std::collections::HashMap<String, serde_yaml::Value> {
&mut self.outputs
}
fn default_outputs(&self) -> Vec<String> {
vec!["shell_pid".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.options
}
fn options_mut(&mut self) -> &mut std::collections::HashMap<String, serde_yaml::Value> {
&mut self.options
}
}
#[derive(Debug, Clone, Default)]
pub struct KeepPidMetaPlugin {
meta_name: String,
is_saved: bool,
outputs: std::collections::HashMap<String, serde_yaml::Value>,
options: std::collections::HashMap<String, serde_yaml::Value>,
}
impl KeepPidMetaPlugin {
pub fn new(
_options: Option<std::collections::HashMap<String, serde_yaml::Value>>,
outputs: Option<std::collections::HashMap<String, serde_yaml::Value>>,
) -> KeepPidMetaPlugin {
// 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);
}
}
KeepPidMetaPlugin {
meta_name: "keep_pid".to_string(),
is_saved: false,
outputs: final_outputs,
options: final_options,
}
}
pub fn new_simple() -> KeepPidMetaPlugin {
Self::new(None, None)
}
}
impl MetaPlugin for KeepPidMetaPlugin {
fn is_internal(&self) -> bool {
true
}
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 pid = process::id().to_string();
self.save_meta(conn, item_id, "keep_pid", pid)?;
self.is_saved = true;
Ok(())
}
fn outputs(&self) -> &std::collections::HashMap<String, serde_yaml::Value> {
&self.outputs
}
fn outputs_mut(&mut self) -> &mut std::collections::HashMap<String, serde_yaml::Value> {
&mut self.outputs
}
fn default_outputs(&self) -> Vec<String> {
vec!["keep_pid".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.options
}
fn options_mut(&mut self) -> &mut std::collections::HashMap<String, serde_yaml::Value> {
&mut self.options
}
}
#[derive(Debug, Clone, Default)]
pub struct HostnameMetaPlugin {
meta_name: String,
is_saved: bool,
outputs: std::collections::HashMap<String, serde_yaml::Value>,
options: std::collections::HashMap<String, serde_yaml::Value>,
}
impl HostnameMetaPlugin {
pub fn new(
_options: Option<std::collections::HashMap<String, serde_yaml::Value>>,
outputs: Option<std::collections::HashMap<String, serde_yaml::Value>>,
) -> HostnameMetaPlugin {
// 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);
}
}
HostnameMetaPlugin {
meta_name: "hostname".to_string(),
is_saved: false,
outputs: final_outputs,
options: final_options,
}
}
pub fn new_simple() -> HostnameMetaPlugin {
Self::new(None, None)
}
}
impl MetaPlugin for HostnameMetaPlugin {
fn is_internal(&self) -> bool {
true
}
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 for hostname
}
fn meta_name(&self) -> String {
self.meta_name.clone()
}
fn initialize(&mut self, conn: &Connection, item_id: i64) -> Result<()> {
let hostname = match gethostname().into_string() {
Ok(hostname) => hostname,
Err(_) => "unknown".to_string(),
};
self.save_meta(conn, item_id, "hostname", hostname)?;
self.is_saved = true;
Ok(())
}
fn outputs(&self) -> &std::collections::HashMap<String, serde_yaml::Value> {
&self.outputs
}
fn outputs_mut(&mut self) -> &mut std::collections::HashMap<String, serde_yaml::Value> {
&mut self.outputs
}
fn default_outputs(&self) -> Vec<String> {
vec!["hostname".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.options
}
fn options_mut(&mut self) -> &mut std::collections::HashMap<String, serde_yaml::Value> {
&mut self.options
}
}
#[derive(Debug, Clone, Default)]
pub struct FullHostnameMetaPlugin {
meta_name: String,
is_saved: bool,
outputs: std::collections::HashMap<String, serde_yaml::Value>,
options: std::collections::HashMap<String, serde_yaml::Value>,
}
impl FullHostnameMetaPlugin {
pub fn new(
_options: Option<std::collections::HashMap<String, serde_yaml::Value>>,
outputs: Option<std::collections::HashMap<String, serde_yaml::Value>>,
) -> 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_saved: false,
outputs: final_outputs,
options: final_options,
}
}
pub fn new_simple() -> FullHostnameMetaPlugin {
Self::new(None, None)
}
}
impl MetaPlugin for FullHostnameMetaPlugin {
fn is_internal(&self) -> bool {
true
}
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 for full hostname
}
fn meta_name(&self) -> String {
self.meta_name.clone()
}
fn initialize(&mut self, conn: &Connection, item_id: i64) -> Result<()> {
// Try to get the FQDN through reverse DNS lookup
let hostname = match local_ip() {
Ok(my_local_ip) => {
match lookup_addr(&my_local_ip) {
Ok(hostname) => hostname,
Err(_) => {
// Fall back to regular hostname if reverse DNS fails
match gethostname().into_string() {
Ok(hostname) => hostname,
Err(_) => "unknown".to_string(),
}
}
}
}
Err(_) => {
// Fall back to regular hostname if we can't get local IP
match gethostname().into_string() {
Ok(hostname) => hostname,
Err(_) => "unknown".to_string(),
}
}
};
self.save_meta(conn, item_id, "full_hostname", hostname)?;
self.is_saved = true;
Ok(())
}
fn outputs(&self) -> &std::collections::HashMap<String, serde_yaml::Value> {
&self.outputs
}
fn outputs_mut(&mut self) -> &mut std::collections::HashMap<String, serde_yaml::Value> {
&mut self.outputs
}
fn default_outputs(&self) -> Vec<String> {
vec!["full_hostname".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.options
}
fn options_mut(&mut self) -> &mut std::collections::HashMap<String, serde_yaml::Value> {
&mut self.options
}
}