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:
Matthias Beyer 2019-10-19 08:54:57 +02:00
parent 6ab5d1d880
commit e7e5d30645
2 changed files with 33 additions and 59 deletions

View file

@ -19,6 +19,7 @@ toml = "0.5.1"
toml-query = "0.9.2" toml-query = "0.9.2"
indicatif = "0.12.0" indicatif = "0.12.0"
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" }

View file

@ -39,6 +39,7 @@ extern crate toml;
extern crate toml_query; extern crate toml_query;
extern crate indicatif; extern crate indicatif;
extern crate failure; extern crate failure;
extern crate resiter;
#[macro_use] extern crate log; #[macro_use] extern crate log;
extern crate libimagrt; extern crate libimagrt;
@ -50,20 +51,18 @@ use std::io::Write;
use libimagrt::runtime::Runtime; use libimagrt::runtime::Runtime;
use libimagrt::application::ImagApplication; use libimagrt::application::ImagApplication;
use libimagerror::trace::MapErrTrace;
use libimagerror::io::ToExitCode;
use libimagerror::exit::ExitUnwrap;
use libimagstore::store::FileLockEntry; use libimagstore::store::FileLockEntry;
use libimagstore::storeid::StoreId; use libimagstore::storeid::StoreId;
use libimagentrylink::linkable::Linkable; use libimagentrylink::linkable::Linkable;
use libimagerror::iter::IterInnerOkOrElse;
use toml::Value; use toml::Value;
use toml_query::read::TomlValueReadExt; use toml_query::read::TomlValueReadExt;
use indicatif::{ProgressBar, ProgressStyle}; use indicatif::{ProgressBar, ProgressStyle};
use failure::Fallible as Result; use failure::Fallible as Result;
use failure::Error;
use failure::err_msg; use failure::err_msg;
use clap::App; use clap::App;
use resiter::AndThen;
use std::collections::BTreeMap; use std::collections::BTreeMap;
@ -106,20 +105,6 @@ impl Diagnostic {
} }
} }
macro_rules! do_write {
($dest:ident, $pattern:tt) => {
let _ = writeln!($dest, $pattern)
.to_exit_code()
.unwrap_or_exit();
};
($dest:ident, $pattern:tt, $( $args:expr ),*) => {
let _ = writeln!($dest, $pattern, $( $args ),*)
.to_exit_code()
.unwrap_or_exit();
}
}
/// Marker enum for implementing ImagApplication on /// Marker enum for implementing ImagApplication on
/// ///
/// This is used by binaries crates to execute business logic /// This is used by binaries crates to execute business logic
@ -127,8 +112,8 @@ macro_rules! do_write {
pub enum ImagDiagnostics {} pub enum ImagDiagnostics {}
impl ImagApplication for ImagDiagnostics { impl ImagApplication for ImagDiagnostics {
fn run(rt: Runtime) -> Result<()> { fn run(rt: Runtime) -> Result<()> {
let template = get_config(&rt, "rt.progressbar_style"); let template = get_config(&rt, "rt.progressbar_style")?;
let tick_chars = get_config(&rt, "rt.progressticker_chars"); let tick_chars = get_config(&rt, "rt.progressticker_chars")?;
let verbose = rt.cli().is_present("more-output"); let verbose = rt.cli().is_present("more-output");
let style = if let Some(tick_chars) = tick_chars { let style = if let Some(tick_chars) = tick_chars {
@ -143,22 +128,16 @@ impl ImagApplication for ImagDiagnostics {
spinner.set_message("Accumulating data"); spinner.set_message("Accumulating data");
let diags = rt.store() let diags = rt.store()
.entries() .entries()?
.map_err_trace_exit_unwrap()
.into_get_iter() .into_get_iter()
.map(|e| { .map_inner_ok_or_else(|| err_msg("Unable to get entry"))
e.map_err_trace_exit_unwrap() .and_then_ok(|e| {
.ok_or_else(|| Error::from(err_msg("Unable to get entry".to_owned())))
.map_err_trace_exit_unwrap()
})
.map(|e| {
let diag = Diagnostic::for_entry(&e); let diag = Diagnostic::for_entry(&e);
debug!("Diagnostic for '{:?}' = {:?}", e.get_location(), diag); debug!("Diagnostic for '{:?}' = {:?}", e.get_location(), diag);
drop(e); drop(e);
diag diag
}) })
.collect::<Result<Vec<_>>>() .collect::<Result<Vec<_>>>()?;
.map_err_trace_exit_unwrap();
spinner.finish(); spinner.finish();
let n = diags.len(); let n = diags.len();
@ -220,38 +199,37 @@ impl ImagApplication for ImagDiagnostics {
let mut out = rt.stdout(); let mut out = rt.stdout();
do_write!(out, "imag version {}", { env!("CARGO_PKG_VERSION") }); write!(out, "imag version {}", { env!("CARGO_PKG_VERSION") })?;
do_write!(out, ""); write!(out, "")?;
do_write!(out, "{} entries", n); write!(out, "{} entries", n)?;
for (k, v) in version_counts { for (k, v) in version_counts {
do_write!(out, "{} entries with store version '{}'", v, k); write!(out, "{} entries with store version '{}'", v, k)?;
} }
if n != 0 { if n != 0 {
do_write!(out, "{} header sections in the average entry", sum_header_sections / n); write!(out, "{} header sections in the average entry", sum_header_sections / n)?;
do_write!(out, "{} average content bytecount", sum_bytecount_content / n); write!(out, "{} average content bytecount", sum_bytecount_content / n)?;
do_write!(out, "{} average overall bytecount", sum_overall_byte_size / n); write!(out, "{} average overall bytecount", sum_overall_byte_size / n)?;
if let Some((num, path)) = max_overall_byte_size { if let Some((num, path)) = max_overall_byte_size {
do_write!(out, "Largest Entry ({} bytes): {}", num, path.local_display_string()); write!(out, "Largest Entry ({} bytes): {}", num, path.local_display_string())?;
} }
do_write!(out, "{} average internal link count per entry", num_links / n); write!(out, "{} average internal link count per entry", num_links / n)?;
if let Some((num, path)) = max_links { if let Some((num, path)) = max_links {
do_write!(out, "Entry with most internal links ({}): {}", write!(out, "Entry with most internal links ({}): {}",
num, num,
path.local_display_string()); path.local_display_string())?;
} }
do_write!(out, "{} verified entries", verified_count); write!(out, "{} verified entries", verified_count)?;
do_write!(out, "{} unverified entries", unverified_count); write!(out, "{} unverified entries", unverified_count)?;
if verbose { if verbose {
for unve in unverified_entries.iter() { for unve in unverified_entries.iter() {
do_write!(out, "Unverified: {}", unve); write!(out, "Unverified: {}", unve)?;
} }
} }
} }
Ok(()) Ok(())
} }
@ -272,17 +250,12 @@ impl ImagApplication for ImagDiagnostics {
} }
} }
fn get_config(rt: &Runtime, s: &'static str) -> Option<String> { fn get_config(rt: &Runtime, s: &'static str) -> Result<Option<String>> {
rt.config().and_then(|cfg| { let cfg = rt.config().ok_or_else(|| err_msg("No configuration"))?;
cfg.read(s)
.map_err(Error::from) match cfg.read(s)? {
.map_err_trace_exit_unwrap() Some(&Value::String(ref s)) => Ok(Some(s.to_owned())),
.map(|opt| match opt { Some(_) => Err(err_msg("Config type wrong: 'rt.progressbar_style' should be a string")),
&Value::String(ref s) => s.to_owned(), None => Ok(None),
_ => { }
error!("Config type wrong: 'rt.progressbar_style' should be a string");
::std::process::exit(1)
}
})
})
} }