Use ExitCode in main
This commit is contained in:
parent
8740f475d7
commit
910e779b09
1 changed files with 36 additions and 22 deletions
58
src/main.rs
58
src/main.rs
|
|
@ -18,7 +18,8 @@ mod rayon;
|
||||||
|
|
||||||
#[cfg(feature = "zopfli")]
|
#[cfg(feature = "zopfli")]
|
||||||
use std::num::NonZeroU8;
|
use std::num::NonZeroU8;
|
||||||
use std::{ffi::OsString, fs::DirBuilder, io::Write, path::PathBuf, process::exit, time::Duration};
|
use std::process::ExitCode;
|
||||||
|
use std::{ffi::OsString, fs::DirBuilder, io::Write, path::PathBuf, time::Duration};
|
||||||
|
|
||||||
use clap::ArgMatches;
|
use clap::ArgMatches;
|
||||||
mod cli;
|
mod cli;
|
||||||
|
|
@ -29,7 +30,7 @@ use rayon::prelude::*;
|
||||||
|
|
||||||
use crate::cli::DISPLAY_CHUNKS;
|
use crate::cli::DISPLAY_CHUNKS;
|
||||||
|
|
||||||
fn main() {
|
fn main() -> ExitCode {
|
||||||
let matches = cli::build_command()
|
let matches = cli::build_command()
|
||||||
// Set the value parser for filters which isn't appropriate to do in the build_command function
|
// Set the value parser for filters which isn't appropriate to do in the build_command function
|
||||||
.mut_arg("filters", |arg| {
|
.mut_arg("filters", |arg| {
|
||||||
|
|
@ -43,15 +44,15 @@ fn main() {
|
||||||
.get_matches_from(std::env::args());
|
.get_matches_from(std::env::args());
|
||||||
|
|
||||||
if matches.get_flag("backup") {
|
if matches.get_flag("backup") {
|
||||||
eprintln!("The --backup flag is no longer supported. Please use --out or --dir to preserve your existing files.");
|
error!("The --backup flag is no longer supported. Please use --out or --dir to preserve your existing files.");
|
||||||
exit(1)
|
return ExitCode::FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
let (out_file, out_dir, opts) = match parse_opts_into_struct(&matches) {
|
let (out_file, out_dir, opts) = match parse_opts_into_struct(&matches) {
|
||||||
Ok(x) => x,
|
Ok(x) => x,
|
||||||
Err(x) => {
|
Err(x) => {
|
||||||
error!("{}", x);
|
error!("{}", x);
|
||||||
exit(1)
|
return ExitCode::FAILURE;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -75,28 +76,41 @@ fn main() {
|
||||||
true,
|
true,
|
||||||
);
|
);
|
||||||
|
|
||||||
let success = files.into_par_iter().filter(|(input, output)| {
|
let summary = files
|
||||||
match oxipng::optimize(input, output, &opts) {
|
.into_par_iter()
|
||||||
// For optimizing single files, this will return the correct exit code always.
|
.map(|(input, output)| {
|
||||||
// For recursive optimization, the correct choice is a bit subjective.
|
match oxipng::optimize(&input, &output, &opts) {
|
||||||
// We're choosing to return a 0 exit code if ANY file in the set
|
// For optimizing single files, this will return the correct exit code always.
|
||||||
// runs correctly.
|
// For recursive optimization, the correct choice is a bit subjective.
|
||||||
// The reason for this is that recursion may pick up files that are not
|
// We're choosing to return a 0 exit code if ANY file in the set
|
||||||
// PNG files, and return an error for them.
|
// runs correctly.
|
||||||
// We don't really want to return an error code for those files.
|
// The reason for this is that recursion may pick up files that are not
|
||||||
Ok(_) => true,
|
// PNG files, and return an error for them.
|
||||||
Err(e) => {
|
// We don't really want to return an error code for those files.
|
||||||
error!("{}: {}", input, e);
|
Ok(_) => OptimizationResult::Ok,
|
||||||
false
|
Err(e) => {
|
||||||
|
error!("{input}: {e}");
|
||||||
|
OptimizationResult::Failed
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
})
|
||||||
});
|
.min()
|
||||||
|
.unwrap_or(OptimizationResult::Skipped);
|
||||||
|
|
||||||
if success.count() == 0 {
|
match summary {
|
||||||
exit(1);
|
OptimizationResult::Ok => ExitCode::SUCCESS,
|
||||||
|
OptimizationResult::Failed => ExitCode::FAILURE,
|
||||||
|
OptimizationResult::Skipped => ExitCode::from(3),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Eq, PartialEq, Ord, PartialOrd)]
|
||||||
|
enum OptimizationResult {
|
||||||
|
Ok,
|
||||||
|
Failed,
|
||||||
|
Skipped,
|
||||||
|
}
|
||||||
|
|
||||||
fn collect_files(
|
fn collect_files(
|
||||||
files: Vec<PathBuf>,
|
files: Vec<PathBuf>,
|
||||||
out_dir: &Option<PathBuf>,
|
out_dir: &Option<PathBuf>,
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue