Skip to content

Commit 4e82c55

Browse files
committed
chore: add --stdin-paths and -stdin-paths0
1 parent 7f65ff4 commit 4e82c55

File tree

15 files changed

+138
-5
lines changed

15 files changed

+138
-5
lines changed

crates/typos-cli/src/bin/typos-cli/args.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,17 @@ impl Default for Format {
4444
#[command(group = clap::ArgGroup::new("mode").multiple(false))]
4545
pub(crate) struct Args {
4646
/// Paths to check with `-` for stdin
47-
#[arg(default_value = ".")]
47+
#[arg(default_value = ".", group = "source")]
4848
pub(crate) path: Vec<std::path::PathBuf>,
4949

50+
/// Read the list of newline separated paths from stdin
51+
#[arg(long, group = "source")]
52+
pub(crate) stdin_paths: bool,
53+
54+
/// Read the list of '\0' separated paths from stdin
55+
#[arg(long, group = "source")]
56+
pub(crate) stdin_paths0: bool,
57+
5058
/// Custom config file
5159
#[arg(short = 'c', long = "config")]
5260
pub(crate) custom_config: Option<std::path::PathBuf>,
@@ -104,6 +112,12 @@ pub(crate) struct Args {
104112
pub(crate) verbose: clap_verbosity_flag::Verbosity,
105113
}
106114

115+
impl Args {
116+
pub fn is_using_stdin_paths(&self) -> bool {
117+
self.stdin_paths || self.stdin_paths0
118+
}
119+
}
120+
107121
#[derive(Debug, Clone, clap::Args)]
108122
#[command(rename_all = "kebab-case")]
109123
pub(crate) struct FileArgs {

crates/typos-cli/src/bin/typos-cli/main.rs

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
use std::io::Write;
1+
use std::{
2+
io::{self, Read, Write},
3+
path::PathBuf,
4+
};
25

36
use clap::Parser;
47

@@ -156,8 +159,24 @@ fn run_checks(args: &args::Args) -> proc_exit::ExitResult {
156159

157160
let mut typos_found = false;
158161
let mut errors_found = false;
159-
for path in args.path.iter() {
160-
let cwd = if path == std::path::Path::new("-") {
162+
163+
let stdin_paths = if args.stdin_paths {
164+
Some(
165+
io::stdin()
166+
.lines()
167+
.map(|res| res.map(PathBuf::from))
168+
.collect::<Result<_, _>>()
169+
.with_code(proc_exit::sysexits::IO_ERR)?,
170+
)
171+
} else if args.stdin_paths0 {
172+
Some(unix_read_paths_from_stdin()?)
173+
} else {
174+
None
175+
};
176+
177+
for path in stdin_paths.as_ref().unwrap_or(&args.path) {
178+
// Note paths are passed through stdin, `-` is treated like a normal path
179+
let cwd = if !args.is_using_stdin_paths() && path == std::path::Path::new("-") {
161180
global_cwd.clone()
162181
} else if path.is_file() {
163182
let mut cwd = path
@@ -269,6 +288,39 @@ fn run_checks(args: &args::Args) -> proc_exit::ExitResult {
269288
}
270289
}
271290

291+
#[cfg(target_family = "unix")]
292+
fn unix_read_paths_from_stdin() -> Result<Vec<PathBuf>, proc_exit::Exit> {
293+
use std::ffi::OsString;
294+
use std::os::unix::ffi::OsStringExt;
295+
296+
let mut buf = Vec::new();
297+
io::stdin()
298+
.read_to_end(&mut buf)
299+
.with_code(proc_exit::sysexits::IO_ERR)?;
300+
301+
let (mut paths, rest) = buf.into_iter().fold(
302+
(vec![], vec![]),
303+
|(mut paths, mut cur_path): (Vec<PathBuf>, Vec<u8>), byte| {
304+
if byte == 0 {
305+
paths.push(PathBuf::from(OsString::from_vec(cur_path)));
306+
(paths, vec![])
307+
} else {
308+
cur_path.push(byte);
309+
(paths, cur_path)
310+
}
311+
},
312+
);
313+
if !rest.is_empty() {
314+
paths.push(PathBuf::from(OsString::from_vec(rest)));
315+
}
316+
Ok(paths)
317+
}
318+
319+
#[cfg(not(target_family = "unix"))]
320+
fn unix_read_paths_from_stdin() -> Result<Vec<PathBuf>, proc_exit::Exit> {
321+
proc_exit::sysexits::CONFIG_ERR.with_message("Reading zero-separated paths is not supported on Windows. PRs are welcome.")
322+
}
323+
272324
fn init_logging(level: Option<log::Level>) {
273325
if let Some(level) = level {
274326
let mut builder = env_logger::Builder::new();

crates/typos-cli/tests/cli_tests.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#[test]
22
#[cfg(feature = "dict")]
33
fn cli_tests() {
4-
trycmd::TestCases::new().case("tests/cmd/*.toml");
4+
trycmd::TestCases::new()
5+
.case("tests/cmd/*.toml")
6+
.case("tests/cmd/*.trycmd");
57
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[files]
2+
extend-exclude = ["_typos.toml"]
3+
4+
[default.extend-identifiers]
5+
hello = "goodbye"
6+
7+
[type.fail]
8+
extend-glob = ["*.fail"]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
hello
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
hello
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
hello
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
hello
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
bin.name = "typos"
2+
args = "--stdin-paths"
3+
stdin = """
4+
b.fail
5+
d.fail
6+
"""
7+
stdout = """
8+
error: `hello` should be `goodbye`
9+
--> b.fail:1:1
10+
|
11+
1 | hello
12+
| ^^^^^
13+
|
14+
error: `hello` should be `goodbye`
15+
--> d.fail:1:1
16+
|
17+
1 | hello
18+
| ^^^^^
19+
|
20+
"""
21+
stderr = ""
22+
status.code = 2
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[files]
2+
extend-exclude = ["_typos.toml"]
3+
4+
[default.extend-identifiers]
5+
hello = "goodbye"
6+
7+
[type.fail]
8+
extend-glob = ["*.fail"]

0 commit comments

Comments
 (0)