From 93d06b4429a2945a3955fbb35a08c648e00db162 Mon Sep 17 00:00:00 2001 From: "Andrew Phillips (aider)" Date: Sat, 10 May 2025 08:37:03 -0300 Subject: [PATCH] refactor: Move mode_delete function to src/modes/delete.rs --- src/main.rs | 25 +------------------------ src/modes/delete.rs | 38 ++++++++++++++++++++++++++++++++++++++ src/modes/get.rs | 1 + 3 files changed, 40 insertions(+), 24 deletions(-) create mode 100644 src/modes/delete.rs diff --git a/src/main.rs b/src/main.rs index 4e8d67b..7602e7a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -319,7 +319,7 @@ fn main() -> Result<(), Error> { KeepModes::List => mode_list(&mut cmd, args, ids, tags, &mut conn, data_path)?, KeepModes::Update => mode_update(&mut cmd, args, ids, tags, &mut conn, data_path)?, KeepModes::Info => mode_info(&mut cmd, args, ids, tags, &mut conn, data_path)?, - KeepModes::Delete => mode_delete(&mut cmd, args, ids, tags, &mut conn, data_path)?, + KeepModes::Delete => crate::modes::delete::mode_delete(&mut cmd, args, ids, tags, &mut conn, data_path)?, KeepModes::Status => mode_status(&mut cmd, args, data_path, db_path)?, _ => todo!() } @@ -998,29 +998,6 @@ fn mode_info(cmd: &mut Command, args: Args, ids: &mut Vec, tags: &mut Vec, tags: &mut Vec, 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(()) -} fn mode_status(_cmd: &mut Command, args: Args, data_path: PathBuf, db_path: PathBuf) -> Result<()> { diff --git a/src/modes/delete.rs b/src/modes/delete.rs new file mode 100644 index 0000000..6b837b8 --- /dev/null +++ b/src/modes/delete.rs @@ -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, + tags: &mut Vec, + 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(()) +} diff --git a/src/modes/get.rs b/src/modes/get.rs index cc6a58e..68c99a5 100644 --- a/src/modes/get.rs +++ b/src/modes/get.rs @@ -2,6 +2,7 @@ use anyhow::anyhow; use clap::Command; +mod delete; use crate::compression::CompressionType; use std::str::FromStr; use std::path::PathBuf;