3 use std
::process
::Command
;
4 use clap
::{App, ArgMatches, SubCommand}
;
6 use mdbook
::errors
::Result
;
10 // Create clap subcommand arguments
11 pub fn make_subcommand
<'a
, 'b
>() -> App
<'a
, 'b
> {
12 SubCommand
::with_name("init")
13 .about("Create boilerplate structure and files in the directory")
14 // the {n} denotes a newline which will properly aligned in all help messages
15 .arg_from_usage("[dir] 'A directory for your book{n}(Defaults to Current Directory \
17 .arg_from_usage("--theme 'Copies the default theme into your source folder'")
18 .arg_from_usage("--force 'skip confirmation prompts'")
21 // Init command implementation
22 pub fn execute(args
: &ArgMatches
) -> Result
<()> {
23 let book_dir
= get_book_dir(args
);
24 let mut builder
= MDBook
::init(&book_dir
);
25 let mut config
= config
::Config
::default();
27 // If flag `--theme` is present, copy theme to src
28 if args
.is_present("theme") {
29 config
.set("output.html.theme", "src/theme")?
;
30 // Skip this if `--force` is present
31 if !args
.is_present("force") {
35 "Copying the default theme to {}",
36 builder
.config().book
.src
.display()
38 println
!("This could potentially overwrite files already present in that directory.");
39 print
!("\nAre you sure you want to continue? (y/n) ");
41 // Read answer from user and exit if it's not 'yes'
43 builder
.copy_theme(true);
46 builder
.copy_theme(true);
50 println
!("\nDo you want a .gitignore to be created? (y/n)");
53 builder
.create_gitignore(true);
56 config
.book
.title
= request_book_title();
58 if let Some(author
) = get_author_name() {
59 debug
!("Obtained user name from gitconfig: {:?}", author
);
60 config
.book
.authors
.push(author
);
61 builder
.with_config(config
);
65 println
!("\nAll done, no errors...");
70 /// Obtains author name from git config file by running the `git config` command.
71 fn get_author_name() -> Option
<String
> {
72 let output
= Command
::new("git")
73 .args(&["config", "--get", "user.name"])
77 if output
.status
.success() {
78 Some(String
::from_utf8_lossy(&output
.stdout
).trim().to_owned())
84 /// Request book title from user and return if provided.
85 fn request_book_title() -> Option
<String
> {
86 println
!("What title would you like to give the book? ");
87 io
::stdout().flush().unwrap();
88 let mut resp
= String
::new();
89 io
::stdin().read_line(&mut resp
).unwrap();
90 let resp
= resp
.trim();
98 // Simple function for user confirmation
99 fn confirm() -> bool
{
100 io
::stdout().flush().unwrap();
101 let mut s
= String
::new();
102 io
::stdin().read_line(&mut s
).ok();
104 "Y" | "y" | "yes" | "Yes" => true,