eureka_manager_cli/commands/delete/
chapter.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 ChapterDeleteArgs {
13 pub ids: Vec<Uuid>,
15 #[arg(long)]
16 pub id_text_file: Vec<PathBuf>,
17}
18
19impl ChapterDeleteArgs {
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 ChapterDeleteArgs {
44 async fn run(&self, ctx: AsyncRunContext) -> anyhow::Result<()> {
45 let ids = self.get_ids();
46 let pb = ctx.progress.add(ProgressBar::new(ids.len() as u64));
47 info!("Deleting {} chapter", ids.len());
48 let dir_option = ctx.manager.get_dir_options().await?;
49 for id in &ids {
50 info!("Deleting chapter {id}");
51 dir_option.delete_chapter(*id).await?;
52 info!("Deleted chapter {id}");
53 pb.inc(1);
54 }
55 pb.finish();
56 ctx.progress.remove(&pb);
57 Ok(())
58 }
59}