refactor: move meta plugin finalization logic into meta plugins

Co-authored-by: aider (openai/andrew/openrouter/qwen/qwen3-coder) <aider@aider.chat>
This commit is contained in:
Andrew Phillips
2025-08-16 14:04:19 -03:00
parent 4f61306d79
commit a7977139a7
5 changed files with 153 additions and 32 deletions

View File

@@ -7,6 +7,7 @@ use std::io::Write;
use std::env;
use std::process;
use uzers::{get_current_uid, get_current_gid, get_current_username, get_current_groupname};
use rusqlite::Connection;
use crate::common::is_binary::is_binary;
use crate::meta_plugin::MetaPlugin;
@@ -21,6 +22,8 @@ pub struct BinaryMetaPlugin {
meta_name: String,
buffer: Vec<u8>,
max_buffer_size: usize,
item_id: Option<i64>,
conn: Option<*mut Connection>,
}
impl BinaryMetaPlugin {
@@ -29,6 +32,8 @@ impl BinaryMetaPlugin {
meta_name: "binary".to_string(),
buffer: Vec::new(),
max_buffer_size: 4096, // 4KB
item_id: None,
conn: None,
}
}
@@ -60,6 +65,13 @@ impl MetaPlugin for BinaryMetaPlugin {
fn meta_name(&mut self) -> String {
self.meta_name.clone()
}
fn initialize(&mut self, conn: &Connection, item_id: i64) -> Result<()> {
self.item_id = Some(item_id);
// Store raw pointer to connection - unsafe but necessary for this design
self.conn = Some(conn as *const _ as *mut Connection);
Ok(())
}
}
impl CwdMetaPlugin {
@@ -93,6 +105,15 @@ impl MetaPlugin for CwdMetaPlugin {
fn meta_name(&mut self) -> String {
self.meta_name.clone()
}
fn initialize(&mut self, conn: &Connection, item_id: i64) -> Result<()> {
let cwd = match env::current_dir() {
Ok(path) => path.to_string_lossy().to_string(),
Err(_) => "unknown".to_string(),
};
self.save_meta(conn, item_id, cwd)?;
Ok(())
}
}
#[derive(Debug, Clone, Default)]
@@ -128,6 +149,12 @@ impl MetaPlugin for UidMetaPlugin {
fn meta_name(&mut self) -> String {
self.meta_name.clone()
}
fn initialize(&mut self, conn: &Connection, item_id: i64) -> Result<()> {
let uid = get_current_uid().to_string();
self.save_meta(conn, item_id, uid)?;
Ok(())
}
}
#[derive(Debug, Clone, Default)]
@@ -166,6 +193,15 @@ impl MetaPlugin for UserMetaPlugin {
fn meta_name(&mut 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)?;
Ok(())
}
}
#[derive(Debug, Clone, Default)]
@@ -201,6 +237,12 @@ impl MetaPlugin for GidMetaPlugin {
fn meta_name(&mut 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)?;
Ok(())
}
}
#[derive(Debug, Clone, Default)]
@@ -239,6 +281,15 @@ impl MetaPlugin for GroupMetaPlugin {
fn meta_name(&mut 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)?;
Ok(())
}
}
#[derive(Debug, Clone, Default)]
@@ -277,6 +328,15 @@ impl MetaPlugin for ShellMetaPlugin {
fn meta_name(&mut 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)?;
Ok(())
}
}
#[derive(Debug, Clone, Default)]
@@ -315,6 +375,15 @@ impl MetaPlugin for ShellPidMetaPlugin {
fn meta_name(&mut 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, pid)?;
Ok(())
}
}
#[derive(Debug, Clone, Default)]
@@ -350,6 +419,12 @@ impl MetaPlugin for KeepPidMetaPlugin {
fn meta_name(&mut 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, pid)?;
Ok(())
}
}
#[derive(Debug, Clone, Default)]
@@ -388,6 +463,15 @@ impl MetaPlugin for HostnameMetaPlugin {
fn meta_name(&mut 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)?;
Ok(())
}
}
#[derive(Debug, Clone, Default)]
@@ -444,5 +528,31 @@ impl MetaPlugin for FullHostnameMetaPlugin {
fn meta_name(&mut 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, hostname)?;
Ok(())
}
}