Remove calls to exit() and replace them with error propagation up to main()
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
This commit is contained in:
parent
05685a6f2d
commit
df2c5faf73
2 changed files with 31 additions and 28 deletions
|
@ -23,6 +23,7 @@ maintenance = { status = "actively-developed" }
|
||||||
log = "0.4.6"
|
log = "0.4.6"
|
||||||
regex = "1.1.7"
|
regex = "1.1.7"
|
||||||
failure = "0.1.5"
|
failure = "0.1.5"
|
||||||
|
resiter = "0.3.0"
|
||||||
|
|
||||||
libimagstore = { version = "0.10.0", path = "../../../lib/core/libimagstore" }
|
libimagstore = { version = "0.10.0", path = "../../../lib/core/libimagstore" }
|
||||||
libimagrt = { version = "0.10.0", path = "../../../lib/core/libimagrt" }
|
libimagrt = { version = "0.10.0", path = "../../../lib/core/libimagrt" }
|
||||||
|
|
|
@ -35,9 +35,10 @@
|
||||||
)]
|
)]
|
||||||
|
|
||||||
#[macro_use] extern crate log;
|
#[macro_use] extern crate log;
|
||||||
|
#[macro_use] extern crate failure;
|
||||||
extern crate clap;
|
extern crate clap;
|
||||||
extern crate regex;
|
extern crate regex;
|
||||||
extern crate failure;
|
extern crate resiter;
|
||||||
|
|
||||||
extern crate libimagstore;
|
extern crate libimagstore;
|
||||||
extern crate libimagrt;
|
extern crate libimagrt;
|
||||||
|
@ -46,15 +47,17 @@ extern crate libimagerror;
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
|
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
use failure::Fallible as Result;
|
|
||||||
use clap::App;
|
use clap::App;
|
||||||
|
use failure::Error;
|
||||||
|
use failure::Fallible as Result;
|
||||||
|
use failure::err_msg;
|
||||||
|
use resiter::AndThen;
|
||||||
|
|
||||||
use libimagrt::runtime::Runtime;
|
use libimagrt::runtime::Runtime;
|
||||||
use libimagrt::application::ImagApplication;
|
use libimagrt::application::ImagApplication;
|
||||||
use libimagstore::store::Entry;
|
use libimagstore::store::Entry;
|
||||||
use libimagerror::trace::MapErrTrace;
|
use libimagerror::iter::IterInnerOkOrElse;
|
||||||
use libimagerror::exit::ExitUnwrap;
|
|
||||||
use libimagerror::io::ToExitCode;
|
|
||||||
|
|
||||||
mod ui;
|
mod ui;
|
||||||
|
|
||||||
|
@ -82,34 +85,32 @@ impl ImagApplication for ImagGrep {
|
||||||
.value_of("pattern")
|
.value_of("pattern")
|
||||||
.map(Regex::new)
|
.map(Regex::new)
|
||||||
.unwrap() // ensured by clap
|
.unwrap() // ensured by clap
|
||||||
.unwrap_or_else(|e| {
|
.map_err(|e| format_err!("Regex building error: {:?}", e))?;
|
||||||
error!("Regex building error: {:?}", e);
|
|
||||||
::std::process::exit(1)
|
|
||||||
});
|
|
||||||
|
|
||||||
let overall_count = rt
|
let overall_count = rt
|
||||||
.store()
|
.store()
|
||||||
.entries()
|
.entries()?
|
||||||
.map_err_trace_exit_unwrap()
|
|
||||||
.into_get_iter()
|
.into_get_iter()
|
||||||
.filter_map(|res| res.map_err_trace_exit_unwrap())
|
.map_inner_ok_or_else(|| err_msg("Entry from entries missing"))
|
||||||
.filter_map(|entry| if pattern.is_match(entry.get_content()) {
|
.and_then_ok(|entry| {
|
||||||
show(&rt, &entry, &pattern, &opts, &mut count);
|
if pattern.is_match(entry.get_content()) {
|
||||||
Some(())
|
debug!("Matched: {}", entry.get_location());
|
||||||
} else {
|
show(&rt, &entry, &pattern, &opts, &mut count)
|
||||||
None
|
} else {
|
||||||
|
debug!("Not matched: {}", entry.get_location());
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
})
|
})
|
||||||
.count();
|
.collect::<Result<Vec<_>>>()?
|
||||||
|
.len();
|
||||||
|
|
||||||
if opts.count {
|
if opts.count {
|
||||||
writeln!(rt.stdout(), "{}", count).to_exit_code().unwrap_or_exit();
|
writeln!(rt.stdout(), "{}", count)?;
|
||||||
} else if !opts.files_with_matches {
|
} else if !opts.files_with_matches {
|
||||||
writeln!(rt.stdout(), "Processed {} files, {} matches, {} nonmatches",
|
writeln!(rt.stdout(), "Processed {} files, {} matches, {} nonmatches",
|
||||||
overall_count,
|
overall_count,
|
||||||
count,
|
count,
|
||||||
overall_count - count)
|
overall_count - count)?;
|
||||||
.to_exit_code()
|
|
||||||
.unwrap_or_exit();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -130,27 +131,28 @@ impl ImagApplication for ImagGrep {
|
||||||
fn version() -> &'static str {
|
fn version() -> &'static str {
|
||||||
env!("CARGO_PKG_VERSION")
|
env!("CARGO_PKG_VERSION")
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn show(rt: &Runtime, e: &Entry, re: &Regex, opts: &Options, count: &mut usize) {
|
fn show(rt: &Runtime, e: &Entry, re: &Regex, opts: &Options, count: &mut usize) -> Result<()> {
|
||||||
if opts.files_with_matches {
|
if opts.files_with_matches {
|
||||||
writeln!(rt.stdout(), "{}", e.get_location()).to_exit_code().unwrap_or_exit();
|
writeln!(rt.stdout(), "{}", e.get_location())?;
|
||||||
} else if opts.count {
|
} else if opts.count {
|
||||||
*count += 1;
|
*count += 1;
|
||||||
} else {
|
} else {
|
||||||
writeln!(rt.stdout(), "{}:", e.get_location()).to_exit_code().unwrap_or_exit();
|
writeln!(rt.stdout(), "{}:", e.get_location())?;
|
||||||
for capture in re.captures_iter(e.get_content()) {
|
for capture in re.captures_iter(e.get_content()) {
|
||||||
for mtch in capture.iter() {
|
for mtch in capture.iter() {
|
||||||
if let Some(m) = mtch {
|
if let Some(m) = mtch {
|
||||||
writeln!(rt.stdout(), " '{}'", m.as_str()).to_exit_code().unwrap_or_exit();
|
writeln!(rt.stdout(), " '{}'", m.as_str())?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
writeln!(rt.stdout()).to_exit_code().unwrap_or_exit();
|
writeln!(rt.stdout())?;
|
||||||
*count += 1;
|
*count += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
rt.report_touched(e.get_location()).unwrap_or_exit();
|
rt.report_touched(e.get_location()).map_err(Error::from)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue