From 3d91385b42847c02d3ff5710b41e8e1554405d0a Mon Sep 17 00:00:00 2001 From: Andrew Phillips Date: Fri, 1 Sep 2023 15:38:05 +0000 Subject: [PATCH] Allow deleteing more than one item at once --- src/main.rs | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/src/main.rs b/src/main.rs index 99c5af0..1ad854b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -578,25 +578,24 @@ fn mode_update(cmd: &mut Command, args: Args, ids: &mut Vec, tags: &mut Vec fn mode_delete(cmd: &mut Command, _args: 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 one ID when using --delete").exit(); + 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, you must supply one ID when using --delete").exit(); - } else if ids.len() > 1 { - cmd.error(ErrorKind::InvalidValue, "More than one ID given, you must supply one ID when using --delete").exit(); + cmd.error(ErrorKind::InvalidValue, "Tags given but not supported, you must supply atleast one ID when using --delete").exit(); } - let item_id = ids.iter().next().expect("Unable to determine item id"); - let item_maybe = db::get_item(conn, *item_id)?; + 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 item = item_maybe.expect("Unable to find item in database"); - debug!("MAIN: Found item {:?}", item); + let mut item_path = data_path.clone(); + item_path.push(item_id.to_string()); - 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))?; + 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(()) }