Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

feat: Set exit code on typos being found #46

Merged
merged 1 commit into from
Jul 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/about.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
| Respect gitignore | Yes | Yes | ? | No | No |
| Checks filenames | Yes | No | ? | Yes | No |
| Status via exit code | Yes | No | Yes | Yes | Yes |
| API | Rust / [JSON Lines] | Rust | ? | Python | None |
| License | MIT or Apache | AGPL | MIT | GPLv2 | GPLv2 |

Expand Down
12 changes: 9 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ pub fn process_file(
ignore_hex: bool,
binary: bool,
report: report::Report,
) -> Result<(), failure::Error> {
) -> Result<bool, failure::Error> {
let mut typos_found = false;

if check_filenames {
for part in path.components().filter_map(|c| c.as_os_str().to_str()) {
for ident in tokens::Identifier::parse(part) {
Expand All @@ -37,6 +39,7 @@ pub fn process_file(
non_exhaustive: (),
};
report(msg.into());
typos_found = true;
}
for word in ident.split() {
if let Some(correction) = dictionary.correct_word(word) {
Expand All @@ -47,6 +50,7 @@ pub fn process_file(
non_exhaustive: (),
};
report(msg.into());
typos_found = true;
}
}
}
Expand All @@ -62,7 +66,7 @@ pub fn process_file(
non_exhaustive: (),
};
report(msg.into());
return Ok(());
return Ok(typos_found);
}

for (line_idx, line) in buffer.lines().enumerate() {
Expand All @@ -82,6 +86,7 @@ pub fn process_file(
correction,
non_exhaustive: (),
};
typos_found = true;
report(msg.into());
}
for word in ident.split() {
Expand All @@ -96,14 +101,15 @@ pub fn process_file(
correction,
non_exhaustive: (),
};
typos_found = true;
report(msg.into());
}
}
}
}
}

Ok(())
Ok(typos_found)
}

fn is_hex(ident: &str) -> bool {
Expand Down
19 changes: 13 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ pub fn get_logging(level: log::Level) -> env_logger::Builder {
builder
}

fn run() -> Result<(), failure::Error> {
fn run() -> Result<i32, failure::Error> {
let options = Options::from_args().infer();

let mut builder = get_logging(options.verbose.log_level());
Expand All @@ -275,25 +275,32 @@ fn run() -> Result<(), failure::Error> {
.git_ignore(options.ignore_vcs().unwrap_or(true))
.git_exclude(options.ignore_vcs().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() {
let entry = entry?;
if entry.file_type().map(|t| t.is_file()).unwrap_or(true) {
typos::process_file(
if typos::process_file(
entry.path(),
&dictionary,
check_filenames,
check_files,
ignore_hex,
binary,
options.format.report(),
)?;
)? {
typos_found = true;
}
}
}

Ok(())
if typos_found {
Ok(1)
} else {
Ok(0)
}
}

fn main() {
run().unwrap();
let code = run().unwrap();
std::process::exit(code);
}