eureka_manager_cli/commands/delete/
manga.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 MangaDeleteArgs {
13 pub ids: Vec<Uuid>,
15 #[arg(long)]
16 pub id_text_file: Vec<PathBuf>,
17}
18
19impl MangaDeleteArgs {
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 MangaDeleteArgs {
44 async fn run(&self, ctx: AsyncRunContext) -> anyhow::Result<()> {
45 let ids = self.get_ids();
46 let progress = ProgressBar::new(ids.len() as u64);
47 ctx.progress.add(progress.clone());
48 info!("Deleting {} titles", ids.len());
49 let dir_option = ctx.manager.get_dir_options().await?;
50 for id in &ids {
51 info!("Deleting title {id}");
52 let delete_data = dir_option.delete_manga(*id).await?;
53 info!("Deleted title {id}");
54 info!(
55 "Deleted {} covers: {:?}",
56 delete_data.covers.len(),
57 delete_data.covers
58 );
59 info!(
60 "Deleted {} chapters: {:?}",
61 delete_data.chapters.len(),
62 delete_data.chapters
63 );
64 progress.inc(1);
65 }
66 progress.finish();
67 ctx.progress.remove(&progress);
68 Ok(())
69 }
70}