Skip to content

Commit 9ff7361

Browse files
committed
fix: deny invalid rtk read flags
1 parent 39bfe1a commit 9ff7361

3 files changed

Lines changed: 52 additions & 12 deletions

File tree

docs/DEVELOPMENT.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ Rewrite order:
8585
## What It Rewrites
8686

8787
- Already preferred RTK commands are left alone, such as `rtk git status --short`.
88+
Invalid `rtk read` forms with invented flags such as `--line`, `--lines`, or
89+
`--range` are denied with `rtk read --help` so agents do not confuse RTK parse
90+
fallback errors with a missing `rtk read` command.
8891
- Mutating PowerShell commands are left alone, including `Remove-Item`,
8992
`Set-Content`, `Add-Content`, `New-Item`, `Move-Item`, and `Copy-Item`.
9093
- Windows/PowerShell and Unix shell reads/searches are handled locally first.

src/rewrite.rs

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,46 @@ struct Token {
99

1010
pub fn suggest(command: &str) -> Option<String> {
1111
let command = command.trim();
12-
if command.is_empty()
13-
|| starts_with_rtk(command) && preferred_rtk_command(command)
14-
|| is_preferred_pwsh_wrapper(command)
15-
|| is_preferred_bash_wrapper(command)
12+
if command.is_empty() {
13+
return None;
14+
}
15+
16+
invalid_rtk_read_redirect(command).or_else(|| {
17+
if starts_with_rtk(command) && preferred_rtk_command(command)
18+
|| is_preferred_pwsh_wrapper(command)
19+
|| is_preferred_bash_wrapper(command)
20+
{
21+
return None;
22+
}
23+
24+
direct_powershell_redirect(command)
25+
.or_else(|| powershell_redirect(command))
26+
.or_else(|| env_redirect(command))
27+
.or_else(|| bash_redirect(command))
28+
.or_else(|| posix_redirect(command))
29+
.or_else(|| rg_redirect(command))
30+
.or_else(|| safe_external_rtk_rewrite(command))
31+
.or_else(|| local_rtk_miss_fallback(command))
32+
})
33+
}
34+
35+
fn invalid_rtk_read_redirect(command: &str) -> Option<String> {
36+
let tokens = tokenize(command);
37+
if command_name(&tokens.first()?.text) != "rtk" || command_name(&tokens.get(1)?.text) != "read"
1638
{
1739
return None;
1840
}
1941

20-
direct_powershell_redirect(command)
21-
.or_else(|| powershell_redirect(command))
22-
.or_else(|| env_redirect(command))
23-
.or_else(|| bash_redirect(command))
24-
.or_else(|| posix_redirect(command))
25-
.or_else(|| rg_redirect(command))
26-
.or_else(|| safe_external_rtk_rewrite(command))
27-
.or_else(|| local_rtk_miss_fallback(command))
42+
tokens
43+
.iter()
44+
.skip(2)
45+
.any(|token| {
46+
matches!(token.text.as_str(), "--line" | "--lines" | "--range")
47+
|| token.text.starts_with("--line=")
48+
|| token.text.starts_with("--lines=")
49+
|| token.text.starts_with("--range=")
50+
})
51+
.then(|| "rtk read --help".to_string())
2852
}
2953

3054
fn preferred_rtk_command(command: &str) -> bool {

tests/pretool.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,11 +205,24 @@ fn already_good_commands_are_noops() {
205205
assert_no_output("rtk git status --short");
206206
assert_no_output(r#"rtk grep -n "foo" src"#);
207207
assert_no_output("rtk find src");
208+
assert_no_output("rtk read src/main.rs --max-lines 120");
209+
assert_no_output("rtk read -n src/main.rs --tail-lines 40");
208210
assert_no_output(
209211
r#"pwsh -NoProfile -Command '$env:PATH="$env:APPDATA\luarocks\bin;$env:PATH"; rtk busted spec'"#,
210212
);
211213
}
212214

215+
#[test]
216+
fn invalid_rtk_read_flags_suggest_help() {
217+
for command in [
218+
"rtk read suwayomi/client/source_manga.lua --line 1 --lines 650",
219+
"rtk read suwayomi/ui.lua --line 1-120",
220+
"rtk read suwayomi/ui.lua --range 130:310",
221+
] {
222+
assert_deny(command, "rtk read --help");
223+
}
224+
}
225+
213226
#[test]
214227
fn powershell_mutations_are_noops() {
215228
for verb in [

0 commit comments

Comments
 (0)