1use std::env;
2use std::fs::File;
3use std::path::PathBuf;
4
5use clap::Args;
6use color_eyre::eyre::Result;
7use fluent_templates::Loader;
8use walkdir::WalkDir;
9
10use crate::{LANG_ID, LOCALES, utils};
11
12#[must_use]
13#[derive(Args)]
14#[command(arg_required_else_help = true,
15 about = LOCALES.lookup(&LANG_ID, "zip_command"))]
16pub struct Zip {
17 #[arg(help = LOCALES.lookup(&LANG_ID, "epub_dir_path"))]
18 pub epub_dir_path: PathBuf,
19
20 #[arg(short, long, default_value_t = false,
21 help = LOCALES.lookup(&LANG_ID, "delete"))]
22 pub delete: bool,
23}
24
25pub fn execute(config: Zip) -> Result<()> {
26 utils::ensure_epub_dir(&config.epub_dir_path)?;
27
28 let epub_file_path = env::current_dir()?
29 .join(config.epub_dir_path.file_stem().unwrap())
30 .with_extension("epub");
31 if epub_file_path.try_exists()? {
32 tracing::warn!("The epub output file already exists and will be deleted");
33 utils::remove_file_or_dir(&epub_file_path)?;
34 }
35
36 let file = File::create(epub_file_path)?;
37 let walkdir = WalkDir::new(&config.epub_dir_path);
38 super::zip_dir(
39 &mut walkdir.into_iter().filter_map(|e| e.ok()),
40 &config.epub_dir_path,
41 file,
42 )?;
43
44 if config.delete {
45 utils::remove_file_or_dir(&config.epub_dir_path)?;
46 }
47
48 Ok(())
49}