]> git.proxmox.com Git - rustc.git/blob - src/vendor/mdbook/src/bin/init.rs
New upstream version 1.31.0+dfsg1
[rustc.git] / src / vendor / mdbook / src / bin / init.rs
1 use std::io;
2 use std::io::Write;
3 use std::process::Command;
4 use clap::{App, ArgMatches, SubCommand};
5 use mdbook::MDBook;
6 use mdbook::errors::Result;
7 use mdbook::config;
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
15 .arg_from_usage("[dir] 'A directory for your book{n}(Defaults to Current Directory \
16 when omitted)'")
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<()> {
23 let book_dir = get_book_dir(args);
24 let mut builder = MDBook::init(&book_dir);
25 let mut config = config::Config::default();
26
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") {
32 // Print warning
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.");
39 print!("\nAre you sure you want to continue? (y/n) ");
40
41 // Read answer from user and exit if it's not 'yes'
42 if confirm() {
43 builder.copy_theme(true);
44 }
45 } else {
46 builder.copy_theme(true);
47 }
48 }
49
50 println!("\nDo you want a .gitignore to be created? (y/n)");
51
52 if confirm() {
53 builder.create_gitignore(true);
54 }
55
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
64 builder.build()?;
65 println!("\nAll done, no errors...");
66
67 Ok(())
68 }
69
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
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 }