Skip to main content

novel_cli/cmd/
zip.rs

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