Skip to content

Commit 85744b5

Browse files
fix(cli): exit diff check appropriately (#87)
1 parent 291933d commit 85744b5

File tree

3 files changed

+17
-11
lines changed

3 files changed

+17
-11
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -183,11 +183,11 @@ Use `--diff-command <command>` to delegate diff generation to an external progra
183183
Use `--mode fix` (or `--fix`) to rewrite the file in place.
184184

185185
The command exits with these codes:
186-
1. `0` — success
187-
2. `1` — syntax errors in input
188-
3. `2` — usage errors: invoked incorrectly
189-
4. `3` — unexpected runtime errors
190-
5. `4` — check mode failed (reformat is needed)
186+
- `0` — success
187+
- `1` — syntax errors in input
188+
- `2` — usage errors: invoked incorrectly
189+
- `3` — unexpected runtime errors
190+
- `4` — check mode failed (reformat is needed)
191191

192192
```shell
193193
$ keepsorted --check Cargo.toml

docs/architecture.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ This document explains how the main pieces of the `keepsorted` crate fit togethe
77
`keepsorted` was inspired by [Buildifier](https://github.com/bazelbuild/buildtools/tree/master/buildifier), which sorts items in Bazel `BUILD` files. The command-line flags and exit codes follow a similar design so that tooling can integrate either tool with minimal changes.
88

99
The CLI returns these codes:
10-
1. `0` — success
11-
2. `1` — syntax errors in input
12-
3. `2` — incorrect command usage
13-
4. `3` — unexpected runtime failures
14-
5. `4` — check mode detected unsorted files
10+
- `0` — success
11+
- `1` — syntax errors in input
12+
- `2` — incorrect command usage
13+
- `3` — unexpected runtime failures
14+
- `4` — check mode detected unsorted files
1515

1616
## Sorting Behaviour
1717

src/main.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ use std::process::{self, Command};
88
///
99
/// clap also exits with this code on usage errors such as conflicting flags.
1010
const EXIT_USAGE_ERROR: i32 = 2;
11+
/// Exit code used for syntax errors in the input.
12+
const EXIT_SYNTAX_ERROR: i32 = 1;
1113
/// Exit code used for I/O problems or internal bugs.
1214
const EXIT_RUNTIME_ERROR: i32 = 3;
1315
/// Exit code used when `--mode check` or `--mode diff` detects unsorted files.
@@ -270,7 +272,11 @@ fn handle_file(path: &Path, features: &[Feature], mode: Mode, diff_command: Opti
270272
Ok(out) => {
271273
print!("{}", String::from_utf8_lossy(&out.stdout));
272274
if !out.status.success() {
273-
process::exit(out.status.code().unwrap_or(EXIT_RUNTIME_ERROR));
275+
let code = out.status.code().unwrap_or(EXIT_RUNTIME_ERROR);
276+
if code == 1 {
277+
process::exit(EXIT_SYNTAX_ERROR);
278+
}
279+
process::exit(code);
274280
}
275281
}
276282
Err(e) => {

0 commit comments

Comments
 (0)