mirror of
https://github.com/crate-ci/typos.git
synced 2024-11-22 09:01:04 -05:00
Merge pull request #708 from dpc/stdin-paths
feat: Add `--file-list <PATH>` to choose files to check
This commit is contained in:
commit
4448cdacf1
9 changed files with 73 additions and 7 deletions
|
@ -39,9 +39,13 @@ impl Format {
|
|||
#[command(group = clap::ArgGroup::new("mode").multiple(false))]
|
||||
pub(crate) struct Args {
|
||||
/// Paths to check with `-` for stdin
|
||||
#[arg(default_value = ".")]
|
||||
#[arg(default_value = ".", group = "source")]
|
||||
pub(crate) path: Vec<std::path::PathBuf>,
|
||||
|
||||
/// Read the list of newline separated paths from file or stdin (if `-`)
|
||||
#[arg(long, group = "source")]
|
||||
pub(crate) file_list: Option<std::path::PathBuf>,
|
||||
|
||||
/// The approximate number of threads to use.
|
||||
#[arg(short = 'j', long = "threads", default_value = "0")]
|
||||
pub(crate) threads: usize,
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
use std::io::Write;
|
||||
use std::io::{BufRead as _, BufReader, Write as _};
|
||||
use std::path::PathBuf;
|
||||
|
||||
use clap::Parser;
|
||||
|
||||
|
@ -167,8 +168,34 @@ fn run_checks(args: &args::Args) -> proc_exit::ExitResult {
|
|||
|
||||
let mut typos_found = false;
|
||||
let mut errors_found = false;
|
||||
for path in args.path.iter() {
|
||||
|
||||
let file_list = match args.file_list.as_deref() {
|
||||
Some(dash) if dash == PathBuf::from("-") => Some(
|
||||
std::io::stdin()
|
||||
.lines()
|
||||
.map(|res| res.map(PathBuf::from))
|
||||
.collect::<Result<_, _>>()
|
||||
.with_code(proc_exit::sysexits::IO_ERR)?,
|
||||
),
|
||||
Some(path) => Some(
|
||||
BufReader::new(std::fs::File::open(path).with_code(proc_exit::sysexits::IO_ERR)?)
|
||||
.lines()
|
||||
.map(|res| res.map(PathBuf::from))
|
||||
.collect::<Result<_, _>>()
|
||||
.with_code(proc_exit::sysexits::IO_ERR)?,
|
||||
),
|
||||
None => None,
|
||||
};
|
||||
|
||||
// Note: file_list and args.path are mutually exclusive, enforced by clap
|
||||
for path in file_list.as_ref().unwrap_or(&args.path) {
|
||||
// Note paths are passed through stdin, `-` is treated like a normal path
|
||||
let cwd = if path == std::path::Path::new("-") {
|
||||
if args.file_list.is_some() {
|
||||
return Err(proc_exit::sysexits::USAGE_ERR.with_message(
|
||||
"Can't use `-` (stdin) while using `--file_list` provided paths",
|
||||
));
|
||||
};
|
||||
global_cwd.clone()
|
||||
} else if path.is_file() {
|
||||
let mut cwd = path
|
||||
|
|
|
@ -0,0 +1,8 @@
|
|||
[files]
|
||||
extend-exclude = ["_typos.toml"]
|
||||
|
||||
[default.extend-identifiers]
|
||||
hello = "goodbye"
|
||||
|
||||
[type.fail]
|
||||
extend-glob = ["*.fail"]
|
1
crates/typos-cli/tests/cmd/file-list-stdin.in/a.fail
Normal file
1
crates/typos-cli/tests/cmd/file-list-stdin.in/a.fail
Normal file
|
@ -0,0 +1 @@
|
|||
hello
|
1
crates/typos-cli/tests/cmd/file-list-stdin.in/b.fail
Normal file
1
crates/typos-cli/tests/cmd/file-list-stdin.in/b.fail
Normal file
|
@ -0,0 +1 @@
|
|||
hello
|
1
crates/typos-cli/tests/cmd/file-list-stdin.in/c.fail
Normal file
1
crates/typos-cli/tests/cmd/file-list-stdin.in/c.fail
Normal file
|
@ -0,0 +1 @@
|
|||
hello
|
1
crates/typos-cli/tests/cmd/file-list-stdin.in/d.fail
Normal file
1
crates/typos-cli/tests/cmd/file-list-stdin.in/d.fail
Normal file
|
@ -0,0 +1 @@
|
|||
hello
|
22
crates/typos-cli/tests/cmd/file-list-stdin.toml
Normal file
22
crates/typos-cli/tests/cmd/file-list-stdin.toml
Normal file
|
@ -0,0 +1,22 @@
|
|||
bin.name = "typos"
|
||||
args = "--file-list -"
|
||||
stdin = """
|
||||
b.fail
|
||||
d.fail
|
||||
"""
|
||||
stdout = """
|
||||
error: `hello` should be `goodbye`
|
||||
--> b.fail:1:1
|
||||
|
|
||||
1 | hello
|
||||
| ^^^^^
|
||||
|
|
||||
error: `hello` should be `goodbye`
|
||||
--> d.fail:1:1
|
||||
|
|
||||
1 | hello
|
||||
| ^^^^^
|
||||
|
|
||||
"""
|
||||
stderr = ""
|
||||
status.code = 2
|
|
@ -9,10 +9,11 @@ Arguments:
|
|||
[PATH]... Paths to check with `-` for stdin [default: .]
|
||||
|
||||
Options:
|
||||
-j, --threads <THREADS> The approximate number of threads to use [default: 0]
|
||||
--force-exclude Respect excluded files even for paths passed explicitly
|
||||
-h, --help Print help
|
||||
-V, --version Print version
|
||||
--file-list <FILE_LIST> Read the list of newline separated paths from file or stdin (if `-`)
|
||||
-j, --threads <THREADS> The approximate number of threads to use [default: 0]
|
||||
--force-exclude Respect excluded files even for paths passed explicitly
|
||||
-h, --help Print help
|
||||
-V, --version Print version
|
||||
|
||||
Config:
|
||||
-c, --config <CUSTOM_CONFIG> Custom config file
|
||||
|
|
Loading…
Reference in a new issue