Do not exit on first non-PNG file found in recursion

Closes #170
This commit is contained in:
Josh Holmer 2020-08-12 10:59:43 -04:00
parent 3202c36846
commit 90c99e4509

View file

@ -20,7 +20,6 @@ use oxipng::AlphaOptim;
use oxipng::Deflaters; use oxipng::Deflaters;
use oxipng::Headers; use oxipng::Headers;
use oxipng::Options; use oxipng::Options;
use oxipng::PngResult;
use oxipng::{InFile, OutFile}; use oxipng::{InFile, OutFile};
use std::fs::DirBuilder; use std::fs::DirBuilder;
use std::path::PathBuf; use std::path::PathBuf;
@ -264,13 +263,26 @@ fn main() {
true, true,
); );
let res: PngResult<()> = files let mut success = false;
.into_iter() for (input, output) in files {
.map(|(input, output)| oxipng::optimize(&input, &output, &opts)) match oxipng::optimize(&input, &output, &opts) {
.collect(); // For optimizing single files, this will return the correct exit code always.
// For recursive optimization, the correct choice is a bit subjective.
// We're choosing to return a 0 exit code if ANY file in the set
// runs correctly.
// The reason for this is that recursion may pick up files that are not
// PNG files, and return an error for them.
// We don't really want to return an error code for those files.
Ok(_) => {
success = true;
}
Err(e) => {
error!("{}", e);
}
};
}
if let Err(e) = res { if !success {
error!("{}", e);
exit(1); exit(1);
} }
} }
@ -288,12 +300,16 @@ fn collect_files(
let using_stdin = allow_stdin && input.to_str().map_or(false, |p| p == "-"); let using_stdin = allow_stdin && input.to_str().map_or(false, |p| p == "-");
if !using_stdin && input.is_dir() { if !using_stdin && input.is_dir() {
if recursive { if recursive {
let files = input match input.read_dir() {
.read_dir() Ok(dir) => {
.unwrap() let files = dir.filter_map(|x| x.ok().map(|x| x.path())).collect();
.map(|x| x.unwrap().path()) in_out_pairs
.collect(); .extend(collect_files(files, out_dir, out_file, recursive, false));
in_out_pairs.extend(collect_files(files, out_dir, out_file, recursive, false)); }
Err(_) => {
return Vec::new();
}
}
} else { } else {
warn!("{} is a directory, skipping", input.display()); warn!("{} is a directory, skipping", input.display());
} }