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> { | |
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