fix: improve error messages and refactor large functions in save/diff modes

Co-authored-by: aider (openai/andrew/openrouter/qwen/qwen3-coder) <aider@aider.chat>
This commit is contained in:
Andrew Phillips
2025-08-10 00:00:33 -03:00
parent 498f3e0b9d
commit 58f047ba6d
3 changed files with 354 additions and 225 deletions

View File

@@ -1,57 +1,39 @@
use anyhow::{Context, Result, anyhow};
use gethostname::gethostname;
use is_terminal::IsTerminal;
use std::collections::HashMap;
use std::io::{self, Read, Write};
use clap::Command;
use clap::error::ErrorKind;
use log::debug;
use rusqlite::Connection;
use std::path::PathBuf;
use crate::compression_engine::get_compression_engine;
use crate::db::{self};
use crate::meta_plugin::{MetaPlugin, MetaPluginType, get_meta_plugin};
use crate::modes::common::{cmd_args_compression_type, cmd_args_digest_type, cmd_args_meta_plugin_types, get_meta_from_env, store_item_meta_value};
use chrono::Utc;
pub fn mode_save(
cmd: &mut Command,
args: &crate::Args,
ids: &mut Vec<i64>,
tags: &mut Vec<String>,
conn: &mut Connection,
data_path: PathBuf,
) -> Result<()> {
fn validate_save_args(cmd: &mut Command, ids: &Vec<i64>) {
if !ids.is_empty() {
cmd.error(
ErrorKind::InvalidValue,
clap::error::ErrorKind::InvalidValue,
"ID given, you cannot supply IDs when using --save",
)
.exit();
}
}
fn initialize_tags(tags: &mut Vec<String>) {
if tags.is_empty() {
tags.push("none".to_string());
}
}
fn setup_compression_and_plugins(
cmd: &mut Command,
args: &crate::Args,
) -> (crate::compression_engine::CompressionType, Box<dyn crate::compression_engine::CompressionEngine>, Vec<Box<dyn crate::meta_plugin::MetaPlugin>>) {
let digest_type = cmd_args_digest_type(cmd, &args);
debug!("MAIN: Digest type: {:?}", digest_type);
let compression_type = cmd_args_compression_type(cmd, &args);
debug!("MAIN: Compression type: {:?}", compression_type);
let compression_engine =
get_compression_engine(compression_type.clone()).expect("Unable to get compression engine");
crate::compression_engine::get_compression_engine(compression_type.clone()).expect("Unable to get compression engine");
// Start with meta plugin types from command line
let mut meta_plugin_types: Vec<MetaPluginType> = cmd_args_meta_plugin_types(cmd, &args);
let mut meta_plugin_types: Vec<crate::meta_plugin::MetaPluginType> = cmd_args_meta_plugin_types(cmd, &args);
debug!("MAIN: Meta plugin types: {:?}", meta_plugin_types);
// Convert digest type to meta plugin type and add to the list if needed
let digest_meta_plugin_type = match digest_type {
crate::meta_plugin::MetaPluginType::DigestSha256 => Some(MetaPluginType::DigestSha256),
crate::meta_plugin::MetaPluginType::DigestMd5 => Some(MetaPluginType::DigestMd5),
crate::meta_plugin::MetaPluginType::DigestSha256 => Some(crate::meta_plugin::MetaPluginType::DigestSha256),
crate::meta_plugin::MetaPluginType::DigestMd5 => Some(crate::meta_plugin::MetaPluginType::DigestMd5),
_ => None,
};
@@ -63,9 +45,9 @@ pub fn mode_save(
}
// Initialize meta_plugins with MetaPlugin instances for each MetaPluginType
let mut meta_plugins: Vec<Box<dyn MetaPlugin>> = meta_plugin_types
let mut meta_plugins: Vec<Box<dyn crate::meta_plugin::MetaPlugin>> = meta_plugin_types
.iter()
.map(|meta_plugin_type| get_meta_plugin(meta_plugin_type.clone()))
.map(|meta_plugin_type| crate::meta_plugin::get_meta_plugin(meta_plugin_type.clone()))
.collect();
// Check for unsupported meta plugins, warn the user, and remove them from the list
@@ -76,21 +58,30 @@ pub fn mode_save(
// We need to get the meta name for the warning message
// Since we can't mutably borrow meta_plugin here, we create a temporary one
let meta_plugin_type = meta_plugin_types[i].clone();
let mut temp_plugin = get_meta_plugin(meta_plugin_type);
let mut temp_plugin = crate::meta_plugin::get_meta_plugin(meta_plugin_type);
eprintln!("Warning: Meta plugin '{}' is enabled but not supported on this system", temp_plugin.meta_name());
}
i += 1;
is_supported
});
let mut item = db::Item {
(compression_type, compression_engine, meta_plugins)
}
fn create_and_log_item(
conn: &mut rusqlite::Connection,
args: &crate::Args,
tags: &Vec<String>,
compression_type: &crate::compression_engine::CompressionType,
) -> Result<crate::db::Item> {
let mut item = crate::db::Item {
id: None,
ts: Utc::now(),
ts: chrono::Utc::now(),
size: None,
compression: compression_type.to_string(),
};
let id = db::insert_item(conn, item.clone())?;
let id = crate::db::insert_item(conn, item.clone())?;
item.id = Some(id);
debug!("MAIN: Added item {:?}", item.clone());
@@ -117,14 +108,23 @@ pub fn mode_save(
}
}
db::set_item_tags(conn, item.clone(), tags)?;
Ok(item)
}
// Use a transaction for database operations to ensure atomicity
let tx = conn.transaction()?;
fn setup_item_metadata(
conn: &rusqlite::Connection,
args: &crate::Args,
item: &crate::db::Item,
tags: &Vec<String>,
) -> Result<()> {
crate::db::set_item_tags(conn, item.clone(), tags)?;
Ok(())
}
let mut item_meta: HashMap<String, String> = get_meta_from_env();
fn collect_item_meta(args: &crate::Args) -> std::collections::HashMap<String, String> {
let mut item_meta: std::collections::HashMap<String, String> = crate::modes::common::get_meta_from_env();
if let Ok(hostname) = gethostname().into_string() {
if let Ok(hostname) = gethostname::gethostname().into_string() {
if !item_meta.contains_key("hostname") {
item_meta.insert("hostname".to_string(), hostname);
}
@@ -135,30 +135,35 @@ pub fn mode_save(
item_meta.insert(item.key, item.value);
}
let item_id = item.id.ok_or_else(|| anyhow!("Item missing ID"))?;
for kv in item_meta.iter() {
let meta = db::Meta {
id: item_id,
name: kv.0.to_string(),
value: kv.1.to_string(),
};
db::store_meta(&tx, meta)?;
}
item_meta
}
fn process_input_stream(
compression_engine: &Box<dyn crate::compression_engine::CompressionEngine>,
data_path: &std::path::PathBuf,
item_id: i64,
meta_plugins: &mut Vec<Box<dyn crate::meta_plugin::MetaPlugin>>,
) -> Result<(Box<dyn std::io::Write>, crate::db::Item)> {
let mut item = crate::db::Item {
id: Some(item_id),
ts: chrono::Utc::now(),
size: None,
compression: String::new(), // Will be set later
};
let mut item_path = data_path.clone();
item_path.push(item_id.to_string());
let mut stdin = io::stdin().lock();
let mut stdout = io::stdout().lock();
let mut stdin = std::io::stdin().lock();
let mut stdout = std::io::stdout().lock();
let mut buffer = [0; libc::BUFSIZ as usize];
let mut item_out: Box<dyn Write> =
let mut item_out: Box<dyn std::io::Write> =
compression_engine
.create(item_path.clone())
.context(anyhow!(
"Unable to write file {:?} using compression {:?}",
item_path,
compression_type
.context(anyhow::anyhow!(
"Unable to write file {:?}",
item_path
))?;
debug!("MAIN: Starting IO loop");
@@ -188,12 +193,20 @@ pub fn mode_save(
stdout.flush()?;
item_out.flush()?;
Ok((item_out, item))
}
fn finalize_meta_plugins(
conn: &rusqlite::Connection,
meta_plugins: &mut Vec<Box<dyn crate::meta_plugin::MetaPlugin>>,
item: &crate::db::Item,
) -> Result<()> {
for meta_plugin in meta_plugins.iter_mut() {
let meta_name = meta_plugin.meta_name();
match meta_plugin.finalize() {
Ok(meta_value) => {
if let Err(e) = store_item_meta_value(&tx, item.clone(), meta_name.clone(), meta_value) {
if let Err(e) = crate::modes::common::store_item_meta_value(conn, item.clone(), meta_name.clone(), meta_value) {
eprintln!("Warning: Failed to store meta value for {}: {}", meta_name, e);
}
}
@@ -202,8 +215,52 @@ pub fn mode_save(
}
}
}
Ok(())
}
db::update_item(&tx, item.clone())?;
pub fn mode_save(
cmd: &mut Command,
args: &crate::Args,
ids: &mut Vec<i64>,
tags: &mut Vec<String>,
conn: &mut rusqlite::Connection,
data_path: std::path::PathBuf,
) -> Result<()> {
validate_save_args(cmd, ids);
initialize_tags(tags);
let (compression_type, compression_engine, mut meta_plugins) = setup_compression_and_plugins(cmd, args);
let mut item = create_and_log_item(conn, args, tags, &compression_type)?;
setup_item_metadata(conn, args, &item, tags)?;
// Use a transaction for database operations to ensure atomicity
let tx = conn.transaction()?;
let item_meta = collect_item_meta(args);
let item_id = item.id.ok_or_else(|| anyhow::anyhow!("Item missing ID"))?;
for kv in item_meta.iter() {
let meta = crate::db::Meta {
id: item_id,
name: kv.0.to_string(),
value: kv.1.to_string(),
};
crate::db::store_meta(&tx, meta)?;
}
let (_item_out, processed_item) = process_input_stream(
&compression_engine,
&data_path,
item_id,
&mut meta_plugins,
)?;
item.size = processed_item.size;
item.compression = compression_type.to_string();
finalize_meta_plugins(&tx, &mut meta_plugins, &item)?;
crate::db::update_item(&tx, item.clone())?;
// Commit the transaction
tx.commit()?;