diff options
author | Alyssa Ross <hi@alyssa.is> | 2022-05-03 16:46:06 +0000 |
---|---|---|
committer | Alyssa Ross <hi@alyssa.is> | 2022-05-03 16:59:29 +0000 |
commit | ec7c061fc7a6d13b00030ab9726d0b060524db9d (patch) | |
tree | 6254e70b424dedc94bc3b57464902cfd31c0e871 | |
parent | 3b7c0bc774e8eaf08457369cb3e23b70f042ec17 (diff) | |
download | hydrasect-ec7c061fc7a6d13b00030ab9726d0b060524db9d.tar hydrasect-ec7c061fc7a6d13b00030ab9726d0b060524db9d.tar.gz hydrasect-ec7c061fc7a6d13b00030ab9726d0b060524db9d.tar.bz2 hydrasect-ec7c061fc7a6d13b00030ab9726d0b060524db9d.tar.lz hydrasect-ec7c061fc7a6d13b00030ab9726d0b060524db9d.tar.xz hydrasect-ec7c061fc7a6d13b00030ab9726d0b060524db9d.tar.zst hydrasect-ec7c061fc7a6d13b00030ab9726d0b060524db9d.zip |
Handle errors properly at the top level
-rw-r--r-- | hydrasect-search.rs | 30 |
1 files changed, 24 insertions, 6 deletions
diff --git a/hydrasect-search.rs b/hydrasect-search.rs index 751005e..348b54f 100644 --- a/hydrasect-search.rs +++ b/hydrasect-search.rs @@ -3,14 +3,14 @@ use std::cmp::min; use std::collections::{BTreeMap, BTreeSet}; -use std::env; +use std::env::{self, args}; use std::ffi::OsStr; use std::fmt::{self, Debug, Display, Formatter}; use std::fs::{create_dir_all, rename, File}; use std::io::{self, BufRead, BufReader, ErrorKind, Read, Seek, SeekFrom}; use std::os::unix::prelude::*; use std::path::{Path, PathBuf}; -use std::process::{Command, ExitStatus, Stdio}; +use std::process::{exit, Command, ExitStatus, Stdio}; use std::str; use std::time::{Duration, SystemTime}; @@ -516,11 +516,29 @@ fn open_history_file() -> Result<File, String> { Ok(file) } -fn main() { - let history = read_history(BufReader::new(open_history_file().unwrap())).unwrap(); - let head = git_rev_parse("HEAD").unwrap(); +fn run() -> Result<(), String> { + let history_file = open_history_file() + .map(BufReader::new) + .map_err(|e| format!("opening history file: {}", e))?; + let history = read_history(history_file).map_err(|e| format!("reading history file: {}", e))?; + let head = git_rev_parse("HEAD").map_err(|e| format!("resolving HEAD: {}", e))?; + let graph = bisect_graph().map_err(|e| format!("finding bisect graph: {}", e))?; - for commit in closest_commits(head, bisect_graph().unwrap(), history) { + for commit in closest_commits(head, graph, history) { println!("{}", commit); } + + Ok(()) +} + +fn main() { + let argv0_option = args().next(); + let argv0 = argv0_option + .as_ref() + .map(String::as_str) + .unwrap_or("hydrasect-search"); + if let Err(e) = run() { + eprintln!("{}: {}", argv0, e); + exit(1); + } } |