Skip to content

Commit b64df32

Browse files
committed
chore: add --stdin-paths and -stdin-paths0
1 parent 66d82e5 commit b64df32

File tree

15 files changed

+138
-3
lines changed

15 files changed

+138
-3
lines changed

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ pub(crate) struct Args {
5050
#[arg(default_value = ".")]
5151
pub(crate) path: Vec<std::path::PathBuf>,
5252

53+
/// Read the list of newline separated paths from stdin
54+
#[arg(long, conflicts_with = "stdin_paths0")]
55+
pub(crate) stdin_paths: bool,
56+
57+
/// Read the list of '\0' separated paths from stdin
58+
#[arg(long, conflicts_with = "stdin_paths")]
59+
pub(crate) stdin_paths0: bool,
60+
5361
/// Custom config file
5462
#[arg(short = 'c', long = "config")]
5563
pub(crate) custom_config: Option<std::path::PathBuf>,
@@ -107,6 +115,12 @@ pub(crate) struct Args {
107115
pub(crate) verbose: clap_verbosity_flag::Verbosity,
108116
}
109117

118+
impl Args {
119+
pub fn is_using_stdin_paths(&self) -> bool {
120+
self.stdin_paths || self.stdin_paths0
121+
}
122+
}
123+
110124
#[derive(Debug, Clone, clap::Args)]
111125
#[command(rename_all = "kebab-case")]
112126
pub(crate) struct FileArgs {

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

Lines changed: 56 additions & 2 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,26 @@ 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() {
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) {
160178
let cwd = if path == std::path::Path::new("-") {
179+
if args.is_using_stdin_paths() {
180+
proc_exit::sysexits::NO_INPUT.ok()?;
181+
}
161182
global_cwd.clone()
162183
} else if path.is_file() {
163184
let mut cwd = path
@@ -269,6 +290,39 @@ fn run_checks(args: &args::Args) -> proc_exit::ExitResult {
269290
}
270291
}
271292

293+
#[cfg(target_family = "unix")]
294+
fn unix_read_paths_from_stdin() -> Result<Vec<PathBuf>, proc_exit::Exit> {
295+
use std::ffi::OsString;
296+
use std::os::unix::ffi::OsStringExt;
297+
298+
let mut buf = Vec::new();
299+
io::stdin()
300+
.read_to_end(&mut buf)
301+
.with_code(proc_exit::sysexits::IO_ERR)?;
302+
303+
let (mut paths, rest) = buf.into_iter().fold(
304+
(vec![], vec![]),
305+
|(mut paths, mut cur_path): (Vec<PathBuf>, Vec<u8>), byte| {
306+
if byte == 0 {
307+
paths.push(PathBuf::from(OsString::from_vec(cur_path)));
308+
(paths, vec![])
309+
} else {
310+
cur_path.push(byte);
311+
(paths, cur_path)
312+
}
313+
},
314+
);
315+
if !rest.is_empty() {
316+
paths.push(PathBuf::from(OsString::from_vec(rest)));
317+
}
318+
Ok(paths)
319+
}
320+
321+
#[cfg(not(target_family = "unix"))]
322+
fn unix_read_paths_from_stdin() -> Result<Vec<PathBuf>, proc_exit::Exit> {
323+
proc_exit::sysexits::CONFIG_ERR.ok()?;
324+
}
325+
272326
fn init_logging(level: Option<log::Level>) {
273327
if let Some(level) = level {
274328
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)