mirror of
https://github.com/uutils/diffutils.git
synced 2026-06-10 15:48:59 -07:00
Match GNU diff's implementation for exit codes (fixes #17)
This commit is contained in:
committed by
Sylvestre Ledru
parent
3bc8668f78
commit
1241db4806
+24
-6
@@ -8,6 +8,7 @@ use std::env;
|
||||
|
||||
use std::fs;
|
||||
use std::io::{self, Write};
|
||||
use std::process::{exit, ExitCode};
|
||||
|
||||
mod context_diff;
|
||||
mod ed_diff;
|
||||
@@ -15,25 +16,35 @@ mod normal_diff;
|
||||
mod params;
|
||||
mod unified_diff;
|
||||
|
||||
fn main() -> Result<(), String> {
|
||||
// Exit codes are documented at
|
||||
// https://www.gnu.org/software/diffutils/manual/html_node/Invoking-diff.html.
|
||||
// An exit status of 0 means no differences were found,
|
||||
// 1 means some differences were found,
|
||||
// and 2 means trouble.
|
||||
fn main() -> ExitCode {
|
||||
let opts = env::args_os();
|
||||
let Params {
|
||||
from,
|
||||
to,
|
||||
context_count,
|
||||
format,
|
||||
} = parse_params(opts)?;
|
||||
} = parse_params(opts).unwrap_or_else(|error| {
|
||||
eprintln!("{error}");
|
||||
exit(2);
|
||||
});
|
||||
// read files
|
||||
let from_content = match fs::read(&from) {
|
||||
Ok(from_content) => from_content,
|
||||
Err(e) => {
|
||||
return Err(format!("Failed to read from-file: {e}"));
|
||||
eprintln!("Failed to read from-file: {e}");
|
||||
return ExitCode::from(2);
|
||||
}
|
||||
};
|
||||
let to_content = match fs::read(&to) {
|
||||
Ok(to_content) => to_content,
|
||||
Err(e) => {
|
||||
return Err(format!("Failed to read to-file: {e}"));
|
||||
eprintln!("Failed to read to-file: {e}");
|
||||
return ExitCode::from(2);
|
||||
}
|
||||
};
|
||||
// run diff
|
||||
@@ -53,8 +64,15 @@ fn main() -> Result<(), String> {
|
||||
&to.to_string_lossy(),
|
||||
context_count,
|
||||
),
|
||||
Format::Ed => ed_diff::diff(&from_content, &to_content)?,
|
||||
Format::Ed => ed_diff::diff(&from_content, &to_content).unwrap_or_else(|error| {
|
||||
eprintln!("{error}");
|
||||
exit(2);
|
||||
}),
|
||||
};
|
||||
io::stdout().write_all(&result).unwrap();
|
||||
Ok(())
|
||||
if result.is_empty() {
|
||||
ExitCode::SUCCESS
|
||||
} else {
|
||||
ExitCode::from(1)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user