Allow deleteing more than one item at once
This commit is contained in:
27
src/main.rs
27
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<()> {
|
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() {
|
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() {
|
} else if ! tags.is_empty() {
|
||||||
cmd.error(ErrorKind::InvalidValue, "Tags 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();
|
||||||
} else if ids.len() > 1 {
|
|
||||||
cmd.error(ErrorKind::InvalidValue, "More than one ID given, you must supply one ID when using --delete").exit();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let item_id = ids.iter().next().expect("Unable to determine item id");
|
for item_id in ids.iter() {
|
||||||
let item_maybe = db::get_item(conn, *item_id)?;
|
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");
|
let mut item_path = data_path.clone();
|
||||||
debug!("MAIN: Found item {:?}", item);
|
item_path.push(item_id.to_string());
|
||||||
|
|
||||||
db::delete_item(conn, item)?;
|
fs::remove_file(&item_path).context(anyhow!("Unable to remove item file {:?}", item_path))?;
|
||||||
|
} else {
|
||||||
let mut item_path = data_path.clone();
|
warn!("Unable to find item {item_id} in database");
|
||||||
item_path.push(item_id.to_string());
|
}
|
||||||
|
}
|
||||||
fs::remove_file(&item_path).context(anyhow!("Unable to remove item file {:?}", item_path))?;
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user