Skip to content

Commit eb0f717

Browse files
DivineGodMichael-F-Bryan
authored andcommitted
Use git config to get author name in mdbook init (#649)
* Use `git config` to get author name in `mdbook init` * Return `None` if `git` command fails * Use `.ok()?` to convert from Result to Option and return early if `None`
1 parent 5fb3675 commit eb0f717

File tree

1 file changed

+9
-14
lines changed

1 file changed

+9
-14
lines changed

src/bin/init.rs

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::io;
22
use std::io::Write;
3-
use std::env;
3+
use std::process::Command;
44
use clap::{App, ArgMatches, SubCommand};
55
use mdbook::MDBook;
66
use mdbook::errors::Result;
@@ -68,20 +68,15 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
6868
Ok(())
6969
}
7070

71-
/// Obtains author name from git config file if it can be located.
71+
/// Obtains author name from git config file by running the `git config` command.
7272
fn get_author_name() -> Option<String> {
73-
if let Some(home) = env::home_dir() {
74-
let git_config_path = home.join(".gitconfig");
75-
let content = utils::fs::file_to_string(git_config_path).unwrap();
76-
let user_name = content
77-
.lines()
78-
.filter(|x| !x.starts_with("#"))
79-
.map(|x| x.trim_left())
80-
.filter(|x| x.starts_with("name"))
81-
.next();
82-
user_name
83-
.and_then(|x| x.rsplit("=").next())
84-
.map(|x| x.trim().to_owned())
73+
let output = Command::new("git")
74+
.args(&["config", "--get", "user.name"])
75+
.output()
76+
.ok()?;
77+
78+
if output.status.success() {
79+
Some(String::from_utf8_lossy(&output.stdout).trim().to_owned())
8580
} else {
8681
None
8782
}

0 commit comments

Comments
 (0)