Files
keep/src/modes/delete.rs
2025-05-10 10:06:33 -03:00

50 lines
1.3 KiB
Rust

use anyhow::{anyhow, Context, Result};
use std::fs;
use std::path::PathBuf;
use crate::db;
use clap::error::ErrorKind;
use clap::Command;
use log::{debug, warn};
use rusqlite::Connection;
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(())
}