summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias Beyer <mail@beyermatthias.de>2019-04-30 16:29:27 +0200
committerGitHub <noreply@github.com>2019-04-30 16:29:27 +0200
commit632b7e43de2df4f90da0fe14d85fae8aac13b151 (patch)
tree538a3390d613b8175c1fdf8c566305fdf409452e
parent77f8ed46dc3511e354e42c23976097c87c707024 (diff)
parent17a57a38aec2a8e8d9649b788be54ca0e85098cd (diff)
downloadrepolocli-632b7e43de2df4f90da0fe14d85fae8aac13b151.tar.gz
repolocli-632b7e43de2df4f90da0fe14d85fae8aac13b151.tar.xz
Merge pull request #11 from matthiasbeyer/pretty-errors
Make error output pretty
-rw-r--r--Cargo.toml1
-rw-r--r--src/main.rs61
2 files changed, 39 insertions, 23 deletions
diff --git a/Cargo.toml b/Cargo.toml
index 474a983..a6db2ea 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -19,6 +19,7 @@ log = "0.4"
flexi_logger = "0.11"
prettytable-rs = "0.8"
filters = "0.3"
+boolinator = "2"
[dependencies.clap]
version = ">=2.33"
diff --git a/src/main.rs b/src/main.rs
index 1ee2b94..82236a8 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -6,6 +6,7 @@ extern crate url;
extern crate xdg;
extern crate flexi_logger;
extern crate filters;
+extern crate boolinator;
#[cfg(feature = "compare_csv")]
extern crate csv;
@@ -28,9 +29,11 @@ use std::io::Cursor;
use failure::err_msg;
use failure::Error;
+use failure::ResultExt;
use failure::Fallible as Result;
use clap::ArgMatches;
use filters::filter::Filter;
+use boolinator::Boolinator;
use config::Configuration;
use compare::ComparePackage;
@@ -91,7 +94,7 @@ fn deserialize_package_list(s: String, filepath: &str) -> Result<Vec<ComparePack
}
}
-fn main() -> Result<()> {
+fn app() -> Result<()> {
let app = cli::build_cli().get_matches();
initialize_logging(&app)?;
let config : Configuration = {
@@ -110,7 +113,9 @@ fn main() -> Result<()> {
let buffer = std::fs::read_to_string(path).map_err(Error::from)?;
trace!("Config read into memory");
- toml::de::from_str(&buffer).map_err(Error::from)
+ toml::de::from_str(&buffer)
+ .map_err(Error::from)
+ .context("Configuration file parsing")
}?;
trace!("Config deserialized");
@@ -160,7 +165,7 @@ fn main() -> Result<()> {
.into_iter()
.filter(|package| repository_filter.filter(package.repo()))
.collect();
- frontend.list_packages(packages)?;
+ frontend.list_packages(packages)
},
("problems", Some(mtch)) => {
trace!("Handling problems");
@@ -178,35 +183,45 @@ fn main() -> Result<()> {
.filter(|problem| repository_filter.filter(problem.repo()))
.collect();
- frontend.list_problems(problems)?;
+ frontend.list_problems(problems)
},
("compare", Some(mtch)) => {
- let repos = mtch.values_of("compare-distros").unwrap().map(|s| Repo::new(String::from(s))).collect();
+ let repos = mtch.values_of("compare-distros").unwrap().map(String::from).map(Repo::new).collect();
let file_path = mtch.value_of("compare-list").unwrap(); // safe by clap
- let content = ::std::fs::read_to_string(file_path).map_err(Error::from)?;
+ let content = ::std::fs::read_to_string(file_path)?;
let pkgs : Vec<ComparePackage> = deserialize_package_list(content, file_path)?;
- frontend.compare_packages(pkgs, &backend, repos)?;
+ frontend.compare_packages(pkgs, &backend, repos)
},
(other, _mtch) => {
- if app.is_present("input_stdin") {
- // Ugly, but works:
- // If we have "--stdin" on CLI, we have a CLI/Stdin backend, which means that we can query
- // _any_ "project", and get the stdin anyways. This is really not like it should be, but
- // works for now
- let packages = backend
- .project("")?
- .into_iter()
- .filter(|package| repository_filter.filter(package.repo()))
- .collect();
- frontend.list_packages(packages)?;
- } else {
- error!("Unknown command: '{}'", other);
- ::std::process::exit(1)
- }
+ app.is_present("input_stdin")
+ .as_result((), Error::from(format_err!("Input not from stdin")))
+ .and_then(|_| {
+ // Ugly, but works:
+ // If we have "--stdin" on CLI, we have a CLI/Stdin backend, which means that we can query
+ // _any_ "project", and get the stdin anyways. This is really not like it should be, but
+ // works for now
+ let packages = backend
+ .project("")?
+ .into_iter()
+ .filter(|package| repository_filter.filter(package.repo()))
+ .collect();
+
+ frontend.list_packages(packages)
+ })
+ .map_err(|_| format_err!("Unknown command: {}", other))
}
}
+}
+
+fn print_error(e: Error) {
+ error!("Error: {}", e);
+ e.iter_causes().for_each(|cause| {
+ error!("Caused by: {}", cause)
+ });
+}
- Ok(())
+fn main() {
+ let _ = app().map_err(print_error);
}