mdbook/src/cmd/clean.rs

33 lines
1.1 KiB
Rust
Raw Normal View History

2018-02-04 16:00:29 +03:00
use clap::{App, ArgMatches, SubCommand};
use get_book_dir;
2018-07-23 12:45:01 -05:00
use mdbook::errors::*;
use mdbook::MDBook;
use std::fs;
2018-02-04 16:00:29 +03:00
// Create clap subcommand arguments
pub fn make_subcommand<'a, 'b>() -> App<'a, 'b> {
SubCommand::with_name("clean")
2018-08-02 15:48:22 -05:00
.about("Deletes a built book")
2018-02-04 16:00:29 +03:00
.arg_from_usage(
2018-08-02 15:48:22 -05:00
"-d, --dest-dir=[dest-dir] 'Output directory for the book{n}\
(If omitted, uses build.build-dir from book.toml or defaults to ./book)'",
2018-08-21 10:58:44 -05:00
).arg_from_usage(
2018-08-02 15:48:22 -05:00
"[dir] 'Root directory for the book{n}\
(Defaults to the Current Directory when omitted)'",
2018-02-04 16:00:29 +03:00
)
}
// Clean command implementation
pub fn execute(args: &ArgMatches) -> ::mdbook::errors::Result<()> {
let book_dir = get_book_dir(args);
let book = MDBook::load(&book_dir)?;
let dir_to_remove = match args.value_of("dest-dir") {
2018-08-02 15:48:22 -05:00
Some(dest_dir) => dest_dir.into(),
2018-02-04 16:00:29 +03:00
None => book.root.join(&book.config.build.build_dir),
};
fs::remove_dir_all(&dir_to_remove).chain_err(|| "Unable to remove the build directory")?;
Ok(())
}