diff --git a/CHANGELOG.md b/CHANGELOG.md index dfed1e6f..b49558f2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +### Version 0.18.3 + - Return exit code of 1 if an error occurred while processing a file using the CLI app ([#93](https://github.com/shssoichiro/oxipng/issues/93)) + ### Version 0.18.2 - Bump `image` to 0.18 - Fix unfiltering of scan lines in interlaced images ([#92](https://github.com/shssoichiro/oxipng/issues/92)) diff --git a/src/main.rs b/src/main.rs index 4b216fb4..cd95460d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,11 +17,12 @@ use glob::glob; use oxipng::colors::AlphaOptim; use oxipng::deflate::Deflaters; use oxipng::headers::Headers; -use oxipng::Options; +use oxipng::{Options, PngError}; use regex::Regex; use std::collections::HashSet; use std::fs::DirBuilder; use std::path::PathBuf; +use std::process::exit; fn main() { let matches = App::new("oxipng") @@ -229,51 +230,55 @@ fn main() { Ok(x) => x, Err(x) => { eprintln!("{}", x); - return (); + exit(1) } }; - handle_optimization( + if let Err(e) = handle_optimization( matches .values_of("files") .unwrap() .map(|pattern| glob(pattern).expect("Failed to parse input file path")) - .flat_map(|paths| paths.into_iter().map(|path| path.expect("Failed to parse input file path"))) + .flat_map(|paths| { + paths + .into_iter() + .map(|path| path.expect("Failed to parse input file path")) + }) .collect(), &opts, - ); + ) { + eprintln!("{}", e); + exit(1); + } } -fn handle_optimization(inputs: Vec, opts: &Options) { - for input in inputs { +fn handle_optimization(inputs: Vec, opts: &Options) -> Result<(), PngError> { + inputs.into_iter().fold(Ok(()), |res, input| { let mut current_opts = opts.clone(); if input.is_dir() { if current_opts.recursive { - handle_optimization( + let cur_result = handle_optimization( input .read_dir() .unwrap() .map(|x| x.unwrap().path()) .collect(), ¤t_opts, - ) + ); + return res.and(cur_result); } else { eprintln!("{} is a directory, skipping", input.display()); } - continue; + return res; } if let Some(ref out_dir) = current_opts.out_dir { current_opts.out_file = out_dir.join(input.file_name().unwrap()); } else if current_opts.out_file.components().count() == 0 { current_opts.out_file = input.clone(); } - match oxipng::optimize(&input, ¤t_opts) { - Ok(_) => (), - Err(x) => { - eprintln!("{}", x); - } - }; - } + let cur_result = oxipng::optimize(&input, ¤t_opts); + res.and(cur_result) + }) } #[cfg_attr(feature = "clippy", allow(cyclomatic_complexity))]