2017-06-26 07:24:33 +08:00
|
|
|
use std::io;
|
|
|
|
use std::io::Write;
|
2017-10-03 19:40:23 +08:00
|
|
|
use clap::{App, ArgMatches, SubCommand};
|
2017-06-26 07:02:32 +08:00
|
|
|
use mdbook::MDBook;
|
2017-06-27 05:17:46 +08:00
|
|
|
use mdbook::errors::Result;
|
2017-06-26 07:24:33 +08:00
|
|
|
use get_book_dir;
|
2017-06-26 07:02:32 +08:00
|
|
|
|
2017-06-27 13:59:50 +08:00
|
|
|
// Create clap subcommand arguments
|
|
|
|
pub fn make_subcommand<'a, 'b>() -> App<'a, 'b> {
|
|
|
|
SubCommand::with_name("init")
|
|
|
|
.about("Create boilerplate structure and files in the directory")
|
|
|
|
// the {n} denotes a newline which will properly aligned in all help messages
|
2017-10-03 19:40:23 +08:00
|
|
|
.arg_from_usage("[dir] 'A directory for your book{n}(Defaults to Current Directory \
|
|
|
|
when omitted)'")
|
2017-06-27 13:59:50 +08:00
|
|
|
.arg_from_usage("--theme 'Copies the default theme into your source folder'")
|
|
|
|
.arg_from_usage("--force 'skip confirmation prompts'")
|
|
|
|
}
|
|
|
|
|
2017-06-26 07:02:32 +08:00
|
|
|
// Init command implementation
|
2017-06-27 13:59:50 +08:00
|
|
|
pub fn execute(args: &ArgMatches) -> Result<()> {
|
2017-06-26 07:02:32 +08:00
|
|
|
let book_dir = get_book_dir(args);
|
2017-11-18 20:41:04 +08:00
|
|
|
let mut builder = MDBook::init(&book_dir);
|
2017-06-26 07:02:32 +08:00
|
|
|
|
|
|
|
// If flag `--theme` is present, copy theme to src
|
|
|
|
if args.is_present("theme") {
|
|
|
|
// Skip this if `--force` is present
|
|
|
|
if !args.is_present("force") {
|
|
|
|
// Print warning
|
2017-11-18 20:41:04 +08:00
|
|
|
print!("\nCopying the default theme to {}", builder.config().book.src.display());
|
|
|
|
println!("This could potentially overwrite files already present in that directory.");
|
2017-06-26 07:02:32 +08:00
|
|
|
print!("\nAre you sure you want to continue? (y/n) ");
|
|
|
|
|
|
|
|
// Read answer from user and exit if it's not 'yes'
|
2017-11-18 20:41:04 +08:00
|
|
|
if confirm() {
|
|
|
|
builder.copy_theme(true);
|
2017-06-26 07:02:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-18 20:41:04 +08:00
|
|
|
println!("\nDo you want a .gitignore to be created? (y/n)");
|
2017-06-26 07:02:32 +08:00
|
|
|
|
2017-11-18 20:41:04 +08:00
|
|
|
if confirm() {
|
|
|
|
builder.create_gitignore(true);
|
2017-06-26 07:02:32 +08:00
|
|
|
}
|
|
|
|
|
2017-11-18 20:41:04 +08:00
|
|
|
builder.build()?;
|
2017-06-26 07:02:32 +08:00
|
|
|
println!("\nAll done, no errors...");
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2017-06-26 07:24:33 +08:00
|
|
|
// Simple function that user comfirmation
|
|
|
|
fn confirm() -> bool {
|
|
|
|
io::stdout().flush().unwrap();
|
|
|
|
let mut s = String::new();
|
|
|
|
io::stdin().read_line(&mut s).ok();
|
|
|
|
match &*s.trim() {
|
|
|
|
"Y" | "y" | "yes" | "Yes" => true,
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|