eureka_manager_cli/commands/delete/
cover.rs1use std::{fs::File, io::BufReader, path::PathBuf, str::FromStr};
2
3use clap::Args;
4use eureka_mmanager::prelude::{DeleteDataAsyncTrait, GetManagerStateData};
5use indicatif::ProgressBar;
6use log::info;
7use uuid::Uuid;
8
9use crate::commands::{AsyncRun, AsyncRunContext};
10
11#[derive(Debug, Args)]
12pub struct CoverDeleteArgs {
13 pub ids: Vec<Uuid>,
15 #[arg(long)]
16 pub id_text_file: Vec<PathBuf>,
17}
18
19impl CoverDeleteArgs {
20 pub fn get_ids(&self) -> Vec<Uuid> {
21 let mut ids = self.ids.clone();
22 self.id_text_file
23 .iter()
24 .map(|e| (e, File::open(e)))
25 .flat_map(|(path, res)| match res {
26 Ok(file) => Some(id_list_txt_reader::IdListTxtReader::new(BufReader::new(
27 file,
28 ))),
29 Err(err) => {
30 log::error!("Cannot open the {} file: {}", path.to_string_lossy(), err);
31 None
32 }
33 })
34 .flat_map(|file| file.flat_map(|s| Uuid::from_str(&s)))
35 .for_each(|id| {
36 ids.push(id);
37 });
38 ids.dedup();
39 ids
40 }
41}
42
43impl AsyncRun for CoverDeleteArgs {
44 async fn run(&self, ctx: AsyncRunContext) -> anyhow::Result<()> {
45 let ids = self.get_ids();
46 let pb = ProgressBar::new(ids.len() as u64);
47 let pb = ctx.progress.add(pb);
48 info!("Deleting {} cover", ids.len());
49
50 let dir_option = ctx.manager.get_dir_options().await?;
51 for id in &ids {
52 info!("Deleting cover {id}");
53 dir_option.delete_cover(*id).await?;
54 info!("Deleted cover {id}");
55 pb.inc(1);
56 }
57 pb.finish();
58 ctx.progress.remove(&pb);
59 Ok(())
60 }
61}