Return exit code of 1 if error occurs while processing a file using the CLI

Closes #93
This commit is contained in:
Josh Holmer 2017-12-27 17:18:27 -05:00
parent 30ecc0cc05
commit bb6a2513eb
2 changed files with 25 additions and 17 deletions

View file

@ -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 ### Version 0.18.2
- Bump `image` to 0.18 - Bump `image` to 0.18
- Fix unfiltering of scan lines in interlaced images ([#92](https://github.com/shssoichiro/oxipng/issues/92)) - Fix unfiltering of scan lines in interlaced images ([#92](https://github.com/shssoichiro/oxipng/issues/92))

View file

@ -17,11 +17,12 @@ use glob::glob;
use oxipng::colors::AlphaOptim; use oxipng::colors::AlphaOptim;
use oxipng::deflate::Deflaters; use oxipng::deflate::Deflaters;
use oxipng::headers::Headers; use oxipng::headers::Headers;
use oxipng::Options; use oxipng::{Options, PngError};
use regex::Regex; use regex::Regex;
use std::collections::HashSet; use std::collections::HashSet;
use std::fs::DirBuilder; use std::fs::DirBuilder;
use std::path::PathBuf; use std::path::PathBuf;
use std::process::exit;
fn main() { fn main() {
let matches = App::new("oxipng") let matches = App::new("oxipng")
@ -229,51 +230,55 @@ fn main() {
Ok(x) => x, Ok(x) => x,
Err(x) => { Err(x) => {
eprintln!("{}", x); eprintln!("{}", x);
return (); exit(1)
} }
}; };
handle_optimization( if let Err(e) = handle_optimization(
matches matches
.values_of("files") .values_of("files")
.unwrap() .unwrap()
.map(|pattern| glob(pattern).expect("Failed to parse input file path")) .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(), .collect(),
&opts, &opts,
); ) {
eprintln!("{}", e);
exit(1);
}
} }
fn handle_optimization(inputs: Vec<PathBuf>, opts: &Options) { fn handle_optimization(inputs: Vec<PathBuf>, opts: &Options) -> Result<(), PngError> {
for input in inputs { inputs.into_iter().fold(Ok(()), |res, input| {
let mut current_opts = opts.clone(); let mut current_opts = opts.clone();
if input.is_dir() { if input.is_dir() {
if current_opts.recursive { if current_opts.recursive {
handle_optimization( let cur_result = handle_optimization(
input input
.read_dir() .read_dir()
.unwrap() .unwrap()
.map(|x| x.unwrap().path()) .map(|x| x.unwrap().path())
.collect(), .collect(),
&current_opts, &current_opts,
) );
return res.and(cur_result);
} else { } else {
eprintln!("{} is a directory, skipping", input.display()); eprintln!("{} is a directory, skipping", input.display());
} }
continue; return res;
} }
if let Some(ref out_dir) = current_opts.out_dir { if let Some(ref out_dir) = current_opts.out_dir {
current_opts.out_file = out_dir.join(input.file_name().unwrap()); current_opts.out_file = out_dir.join(input.file_name().unwrap());
} else if current_opts.out_file.components().count() == 0 { } else if current_opts.out_file.components().count() == 0 {
current_opts.out_file = input.clone(); current_opts.out_file = input.clone();
} }
match oxipng::optimize(&input, &current_opts) { let cur_result = oxipng::optimize(&input, &current_opts);
Ok(_) => (), res.and(cur_result)
Err(x) => { })
eprintln!("{}", x);
}
};
}
} }
#[cfg_attr(feature = "clippy", allow(cyclomatic_complexity))] #[cfg_attr(feature = "clippy", allow(cyclomatic_complexity))]