Allow deleteing more than one item at once
This commit is contained in:
17
src/main.rs
17
src/main.rs
@@ -578,25 +578,24 @@ fn mode_update(cmd: &mut Command, args: Args, ids: &mut Vec<i64>, tags: &mut Vec
|
||||
|
||||
fn mode_delete(cmd: &mut Command, _args: 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 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)?;
|
||||
|
||||
let item = item_maybe.expect("Unable to find item in database");
|
||||
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(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user