Skip to content

Commit af036d9

Browse files
authored
Merge pull request #2057 from seanpoulter/init-skip
fix(cli): init --force skips confirmation prompts
2 parents 41567b0 + b9c6b32 commit af036d9

File tree

4 files changed

+32
-1
lines changed

4 files changed

+32
-1
lines changed

guide/src/cli/init.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,7 @@ mdbook init --ignore=git
7676
```
7777

7878
[building]: build.md
79+
80+
#### --force
81+
82+
Skip the prompts to create a `.gitignore` and for the title for the book.

src/cmd/init.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
5656
"git" => builder.create_gitignore(true),
5757
_ => builder.create_gitignore(false),
5858
};
59-
} else {
59+
} else if !args.get_flag("force") {
6060
println!("\nDo you want a .gitignore to be created? (y/n)");
6161
if confirm() {
6262
builder.create_gitignore(true);
@@ -65,6 +65,8 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
6565

6666
config.book.title = if args.contains_id("title") {
6767
args.get_one::<String>("title").map(String::from)
68+
} else if args.get_flag("force") {
69+
None
6870
} else {
6971
request_book_title()
7072
};

tests/cli/init.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use crate::cli::cmd::mdbook_cmd;
2+
use crate::dummy_book::DummyBook;
3+
4+
use mdbook::config::Config;
5+
6+
/// Run `mdbook init` with `--force` to skip the confirmation prompts
7+
#[test]
8+
fn base_mdbook_init_can_skip_confirmation_prompts() {
9+
let temp = DummyBook::new().build().unwrap();
10+
11+
// doesn't exist before
12+
assert!(!temp.path().join("book").exists());
13+
14+
let mut cmd = mdbook_cmd();
15+
cmd.args(["init", "--force"]).current_dir(temp.path());
16+
cmd.assert()
17+
.success()
18+
.stdout(predicates::str::contains("\nAll done, no errors...\n"));
19+
20+
let config = Config::from_disk(temp.path().join("book.toml")).unwrap();
21+
assert_eq!(config.book.title, None);
22+
23+
assert!(!temp.path().join(".gitignore").exists());
24+
}

tests/cli/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
mod build;
22
mod cmd;
3+
mod init;
34
mod test;

0 commit comments

Comments
 (0)