Skip to content

Commit

Permalink
[feat] invoke as cargo subcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
DevinR528 committed Dec 4, 2019
1 parent 5863335 commit 1b44537
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 31 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cargo-sort-ck"
version = "1.0.1"
version = "1.1.0"
authors = ["Devin R <[email protected]>"]
license = "MIT/Apache-2.0"
description = "Check if tables and items in a .toml file are lexically sorted"
Expand Down
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@ A tool to check that your Cargo.toml dependencies are sorted alphabetically. Pro

## Use
There are three modes cargo-sort-ck can be used in:
* **default** - no flags set cargo-sort-ck will pass (exit 0) if .toml is sorted or fail if not (exit 1).
* **-p or --print** - will print the *__sorted toml__* file to stdout.
* **-w or --write** - will rewrite the toml file, I would like to eventually add some kind of check like cargo fix to warn if file is uncommitted/unsaved?.
* **default**
- no flags set cargo-sort-ck will pass (exit 0) if .toml is sorted or fail if not (exit 1).
* **-p or --print**
- will print the *__sorted toml__* file to stdout.
* **-w or --write**
- will rewrite the toml file, I would like to eventually add some kind of check like cargo fix to warn if file is uncommitted/unsaved?.

[toml]: https://github.com/toml-lang/toml
included in sort check is:
Expand Down Expand Up @@ -50,7 +53,10 @@ FLAGS:
ARGS:
<CWD>... Sets cwd, must contain Cargo.toml
```

Thanks to [dspicher](https://github.com/dspicher) for [issue #4](https://github.com/DevinR528/cargo-sort-ck/issues/4) you can now invoke cargo sort check as a cargo subcommand
```bash
cargo sort-ck [FLAGS] [path]
```
Wildcard expansion is supported so you can do this
```bash
cargo-sort-ck [FLAGS] [path/to/*/Cargo.toml | path/to/*]
Expand Down
55 changes: 30 additions & 25 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ fn check_toml(path: &str, matches: &clap::ArgMatches) -> bool {
}
}

fn main() -> std::io::Result<()> {
fn main() {
let matches = App::new("Cargo Sort Check")
.author("Devin R <[email protected]>")
.about("Ensure Cargo.toml dependency tables are sorted.")
Expand Down Expand Up @@ -152,34 +152,39 @@ fn main() -> std::io::Result<()> {
)
.get_matches();

let cwd = env::current_dir().unwrap_or_else(|e| {
let msg = format!("No file found at: {}", e);
write_err(&msg).unwrap();
std::process::exit(1);
});
// either default cwd or from user
let path = matches.values_of("cwd").map_or(cwd, |s| {
let dirs: Vec<&str> = s.collect();
if dirs.len() == 1 {
PathBuf::from(dirs[0])
let cwd = env::current_dir()
.unwrap_or_else(|e| {
let msg = format!("no current directory found: {}", e);
write_err(&msg).unwrap();
std::process::exit(1);
});
let dir = cwd.to_str()
.unwrap_or_else(|| {
let msg = format!("could not represent path as string");
write_err(&msg).unwrap();
std::process::exit(1);
});

// remove "sort-ck" when invoked `cargo sort-ck` sort-ck is the first arg
// https://github.com/rust-lang/cargo/issues/7653
let filtered_matches = matches.values_of("cwd").map_or(vec![dir], |s| {
let args = s.filter(|it| *it != "sort-ck").collect::<Vec<&str>>();
if args.is_empty() {
vec![dir]
} else {
let mut flag = true;
dirs.iter()
.map(|path| check_toml(path, &matches))
.for_each(|sorted| {
if !sorted {
flag = false;
}
});
if flag {
std::process::exit(0)
} else {
std::process::exit(1)
}
args
}
});

if check_toml(path.to_str().unwrap(), &matches) {
let mut flag = true;
filtered_matches.iter()
.map(|path| check_toml(path, &matches))
.for_each(|sorted| {
if !sorted {
flag = false;
}
});
if flag {
std::process::exit(0)
} else {
std::process::exit(1)
Expand Down

0 comments on commit 1b44537

Please sign in to comment.