Created
March 23, 2017 18:19
-
-
Save barafael/646e3b88c1b2bdccb7f82e921af1ae52 to your computer and use it in GitHub Desktop.
Confused about error handling
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::process::Command; | |
fn main() { | |
println!("{}", git_name().unwrap_or("".to_string())); | |
} | |
pub fn git_name() -> Option<String> { | |
let output = Command::new("git") | |
.arg("config") | |
.arg("user.name") | |
.output() | |
.expect("Couldn't run git!"); | |
if output.status.success() { | |
let s = String::from_utf8_lossy(&output.stdout); | |
print!("git config user.name: {}", s); | |
Some(s.to_string()) | |
} else { | |
let s = String::from_utf8_lossy(&output.stderr); | |
print!("git config user.name failed! {}", s); | |
None | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment