From 3eff48a8e801a17271fcf1f680951a6fbc5445d6 Mon Sep 17 00:00:00 2001 From: Roey Darwish Dror Date: Mon, 11 Jun 2018 08:57:55 +0300 Subject: [PATCH] Return a failure code if a step failed (fix #26) --- src/main.rs | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/src/main.rs b/src/main.rs index d928b0e7..465d2373 100644 --- a/src/main.rs +++ b/src/main.rs @@ -24,7 +24,7 @@ use git::{Git, Repositories}; use report::{Report, Reporter}; use std::env::home_dir; use std::path::{Path, PathBuf}; -use std::process::ExitStatus; +use std::process::{exit, ExitStatus}; use steps::*; use terminal::Terminal; use which::which; @@ -33,6 +33,10 @@ use which::which; #[fail(display = "Process failed")] struct ProcessFailed; +#[derive(Fail, Debug)] +#[fail(display = "A step failed")] +struct StepFailed; + trait Check { fn check(self) -> Result<(), Error>; } @@ -77,7 +81,7 @@ fn tpm() -> Option { } } -fn main() -> Result<(), Error> { +fn run() -> Result<(), Error> { let git = Git::new(); let mut git_repos = Repositories::new(&git); let terminal = Terminal::new(); @@ -224,10 +228,29 @@ fn main() -> Result<(), Error> { if !reports.is_empty() { terminal.print_separator("Summary"); - for (key, succeeded) in reports { - terminal.print_result(key, succeeded); + for (key, succeeded) in &reports { + terminal.print_result(key, *succeeded); } } - Ok(()) + if reports.iter().all(|(_, succeeded)| *succeeded) { + Ok(()) + } else { + Err(StepFailed.into()) + } +} + +fn main() { + match run() { + Ok(()) => { + exit(0); + } + Err(error) => { + match error.downcast::() { + Ok(_) => (), + Err(error) => println!("ERROR: {}", error), + }; + exit(1); + } + } }