refactor: Move mode_delete function to src/modes/delete.rs
This commit is contained in:
25
src/main.rs
25
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::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::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::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)?,
|
KeepModes::Status => mode_status(&mut cmd, args, data_path, db_path)?,
|
||||||
_ => todo!()
|
_ => todo!()
|
||||||
}
|
}
|
||||||
@@ -998,29 +998,6 @@ fn mode_info(cmd: &mut Command, args: Args, ids: &mut Vec<i64>, tags: &mut Vec<S
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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 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<()> {
|
fn mode_status(_cmd: &mut Command, args: Args, data_path: PathBuf, db_path: PathBuf) -> Result<()> {
|
||||||
|
|||||||
38
src/modes/delete.rs
Normal file
38
src/modes/delete.rs
Normal file
@@ -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<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(())
|
||||||
|
}
|
||||||
@@ -2,6 +2,7 @@
|
|||||||
use anyhow::anyhow;
|
use anyhow::anyhow;
|
||||||
|
|
||||||
use clap::Command;
|
use clap::Command;
|
||||||
|
mod delete;
|
||||||
use crate::compression::CompressionType;
|
use crate::compression::CompressionType;
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|||||||
Reference in New Issue
Block a user