]>
Commit | Line | Data |
---|---|---|
ea8adc8c XL |
1 | use std::io; |
2 | use std::io::Write; | |
83c7162d | 3 | use std::process::Command; |
2c00a5a8 | 4 | use clap::{App, ArgMatches, SubCommand}; |
ea8adc8c XL |
5 | use mdbook::MDBook; |
6 | use mdbook::errors::Result; | |
83c7162d | 7 | use mdbook::config; |
ea8adc8c XL |
8 | use get_book_dir; |
9 | ||
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 | |
2c00a5a8 XL |
15 | .arg_from_usage("[dir] 'A directory for your book{n}(Defaults to Current Directory \ |
16 | when omitted)'") | |
ea8adc8c XL |
17 | .arg_from_usage("--theme 'Copies the default theme into your source folder'") |
18 | .arg_from_usage("--force 'skip confirmation prompts'") | |
19 | } | |
20 | ||
21 | // Init command implementation | |
22 | pub fn execute(args: &ArgMatches) -> Result<()> { | |
ea8adc8c | 23 | let book_dir = get_book_dir(args); |
2c00a5a8 | 24 | let mut builder = MDBook::init(&book_dir); |
83c7162d | 25 | let mut config = config::Config::default(); |
ea8adc8c XL |
26 | |
27 | // If flag `--theme` is present, copy theme to src | |
28 | if args.is_present("theme") { | |
83c7162d | 29 | config.set("output.html.theme", "src/theme")?; |
ea8adc8c XL |
30 | // Skip this if `--force` is present |
31 | if !args.is_present("force") { | |
32 | // Print warning | |
2c00a5a8 XL |
33 | println!(); |
34 | println!( | |
35 | "Copying the default theme to {}", | |
36 | builder.config().book.src.display() | |
37 | ); | |
38 | println!("This could potentially overwrite files already present in that directory."); | |
ea8adc8c XL |
39 | print!("\nAre you sure you want to continue? (y/n) "); |
40 | ||
41 | // Read answer from user and exit if it's not 'yes' | |
2c00a5a8 XL |
42 | if confirm() { |
43 | builder.copy_theme(true); | |
ea8adc8c | 44 | } |
83c7162d XL |
45 | } else { |
46 | builder.copy_theme(true); | |
ea8adc8c | 47 | } |
ea8adc8c XL |
48 | } |
49 | ||
2c00a5a8 | 50 | println!("\nDo you want a .gitignore to be created? (y/n)"); |
ea8adc8c | 51 | |
2c00a5a8 XL |
52 | if confirm() { |
53 | builder.create_gitignore(true); | |
ea8adc8c XL |
54 | } |
55 | ||
83c7162d XL |
56 | config.book.title = request_book_title(); |
57 | ||
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); | |
62 | } | |
63 | ||
2c00a5a8 | 64 | builder.build()?; |
ea8adc8c XL |
65 | println!("\nAll done, no errors..."); |
66 | ||
67 | Ok(()) | |
68 | } | |
69 | ||
83c7162d XL |
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"]) | |
74 | .output() | |
75 | .ok()?; | |
76 | ||
77 | if output.status.success() { | |
78 | Some(String::from_utf8_lossy(&output.stdout).trim().to_owned()) | |
79 | } else { | |
80 | None | |
81 | } | |
82 | } | |
83 | ||
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(); | |
91 | if resp.is_empty() { | |
92 | None | |
93 | } else { | |
94 | Some(resp.into()) | |
95 | } | |
96 | } | |
97 | ||
98 | // Simple function for user confirmation | |
ea8adc8c XL |
99 | fn confirm() -> bool { |
100 | io::stdout().flush().unwrap(); | |
101 | let mut s = String::new(); | |
102 | io::stdin().read_line(&mut s).ok(); | |
103 | match &*s.trim() { | |
104 | "Y" | "y" | "yes" | "Yes" => true, | |
105 | _ => false, | |
106 | } | |
107 | } |