From eb0f7179ab1822b91360dc758362f7ec359c5cc3 Mon Sep 17 00:00:00 2001 From: Anders Rasmussen Date: Tue, 27 Mar 2018 01:37:11 +1100 Subject: [PATCH] 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` --- src/bin/init.rs | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/src/bin/init.rs b/src/bin/init.rs index b51ee73a..f22618ba 100644 --- a/src/bin/init.rs +++ b/src/bin/init.rs @@ -1,6 +1,6 @@ use std::io; use std::io::Write; -use std::env; +use std::process::Command; use clap::{App, ArgMatches, SubCommand}; use mdbook::MDBook; use mdbook::errors::Result; @@ -68,20 +68,15 @@ pub fn execute(args: &ArgMatches) -> Result<()> { Ok(()) } -/// Obtains author name from git config file if it can be located. +/// Obtains author name from git config file by running the `git config` command. fn get_author_name() -> Option { - if let Some(home) = env::home_dir() { - let git_config_path = home.join(".gitconfig"); - let content = utils::fs::file_to_string(git_config_path).unwrap(); - let user_name = content - .lines() - .filter(|x| !x.starts_with("#")) - .map(|x| x.trim_left()) - .filter(|x| x.starts_with("name")) - .next(); - user_name - .and_then(|x| x.rsplit("=").next()) - .map(|x| x.trim().to_owned()) + let output = Command::new("git") + .args(&["config", "--get", "user.name"]) + .output() + .ok()?; + + if output.status.success() { + Some(String::from_utf8_lossy(&output.stdout).trim().to_owned()) } else { None }