Merge pull request #46 from epage/exit

feat: Set exit code on typos being found
This commit is contained in:
Ed Page 2019-07-23 10:37:33 -06:00 committed by GitHub
commit e69143dd23
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 9 deletions

View file

@ -47,6 +47,7 @@ Whitelist: A confidence rating is given for how close a word is to one in the wh
| Ignores hidden | Yes | Yes | ? | Yes | No | | Ignores hidden | Yes | Yes | ? | Yes | No |
| Respect gitignore | Yes | Yes | ? | No | No | | Respect gitignore | Yes | Yes | ? | No | No |
| Checks filenames | Yes | No | ? | Yes | No | | Checks filenames | Yes | No | ? | Yes | No |
| Status via exit code | Yes | No | Yes | Yes | Yes |
| API | Rust / [JSON Lines] | Rust | ? | Python | None | | API | Rust / [JSON Lines] | Rust | ? | Python | None |
| License | MIT or Apache | AGPL | MIT | GPLv2 | GPLv2 | | License | MIT or Apache | AGPL | MIT | GPLv2 | GPLv2 |

View file

@ -22,7 +22,9 @@ pub fn process_file(
ignore_hex: bool, ignore_hex: bool,
binary: bool, binary: bool,
report: report::Report, report: report::Report,
) -> Result<(), failure::Error> { ) -> Result<bool, failure::Error> {
let mut typos_found = false;
if check_filenames { if check_filenames {
for part in path.components().filter_map(|c| c.as_os_str().to_str()) { for part in path.components().filter_map(|c| c.as_os_str().to_str()) {
for ident in tokens::Identifier::parse(part) { for ident in tokens::Identifier::parse(part) {
@ -37,6 +39,7 @@ pub fn process_file(
non_exhaustive: (), non_exhaustive: (),
}; };
report(msg.into()); report(msg.into());
typos_found = true;
} }
for word in ident.split() { for word in ident.split() {
if let Some(correction) = dictionary.correct_word(word) { if let Some(correction) = dictionary.correct_word(word) {
@ -47,6 +50,7 @@ pub fn process_file(
non_exhaustive: (), non_exhaustive: (),
}; };
report(msg.into()); report(msg.into());
typos_found = true;
} }
} }
} }
@ -62,7 +66,7 @@ pub fn process_file(
non_exhaustive: (), non_exhaustive: (),
}; };
report(msg.into()); report(msg.into());
return Ok(()); return Ok(typos_found);
} }
for (line_idx, line) in buffer.lines().enumerate() { for (line_idx, line) in buffer.lines().enumerate() {
@ -82,6 +86,7 @@ pub fn process_file(
correction, correction,
non_exhaustive: (), non_exhaustive: (),
}; };
typos_found = true;
report(msg.into()); report(msg.into());
} }
for word in ident.split() { for word in ident.split() {
@ -96,6 +101,7 @@ pub fn process_file(
correction, correction,
non_exhaustive: (), non_exhaustive: (),
}; };
typos_found = true;
report(msg.into()); report(msg.into());
} }
} }
@ -103,7 +109,7 @@ pub fn process_file(
} }
} }
Ok(()) Ok(typos_found)
} }
fn is_hex(ident: &str) -> bool { fn is_hex(ident: &str) -> bool {

View file

@ -249,7 +249,7 @@ pub fn get_logging(level: log::Level) -> env_logger::Builder {
builder builder
} }
fn run() -> Result<(), failure::Error> { fn run() -> Result<i32, failure::Error> {
let options = Options::from_args().infer(); let options = Options::from_args().infer();
let mut builder = get_logging(options.verbose.log_level()); let mut builder = get_logging(options.verbose.log_level());
@ -275,11 +275,11 @@ fn run() -> Result<(), failure::Error> {
.git_ignore(options.ignore_vcs().unwrap_or(true)) .git_ignore(options.ignore_vcs().unwrap_or(true))
.git_exclude(options.ignore_vcs().unwrap_or(true)) .git_exclude(options.ignore_vcs().unwrap_or(true))
.parents(options.ignore_parent().unwrap_or(true)); .parents(options.ignore_parent().unwrap_or(true));
// TODO Add build_parallel for options.threads != 1 let mut typos_found = false;
for entry in walk.build() { for entry in walk.build() {
let entry = entry?; let entry = entry?;
if entry.file_type().map(|t| t.is_file()).unwrap_or(true) { if entry.file_type().map(|t| t.is_file()).unwrap_or(true) {
typos::process_file( if typos::process_file(
entry.path(), entry.path(),
&dictionary, &dictionary,
check_filenames, check_filenames,
@ -287,13 +287,20 @@ fn run() -> Result<(), failure::Error> {
ignore_hex, ignore_hex,
binary, binary,
options.format.report(), options.format.report(),
)?; )? {
typos_found = true;
}
} }
} }
Ok(()) if typos_found {
Ok(1)
} else {
Ok(0)
}
} }
fn main() { fn main() {
run().unwrap(); let code = run().unwrap();
std::process::exit(code);
} }