refactor: Move mode_delete function to src/modes/delete.rs

This commit is contained in:
Andrew Phillips (aider)
2025-05-10 08:37:03 -03:00
parent b894eab29c
commit 93d06b4429
3 changed files with 40 additions and 24 deletions

38
src/modes/delete.rs Normal file
View File

@@ -0,0 +1,38 @@
use anyhow::{Context, Result, Error, anyhow};
use std::fs;
use std::path::PathBuf;
use crate::db::{self, Item};
use crate::compression::CompressionType;
use clap::Command;
pub fn mode_delete(
cmd: &mut Command,
_args: crate::Args,
ids: &mut Vec<i64>,
tags: &mut Vec<String>,
conn: &mut Connection,
data_path: PathBuf,
) -> Result<()> {
if ids.is_empty() {
cmd.error(ErrorKind::InvalidValue, "No ID given, you must supply atleast one ID when using --delete").exit();
} else if ! tags.is_empty() {
cmd.error(ErrorKind::InvalidValue, "Tags given but not supported, you must supply atleast one ID when using --delete").exit();
}
for item_id in ids.iter() {
if let Some(item) = db::get_item(conn, *item_id)? {
debug!("MAIN: Found item {:?}", item);
db::delete_item(conn, item)?;
let mut item_path = data_path.clone();
item_path.push(item_id.to_string());
fs::remove_file(&item_path).context(anyhow!("Unable to remove item file {:?}", item_path))?;
} else {
warn!("Unable to find item {item_id} in database");
}
}
Ok(())
}