Skip to content

Instantly share code, notes, and snippets.

@durka
Forked from barafael/main.rs
Last active March 23, 2017 18:26
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save durka/d4045a62acb3e359226098102da56f33 to your computer and use it in GitHub Desktop.
Confused about error handling
use std::process::Command;
fn main() {
println!("{}", git_name().unwrap_or("".to_string()));
}
pub fn git_name() -> Option<String> {
if let Ok(output) = Command::new("git")
.arg("config")
.arg("user.name")
.output()
{
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
}
} else {
None
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment