Compare commits
6
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2aff2fa773 | ||
|
|
d14e63911c | ||
|
|
18f4401a93 | ||
|
|
3f37f5a78f | ||
|
|
b2c2914b90 | ||
|
|
91289d761b |
No files matched your search
+1
-1
@@ -1488,4 +1488,4 @@ When implementing a new command, consider:
|
||||
|
||||
**Last Updated**: 2026-02-22
|
||||
**Architecture Version**: 2.2
|
||||
**rtk Version**: 0.28.0
|
||||
**rtk Version**: 0.28.2
|
||||
@@ -16,7 +16,7 @@ This is a fork with critical fixes for git argument parsing and modern JavaScrip
|
||||
|
||||
**Verify correct installation:**
|
||||
```bash
|
||||
rtk --version # Should show "rtk 0.28.0" (or newer)
|
||||
rtk --version # Should show "rtk 0.28.2" (or newer)
|
||||
rtk gain # Should show token savings stats (NOT "command not found")
|
||||
```
|
||||
|
||||
|
||||
Generated
+10
@@ -858,6 +858,7 @@ dependencies = [
|
||||
"toml",
|
||||
"ureq",
|
||||
"walkdir",
|
||||
"which",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -1301,6 +1302,15 @@ dependencies = [
|
||||
"rustls-pki-types",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "which"
|
||||
version = "8.0.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "81995fafaaaf6ae47a7d0cc83c67caf92aeb7e5331650ae6ff856f7c0c60c459"
|
||||
dependencies = [
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "winapi-util"
|
||||
version = "0.1.11"
|
||||
|
||||
@@ -30,6 +30,7 @@ tempfile = "3"
|
||||
sha2 = "0.10"
|
||||
ureq = "2"
|
||||
hostname = "0.4"
|
||||
which = "8"
|
||||
|
||||
[build-dependencies]
|
||||
toml = "0.8"
|
||||
|
||||
@@ -90,7 +90,7 @@ Download from [releases](https://github.com/rtk-ai/rtk/releases):
|
||||
### Verify Installation
|
||||
|
||||
```bash
|
||||
rtk --version # Should show "rtk 0.28.0"
|
||||
rtk --version # Should show "rtk 0.28.2"
|
||||
rtk gain # Should show token savings stats
|
||||
```
|
||||
|
||||
|
||||
+5
-13
@@ -4,6 +4,7 @@
|
||||
//! Claude Code API usage metrics. Handles subprocess execution, JSON parsing,
|
||||
//! and graceful degradation when ccusage is unavailable.
|
||||
|
||||
use crate::utils::{resolve_binary, tool_exists};
|
||||
use anyhow::{Context, Result};
|
||||
use serde::Deserialize;
|
||||
use std::process::Command;
|
||||
@@ -82,23 +83,14 @@ struct MonthlyEntry {
|
||||
|
||||
// ── Public API ──
|
||||
|
||||
/// Check if ccusage binary exists in PATH
|
||||
fn binary_exists() -> bool {
|
||||
Command::new("which")
|
||||
.arg("ccusage")
|
||||
.output()
|
||||
.map(|o| o.status.success())
|
||||
.unwrap_or(false)
|
||||
}
|
||||
|
||||
/// Build the ccusage command, falling back to npx if binary not in PATH
|
||||
fn build_command() -> Option<Command> {
|
||||
if binary_exists() {
|
||||
return Some(Command::new("ccusage"));
|
||||
if tool_exists("ccusage") {
|
||||
return Some(Command::new(resolve_binary("ccusage")));
|
||||
}
|
||||
|
||||
// Fallback: try npx
|
||||
let npx_check = Command::new("npx")
|
||||
let npx_check = Command::new(resolve_binary("npx"))
|
||||
.arg("ccusage")
|
||||
.arg("--help")
|
||||
.stdout(std::process::Stdio::null())
|
||||
@@ -106,7 +98,7 @@ fn build_command() -> Option<Command> {
|
||||
.status();
|
||||
|
||||
if npx_check.map(|s| s.success()).unwrap_or(false) {
|
||||
let mut cmd = Command::new("npx");
|
||||
let mut cmd = Command::new(resolve_binary("npx"));
|
||||
cmd.arg("ccusage");
|
||||
return Some(cmd);
|
||||
}
|
||||
|
||||
@@ -611,10 +611,10 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_classify_terraform_unsupported() {
|
||||
match classify_command("terraform plan -var-file=prod.tfvars") {
|
||||
fn test_classify_htop_unsupported() {
|
||||
match classify_command("htop -d 10") {
|
||||
Classification::Unsupported { base_command } => {
|
||||
assert_eq!(base_command, "terraform plan");
|
||||
assert_eq!(base_command, "htop");
|
||||
}
|
||||
other => panic!("expected Unsupported, got {:?}", other),
|
||||
}
|
||||
@@ -886,8 +886,8 @@ mod tests {
|
||||
#[test]
|
||||
fn test_rewrite_background_unsupported_right() {
|
||||
assert_eq!(
|
||||
rewrite_command("cargo test & terraform plan", &[]),
|
||||
Some("rtk cargo test & terraform plan".into())
|
||||
rewrite_command("cargo test & htop", &[]),
|
||||
Some("rtk cargo test & htop".into())
|
||||
);
|
||||
}
|
||||
|
||||
@@ -902,7 +902,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_rewrite_unsupported_returns_none() {
|
||||
assert_eq!(rewrite_command("terraform plan", &[]), None);
|
||||
assert_eq!(rewrite_command("htop", &[]), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -1695,18 +1695,15 @@ mod tests {
|
||||
fn test_rewrite_compound_mixed_supported_unsupported() {
|
||||
// unsupported segments stay raw
|
||||
assert_eq!(
|
||||
rewrite_command("cargo test && terraform plan", &[]),
|
||||
Some("rtk cargo test && terraform plan".into())
|
||||
rewrite_command("cargo test && htop", &[]),
|
||||
Some("rtk cargo test && htop".into())
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_rewrite_compound_all_unsupported_returns_none() {
|
||||
// No rewrite at all: returns None
|
||||
assert_eq!(
|
||||
rewrite_command("terraform plan && terraform apply", &[]),
|
||||
None
|
||||
);
|
||||
assert_eq!(rewrite_command("htop && top", &[]), None);
|
||||
}
|
||||
|
||||
// --- sudo / env prefix + rewrite ---
|
||||
|
||||
@@ -48,6 +48,39 @@ pub const PATTERNS: &[&str] = &[
|
||||
r"^aws\s+",
|
||||
// PostgreSQL
|
||||
r"^psql(\s|$)",
|
||||
// TOML-filtered commands
|
||||
r"^ansible-playbook\b",
|
||||
r"^brew\s+(install|upgrade)\b",
|
||||
r"^composer\s+(install|update|require)\b",
|
||||
r"^df(\s|$)",
|
||||
r"^dotnet\s+build\b",
|
||||
r"^du\b",
|
||||
r"^fail2ban-client\b",
|
||||
r"^gcloud\b",
|
||||
r"^hadolint\b",
|
||||
r"^helm\b",
|
||||
r"^iptables\b",
|
||||
r"^make\b",
|
||||
r"^markdownlint\b",
|
||||
r"^mix\s+(compile|format)(\s|$)",
|
||||
r"^mvn\s+(compile|package|clean|install)\b",
|
||||
r"^ping\b",
|
||||
r"^pio\s+run",
|
||||
r"^poetry\s+(install|lock|update)\b",
|
||||
r"^pre-commit\b",
|
||||
r"^ps(\s|$)",
|
||||
r"^quarto\s+render",
|
||||
r"^rsync\b",
|
||||
r"^shellcheck\b",
|
||||
r"^shopify\s+theme\s+(push|pull)",
|
||||
r"^sops\b",
|
||||
r"^swift\s+build\b",
|
||||
r"^systemctl\s+status\b",
|
||||
r"^terraform\s+plan",
|
||||
r"^tofu\s+(fmt|init|plan|validate)(\s|$)",
|
||||
r"^trunk\s+build",
|
||||
r"^uv\s+(sync|pip\s+install)\b",
|
||||
r"^yamllint\b",
|
||||
];
|
||||
|
||||
pub const RULES: &[RtkRule] = &[
|
||||
@@ -317,6 +350,263 @@ pub const RULES: &[RtkRule] = &[
|
||||
subcmd_savings: &[],
|
||||
subcmd_status: &[],
|
||||
},
|
||||
// TOML-filtered commands
|
||||
RtkRule {
|
||||
rtk_cmd: "rtk proxy ansible-playbook",
|
||||
rewrite_prefixes: &["ansible-playbook"],
|
||||
category: "Infra",
|
||||
savings_pct: 70.0,
|
||||
subcmd_savings: &[],
|
||||
subcmd_status: &[],
|
||||
},
|
||||
RtkRule {
|
||||
rtk_cmd: "rtk proxy brew",
|
||||
rewrite_prefixes: &["brew"],
|
||||
category: "PackageManager",
|
||||
savings_pct: 65.0,
|
||||
subcmd_savings: &[],
|
||||
subcmd_status: &[],
|
||||
},
|
||||
RtkRule {
|
||||
rtk_cmd: "rtk proxy composer",
|
||||
rewrite_prefixes: &["composer"],
|
||||
category: "PackageManager",
|
||||
savings_pct: 65.0,
|
||||
subcmd_savings: &[],
|
||||
subcmd_status: &[],
|
||||
},
|
||||
RtkRule {
|
||||
rtk_cmd: "rtk proxy df",
|
||||
rewrite_prefixes: &["df"],
|
||||
category: "System",
|
||||
savings_pct: 60.0,
|
||||
subcmd_savings: &[],
|
||||
subcmd_status: &[],
|
||||
},
|
||||
RtkRule {
|
||||
rtk_cmd: "rtk proxy dotnet",
|
||||
rewrite_prefixes: &["dotnet"],
|
||||
category: "Build",
|
||||
savings_pct: 70.0,
|
||||
subcmd_savings: &[],
|
||||
subcmd_status: &[],
|
||||
},
|
||||
RtkRule {
|
||||
rtk_cmd: "rtk proxy du",
|
||||
rewrite_prefixes: &["du"],
|
||||
category: "System",
|
||||
savings_pct: 60.0,
|
||||
subcmd_savings: &[],
|
||||
subcmd_status: &[],
|
||||
},
|
||||
RtkRule {
|
||||
rtk_cmd: "rtk proxy fail2ban-client",
|
||||
rewrite_prefixes: &["fail2ban-client"],
|
||||
category: "Infra",
|
||||
savings_pct: 60.0,
|
||||
subcmd_savings: &[],
|
||||
subcmd_status: &[],
|
||||
},
|
||||
RtkRule {
|
||||
rtk_cmd: "rtk proxy gcloud",
|
||||
rewrite_prefixes: &["gcloud"],
|
||||
category: "Infra",
|
||||
savings_pct: 65.0,
|
||||
subcmd_savings: &[],
|
||||
subcmd_status: &[],
|
||||
},
|
||||
RtkRule {
|
||||
rtk_cmd: "rtk proxy hadolint",
|
||||
rewrite_prefixes: &["hadolint"],
|
||||
category: "Build",
|
||||
savings_pct: 65.0,
|
||||
subcmd_savings: &[],
|
||||
subcmd_status: &[],
|
||||
},
|
||||
RtkRule {
|
||||
rtk_cmd: "rtk proxy helm",
|
||||
rewrite_prefixes: &["helm"],
|
||||
category: "Infra",
|
||||
savings_pct: 65.0,
|
||||
subcmd_savings: &[],
|
||||
subcmd_status: &[],
|
||||
},
|
||||
RtkRule {
|
||||
rtk_cmd: "rtk proxy iptables",
|
||||
rewrite_prefixes: &["iptables"],
|
||||
category: "Infra",
|
||||
savings_pct: 60.0,
|
||||
subcmd_savings: &[],
|
||||
subcmd_status: &[],
|
||||
},
|
||||
RtkRule {
|
||||
rtk_cmd: "rtk proxy make",
|
||||
rewrite_prefixes: &["make"],
|
||||
category: "Build",
|
||||
savings_pct: 65.0,
|
||||
subcmd_savings: &[],
|
||||
subcmd_status: &[],
|
||||
},
|
||||
RtkRule {
|
||||
rtk_cmd: "rtk proxy markdownlint",
|
||||
rewrite_prefixes: &["markdownlint"],
|
||||
category: "Build",
|
||||
savings_pct: 65.0,
|
||||
subcmd_savings: &[],
|
||||
subcmd_status: &[],
|
||||
},
|
||||
RtkRule {
|
||||
rtk_cmd: "rtk proxy mix",
|
||||
rewrite_prefixes: &["mix"],
|
||||
category: "Build",
|
||||
savings_pct: 65.0,
|
||||
subcmd_savings: &[],
|
||||
subcmd_status: &[],
|
||||
},
|
||||
RtkRule {
|
||||
rtk_cmd: "rtk proxy mvn",
|
||||
rewrite_prefixes: &["mvn"],
|
||||
category: "Build",
|
||||
savings_pct: 70.0,
|
||||
subcmd_savings: &[],
|
||||
subcmd_status: &[],
|
||||
},
|
||||
RtkRule {
|
||||
rtk_cmd: "rtk proxy ping",
|
||||
rewrite_prefixes: &["ping"],
|
||||
category: "Network",
|
||||
savings_pct: 60.0,
|
||||
subcmd_savings: &[],
|
||||
subcmd_status: &[],
|
||||
},
|
||||
RtkRule {
|
||||
rtk_cmd: "rtk proxy pio",
|
||||
rewrite_prefixes: &["pio"],
|
||||
category: "Build",
|
||||
savings_pct: 65.0,
|
||||
subcmd_savings: &[],
|
||||
subcmd_status: &[],
|
||||
},
|
||||
RtkRule {
|
||||
rtk_cmd: "rtk proxy poetry",
|
||||
rewrite_prefixes: &["poetry"],
|
||||
category: "Python",
|
||||
savings_pct: 65.0,
|
||||
subcmd_savings: &[],
|
||||
subcmd_status: &[],
|
||||
},
|
||||
RtkRule {
|
||||
rtk_cmd: "rtk proxy pre-commit",
|
||||
rewrite_prefixes: &["pre-commit"],
|
||||
category: "Build",
|
||||
savings_pct: 65.0,
|
||||
subcmd_savings: &[],
|
||||
subcmd_status: &[],
|
||||
},
|
||||
RtkRule {
|
||||
rtk_cmd: "rtk proxy ps",
|
||||
rewrite_prefixes: &["ps"],
|
||||
category: "System",
|
||||
savings_pct: 60.0,
|
||||
subcmd_savings: &[],
|
||||
subcmd_status: &[],
|
||||
},
|
||||
RtkRule {
|
||||
rtk_cmd: "rtk proxy quarto",
|
||||
rewrite_prefixes: &["quarto"],
|
||||
category: "Build",
|
||||
savings_pct: 65.0,
|
||||
subcmd_savings: &[],
|
||||
subcmd_status: &[],
|
||||
},
|
||||
RtkRule {
|
||||
rtk_cmd: "rtk proxy rsync",
|
||||
rewrite_prefixes: &["rsync"],
|
||||
category: "Network",
|
||||
savings_pct: 65.0,
|
||||
subcmd_savings: &[],
|
||||
subcmd_status: &[],
|
||||
},
|
||||
RtkRule {
|
||||
rtk_cmd: "rtk proxy shellcheck",
|
||||
rewrite_prefixes: &["shellcheck"],
|
||||
category: "Build",
|
||||
savings_pct: 65.0,
|
||||
subcmd_savings: &[],
|
||||
subcmd_status: &[],
|
||||
},
|
||||
RtkRule {
|
||||
rtk_cmd: "rtk proxy shopify",
|
||||
rewrite_prefixes: &["shopify"],
|
||||
category: "Build",
|
||||
savings_pct: 65.0,
|
||||
subcmd_savings: &[],
|
||||
subcmd_status: &[],
|
||||
},
|
||||
RtkRule {
|
||||
rtk_cmd: "rtk proxy sops",
|
||||
rewrite_prefixes: &["sops"],
|
||||
category: "Infra",
|
||||
savings_pct: 60.0,
|
||||
subcmd_savings: &[],
|
||||
subcmd_status: &[],
|
||||
},
|
||||
RtkRule {
|
||||
rtk_cmd: "rtk proxy swift",
|
||||
rewrite_prefixes: &["swift"],
|
||||
category: "Build",
|
||||
savings_pct: 65.0,
|
||||
subcmd_savings: &[],
|
||||
subcmd_status: &[],
|
||||
},
|
||||
RtkRule {
|
||||
rtk_cmd: "rtk proxy systemctl",
|
||||
rewrite_prefixes: &["systemctl"],
|
||||
category: "System",
|
||||
savings_pct: 65.0,
|
||||
subcmd_savings: &[],
|
||||
subcmd_status: &[],
|
||||
},
|
||||
RtkRule {
|
||||
rtk_cmd: "rtk proxy terraform",
|
||||
rewrite_prefixes: &["terraform"],
|
||||
category: "Infra",
|
||||
savings_pct: 70.0,
|
||||
subcmd_savings: &[],
|
||||
subcmd_status: &[],
|
||||
},
|
||||
RtkRule {
|
||||
rtk_cmd: "rtk proxy tofu",
|
||||
rewrite_prefixes: &["tofu"],
|
||||
category: "Infra",
|
||||
savings_pct: 70.0,
|
||||
subcmd_savings: &[],
|
||||
subcmd_status: &[],
|
||||
},
|
||||
RtkRule {
|
||||
rtk_cmd: "rtk proxy trunk",
|
||||
rewrite_prefixes: &["trunk"],
|
||||
category: "Build",
|
||||
savings_pct: 65.0,
|
||||
subcmd_savings: &[],
|
||||
subcmd_status: &[],
|
||||
},
|
||||
RtkRule {
|
||||
rtk_cmd: "rtk proxy uv",
|
||||
rewrite_prefixes: &["uv"],
|
||||
category: "Python",
|
||||
savings_pct: 65.0,
|
||||
subcmd_savings: &[],
|
||||
subcmd_status: &[],
|
||||
},
|
||||
RtkRule {
|
||||
rtk_cmd: "rtk proxy yamllint",
|
||||
rewrite_prefixes: &["yamllint"],
|
||||
category: "Build",
|
||||
savings_pct: 65.0,
|
||||
subcmd_savings: &[],
|
||||
subcmd_status: &[],
|
||||
},
|
||||
];
|
||||
|
||||
/// Commands to ignore (shell builtins, trivial, already rtk).
|
||||
|
||||
@@ -50,6 +50,7 @@ pub enum Language {
|
||||
Java,
|
||||
Ruby,
|
||||
Shell,
|
||||
Data,
|
||||
Unknown,
|
||||
}
|
||||
|
||||
@@ -66,6 +67,9 @@ impl Language {
|
||||
"java" => Language::Java,
|
||||
"rb" => Language::Ruby,
|
||||
"sh" | "bash" | "zsh" => Language::Shell,
|
||||
"json" | "jsonc" | "json5" | "yaml" | "yml" | "toml" | "xml" | "html" | "htm"
|
||||
| "css" | "scss" | "svg" | "md" | "markdown" | "txt" | "csv" | "tsv" | "env"
|
||||
| "ini" | "cfg" | "conf" | "lock" => Language::Data,
|
||||
_ => Language::Unknown,
|
||||
}
|
||||
}
|
||||
@@ -112,6 +116,13 @@ impl Language {
|
||||
doc_line: None,
|
||||
doc_block_start: None,
|
||||
},
|
||||
Language::Data => CommentPatterns {
|
||||
line: None,
|
||||
block_start: None,
|
||||
block_end: None,
|
||||
doc_line: None,
|
||||
doc_block_start: None,
|
||||
},
|
||||
Language::Unknown => CommentPatterns {
|
||||
line: Some("//"),
|
||||
block_start: Some("/*"),
|
||||
@@ -389,6 +400,52 @@ mod tests {
|
||||
assert_eq!(Language::from_extension("js"), Language::JavaScript);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_language_detection_data_formats() {
|
||||
assert_eq!(Language::from_extension("json"), Language::Data);
|
||||
assert_eq!(Language::from_extension("yaml"), Language::Data);
|
||||
assert_eq!(Language::from_extension("yml"), Language::Data);
|
||||
assert_eq!(Language::from_extension("toml"), Language::Data);
|
||||
assert_eq!(Language::from_extension("xml"), Language::Data);
|
||||
assert_eq!(Language::from_extension("md"), Language::Data);
|
||||
assert_eq!(Language::from_extension("csv"), Language::Data);
|
||||
assert_eq!(Language::from_extension("lock"), Language::Data);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_data_files_no_comment_stripping() {
|
||||
// Regression test for #464: package.json with `/*` in strings
|
||||
let json = r#"{
|
||||
"workspaces": {
|
||||
"packages": [
|
||||
"packages/*"
|
||||
]
|
||||
},
|
||||
"scripts": {
|
||||
"build": "bun run --workspaces build"
|
||||
},
|
||||
"lint-staged": {
|
||||
"**/package.json": [
|
||||
"sort-package-json"
|
||||
]
|
||||
}
|
||||
}"#;
|
||||
let filter = MinimalFilter;
|
||||
let result = filter.filter(json, &Language::Data);
|
||||
assert!(
|
||||
result.contains("scripts"),
|
||||
"scripts section must be preserved"
|
||||
);
|
||||
assert!(
|
||||
result.contains("packages/*"),
|
||||
"glob pattern must be preserved"
|
||||
);
|
||||
assert!(
|
||||
result.contains("**/package.json"),
|
||||
"glob pattern must be preserved"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_minimal_filter_removes_comments() {
|
||||
let code = r#"
|
||||
|
||||
+3
-3
@@ -1,7 +1,7 @@
|
||||
use crate::prettier_cmd;
|
||||
use crate::ruff_cmd;
|
||||
use crate::tracking;
|
||||
use crate::utils::package_manager_exec;
|
||||
use crate::utils::{package_manager_exec, resolve_binary};
|
||||
use anyhow::{Context, Result};
|
||||
use std::path::Path;
|
||||
use std::process::Command;
|
||||
@@ -72,9 +72,9 @@ pub fn run(args: &[String], verbose: u8) -> Result<()> {
|
||||
// Build command based on formatter
|
||||
let mut cmd = match formatter.as_str() {
|
||||
"prettier" => package_manager_exec("prettier"),
|
||||
"black" | "ruff" => Command::new(formatter.as_str()),
|
||||
"black" | "ruff" => Command::new(resolve_binary(formatter.as_str())),
|
||||
"biome" => package_manager_exec("biome"),
|
||||
_ => Command::new(formatter.as_str()),
|
||||
_ => Command::new(resolve_binary(formatter.as_str())),
|
||||
};
|
||||
|
||||
// Add formatter-specific flags
|
||||
|
||||
+51
-1
@@ -121,7 +121,16 @@ fn extract_identifier_and_extra_args(args: &[String]) -> Option<(String, Vec<Str
|
||||
}
|
||||
|
||||
// Known gh flags that take a value — skip these and their values
|
||||
let flags_with_value = ["-R", "--repo", "-q", "--jq", "-t", "--template"];
|
||||
let flags_with_value = [
|
||||
"-R",
|
||||
"--repo",
|
||||
"-q",
|
||||
"--jq",
|
||||
"-t",
|
||||
"--template",
|
||||
"--job",
|
||||
"--attempt",
|
||||
];
|
||||
let mut identifier = None;
|
||||
let mut extra = Vec::new();
|
||||
let mut skip_next = false;
|
||||
@@ -1418,6 +1427,47 @@ mod tests {
|
||||
assert!(!should_passthrough_run_view(&["--web".into()]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_extract_identifier_with_job_flag_after() {
|
||||
// gh run view 12345 --job 67890
|
||||
let args: Vec<String> = vec!["12345".into(), "--job".into(), "67890".into()];
|
||||
let (id, extra) = extract_identifier_and_extra_args(&args).unwrap();
|
||||
assert_eq!(id, "12345");
|
||||
assert_eq!(extra, vec!["--job", "67890"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_extract_identifier_with_job_flag_before() {
|
||||
// gh run view --job 67890 12345
|
||||
let args: Vec<String> = vec!["--job".into(), "67890".into(), "12345".into()];
|
||||
let (id, extra) = extract_identifier_and_extra_args(&args).unwrap();
|
||||
assert_eq!(id, "12345");
|
||||
assert_eq!(extra, vec!["--job", "67890"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_extract_identifier_with_job_and_log_failed() {
|
||||
// gh run view --log-failed --job 67890 12345
|
||||
let args: Vec<String> = vec![
|
||||
"--log-failed".into(),
|
||||
"--job".into(),
|
||||
"67890".into(),
|
||||
"12345".into(),
|
||||
];
|
||||
let (id, extra) = extract_identifier_and_extra_args(&args).unwrap();
|
||||
assert_eq!(id, "12345");
|
||||
assert_eq!(extra, vec!["--log-failed", "--job", "67890"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_extract_identifier_with_attempt_flag() {
|
||||
// gh run view 12345 --attempt 3
|
||||
let args: Vec<String> = vec!["12345".into(), "--attempt".into(), "3".into()];
|
||||
let (id, extra) = extract_identifier_and_extra_args(&args).unwrap();
|
||||
assert_eq!(id, "12345");
|
||||
assert_eq!(extra, vec!["--attempt", "3"]);
|
||||
}
|
||||
|
||||
// --- should_passthrough_pr_view tests ---
|
||||
|
||||
#[test]
|
||||
|
||||
+12
-5
@@ -352,9 +352,11 @@ fn run_log(
|
||||
arg.starts_with("--oneline") || arg.starts_with("--pretty") || arg.starts_with("--format")
|
||||
});
|
||||
|
||||
// Check if user provided limit flag
|
||||
// Check if user provided limit flag (-N, -n N, --max-count=N, --max-count N)
|
||||
let has_limit_flag = args.iter().any(|arg| {
|
||||
arg.starts_with('-') && arg.chars().nth(1).map_or(false, |c| c.is_ascii_digit())
|
||||
(arg.starts_with('-') && arg.chars().nth(1).map_or(false, |c| c.is_ascii_digit()))
|
||||
|| arg == "-n"
|
||||
|| arg.starts_with("--max-count")
|
||||
});
|
||||
|
||||
// Apply RTK defaults only if user didn't specify them
|
||||
@@ -362,17 +364,22 @@ fn run_log(
|
||||
cmd.args(["--pretty=format:%h %s (%ar) <%an>"]);
|
||||
}
|
||||
|
||||
let limit = if !has_limit_flag {
|
||||
// Only inject -10 limit when RTK is applying its own format.
|
||||
// When user provides --oneline/--pretty/--format, respect git's default (no limit).
|
||||
let limit = if !has_limit_flag && !has_format_flag {
|
||||
cmd.arg("-10");
|
||||
10
|
||||
} else {
|
||||
} else if has_limit_flag {
|
||||
// Extract limit from args if provided
|
||||
args.iter()
|
||||
.find(|arg| {
|
||||
arg.starts_with('-') && arg.chars().nth(1).map_or(false, |c| c.is_ascii_digit())
|
||||
})
|
||||
.and_then(|arg| arg[1..].parse::<usize>().ok())
|
||||
.unwrap_or(10)
|
||||
.unwrap_or(500)
|
||||
} else {
|
||||
// User format, no limit — use a high cap for filter_log_output
|
||||
500
|
||||
};
|
||||
|
||||
// Only add --no-merges if user didn't explicitly request merge commits
|
||||
|
||||
+2
-2
@@ -1,5 +1,5 @@
|
||||
use crate::tracking;
|
||||
use crate::utils::truncate;
|
||||
use crate::utils::{resolve_binary, truncate};
|
||||
use anyhow::{Context, Result};
|
||||
use serde::Deserialize;
|
||||
use std::collections::HashMap;
|
||||
@@ -34,7 +34,7 @@ struct GolangciOutput {
|
||||
pub fn run(args: &[String], verbose: u8) -> Result<()> {
|
||||
let timer = tracking::TimedExecution::start();
|
||||
|
||||
let mut cmd = Command::new("golangci-lint");
|
||||
let mut cmd = Command::new(resolve_binary("golangci-lint"));
|
||||
|
||||
// Force JSON output
|
||||
let has_format = args
|
||||
|
||||
+2
-2
@@ -1,7 +1,7 @@
|
||||
use crate::mypy_cmd;
|
||||
use crate::ruff_cmd;
|
||||
use crate::tracking;
|
||||
use crate::utils::{package_manager_exec, truncate};
|
||||
use crate::utils::{package_manager_exec, resolve_binary, truncate};
|
||||
use anyhow::{Context, Result};
|
||||
use regex::Regex;
|
||||
use serde::{Deserialize, Serialize};
|
||||
@@ -93,7 +93,7 @@ pub fn run(args: &[String], verbose: u8) -> Result<()> {
|
||||
// Python linters use Command::new() directly (they're on PATH via pip/pipx)
|
||||
// JS linters use package_manager_exec (npx/pnpm exec)
|
||||
let mut cmd = if is_python_linter(linter) {
|
||||
Command::new(linter)
|
||||
Command::new(resolve_binary(linter))
|
||||
} else {
|
||||
package_manager_exec(linter)
|
||||
};
|
||||
|
||||
@@ -121,6 +121,7 @@ fn lang_display_name(lang: &Language) -> &'static str {
|
||||
Language::Java => "Java",
|
||||
Language::Ruby => "Ruby",
|
||||
Language::Shell => "Shell",
|
||||
Language::Data => "Data",
|
||||
Language::Unknown => "Code",
|
||||
}
|
||||
}
|
||||
|
||||
+6
-5
@@ -63,6 +63,7 @@ use clap::error::ErrorKind;
|
||||
use clap::{Parser, Subcommand};
|
||||
use std::ffi::OsString;
|
||||
use std::path::{Path, PathBuf};
|
||||
use utils::resolve_binary;
|
||||
|
||||
#[derive(Parser)]
|
||||
#[command(
|
||||
@@ -998,7 +999,7 @@ fn run_fallback(parse_error: clap::Error) -> Result<()> {
|
||||
|
||||
if let Some(filter) = toml_match {
|
||||
// TOML match: capture stdout for filtering
|
||||
let result = std::process::Command::new(&args[0])
|
||||
let result = std::process::Command::new(resolve_binary(&args[0]))
|
||||
.args(&args[1..])
|
||||
.stdin(std::process::Stdio::inherit())
|
||||
.stdout(std::process::Stdio::piped()) // capture
|
||||
@@ -1043,7 +1044,7 @@ fn run_fallback(parse_error: clap::Error) -> Result<()> {
|
||||
}
|
||||
} else {
|
||||
// No TOML match: original passthrough behaviour (Stdio::inherit, streaming)
|
||||
let status = std::process::Command::new(&args[0])
|
||||
let status = std::process::Command::new(resolve_binary(&args[0]))
|
||||
.args(&args[1..])
|
||||
.stdin(std::process::Stdio::inherit())
|
||||
.stdout(std::process::Stdio::inherit())
|
||||
@@ -1734,7 +1735,7 @@ fn main() -> Result<()> {
|
||||
_ => {
|
||||
// Passthrough other prisma subcommands
|
||||
let timer = tracking::TimedExecution::start();
|
||||
let mut cmd = std::process::Command::new("npx");
|
||||
let mut cmd = std::process::Command::new(resolve_binary("npx"));
|
||||
for arg in &args {
|
||||
cmd.arg(arg);
|
||||
}
|
||||
@@ -1751,7 +1752,7 @@ fn main() -> Result<()> {
|
||||
}
|
||||
} else {
|
||||
let timer = tracking::TimedExecution::start();
|
||||
let status = std::process::Command::new("npx")
|
||||
let status = std::process::Command::new(resolve_binary("npx"))
|
||||
.arg("prisma")
|
||||
.status()
|
||||
.context("Failed to run npx prisma")?;
|
||||
@@ -1867,7 +1868,7 @@ fn main() -> Result<()> {
|
||||
eprintln!("Proxy mode: {} {}", cmd_name, cmd_args.join(" "));
|
||||
}
|
||||
|
||||
let mut child = Command::new(cmd_name.as_ref())
|
||||
let mut child = Command::new(resolve_binary(cmd_name.as_ref()))
|
||||
.args(&cmd_args)
|
||||
.stdout(Stdio::piped())
|
||||
.stderr(Stdio::piped())
|
||||
|
||||
+4
-15
@@ -1,5 +1,5 @@
|
||||
use crate::tracking;
|
||||
use crate::utils::{strip_ansi, truncate};
|
||||
use crate::utils::{resolve_binary, strip_ansi, tool_exists, truncate};
|
||||
use anyhow::{Context, Result};
|
||||
use regex::Regex;
|
||||
use std::collections::HashMap;
|
||||
@@ -8,10 +8,10 @@ use std::process::Command;
|
||||
pub fn run(args: &[String], verbose: u8) -> Result<()> {
|
||||
let timer = tracking::TimedExecution::start();
|
||||
|
||||
let mut cmd = if which_command("mypy").is_some() {
|
||||
Command::new("mypy")
|
||||
let mut cmd = if tool_exists("mypy") {
|
||||
Command::new(resolve_binary("mypy"))
|
||||
} else {
|
||||
let mut c = Command::new("python3");
|
||||
let mut c = Command::new(resolve_binary("python3"));
|
||||
c.arg("-m").arg("mypy");
|
||||
c
|
||||
};
|
||||
@@ -47,17 +47,6 @@ pub fn run(args: &[String], verbose: u8) -> Result<()> {
|
||||
std::process::exit(output.status.code().unwrap_or(1));
|
||||
}
|
||||
|
||||
fn which_command(cmd: &str) -> Option<String> {
|
||||
Command::new("which")
|
||||
.arg(cmd)
|
||||
.output()
|
||||
.ok()
|
||||
.filter(|o| o.status.success())
|
||||
.and_then(|o| String::from_utf8(o.stdout).ok())
|
||||
.map(|s| s.trim().to_string())
|
||||
.filter(|s| !s.is_empty())
|
||||
}
|
||||
|
||||
struct MypyError {
|
||||
file: String,
|
||||
line: usize,
|
||||
|
||||
+4
-8
@@ -1,5 +1,5 @@
|
||||
use crate::tracking;
|
||||
use crate::utils::{strip_ansi, truncate};
|
||||
use crate::utils::{resolve_binary, strip_ansi, tool_exists, truncate};
|
||||
use anyhow::{Context, Result};
|
||||
use regex::Regex;
|
||||
use std::process::Command;
|
||||
@@ -8,16 +8,12 @@ pub fn run(args: &[String], verbose: u8) -> Result<()> {
|
||||
let timer = tracking::TimedExecution::start();
|
||||
|
||||
// Try next directly first, fallback to npx if not found
|
||||
let next_exists = Command::new("which")
|
||||
.arg("next")
|
||||
.output()
|
||||
.map(|o| o.status.success())
|
||||
.unwrap_or(false);
|
||||
let next_exists = tool_exists("next");
|
||||
|
||||
let mut cmd = if next_exists {
|
||||
Command::new("next")
|
||||
Command::new(resolve_binary("next"))
|
||||
} else {
|
||||
let mut c = Command::new("npx");
|
||||
let mut c = Command::new(resolve_binary("npx"));
|
||||
c.arg("next");
|
||||
c
|
||||
};
|
||||
|
||||
+2
-1
@@ -1,11 +1,12 @@
|
||||
use crate::tracking;
|
||||
use crate::utils::resolve_binary;
|
||||
use anyhow::{Context, Result};
|
||||
use std::process::Command;
|
||||
|
||||
pub fn run(args: &[String], verbose: u8, skip_env: bool) -> Result<()> {
|
||||
let timer = tracking::TimedExecution::start();
|
||||
|
||||
let mut cmd = Command::new("npm");
|
||||
let mut cmd = Command::new(resolve_binary("npm"));
|
||||
cmd.arg("run");
|
||||
|
||||
// Strip leading "run" to avoid doubling (rtk npm run build → npm run build, not npm run run build)
|
||||
|
||||
+5
-16
@@ -1,4 +1,5 @@
|
||||
use crate::tracking;
|
||||
use crate::utils::{resolve_binary, tool_exists};
|
||||
use anyhow::{Context, Result};
|
||||
use serde::Deserialize;
|
||||
use std::process::Command;
|
||||
@@ -15,7 +16,7 @@ pub fn run(args: &[String], verbose: u8) -> Result<()> {
|
||||
let timer = tracking::TimedExecution::start();
|
||||
|
||||
// Auto-detect uv vs pip
|
||||
let use_uv = which_command("uv").is_some();
|
||||
let use_uv = tool_exists("uv");
|
||||
let base_cmd = if use_uv { "uv" } else { "pip" };
|
||||
|
||||
if verbose > 0 && use_uv {
|
||||
@@ -51,7 +52,7 @@ pub fn run(args: &[String], verbose: u8) -> Result<()> {
|
||||
}
|
||||
|
||||
fn run_list(base_cmd: &str, args: &[String], verbose: u8) -> Result<(String, String)> {
|
||||
let mut cmd = Command::new(base_cmd);
|
||||
let mut cmd = Command::new(resolve_binary(base_cmd));
|
||||
|
||||
if base_cmd == "uv" {
|
||||
cmd.arg("pip");
|
||||
@@ -86,7 +87,7 @@ fn run_list(base_cmd: &str, args: &[String], verbose: u8) -> Result<(String, Str
|
||||
}
|
||||
|
||||
fn run_outdated(base_cmd: &str, args: &[String], verbose: u8) -> Result<(String, String)> {
|
||||
let mut cmd = Command::new(base_cmd);
|
||||
let mut cmd = Command::new(resolve_binary(base_cmd));
|
||||
|
||||
if base_cmd == "uv" {
|
||||
cmd.arg("pip");
|
||||
@@ -121,7 +122,7 @@ fn run_outdated(base_cmd: &str, args: &[String], verbose: u8) -> Result<(String,
|
||||
}
|
||||
|
||||
fn run_passthrough(base_cmd: &str, args: &[String], verbose: u8) -> Result<(String, String)> {
|
||||
let mut cmd = Command::new(base_cmd);
|
||||
let mut cmd = Command::new(resolve_binary(base_cmd));
|
||||
|
||||
if base_cmd == "uv" {
|
||||
cmd.arg("pip");
|
||||
@@ -153,18 +154,6 @@ fn run_passthrough(base_cmd: &str, args: &[String], verbose: u8) -> Result<(Stri
|
||||
Ok((raw.clone(), raw))
|
||||
}
|
||||
|
||||
/// Check if a command exists in PATH
|
||||
fn which_command(cmd: &str) -> Option<String> {
|
||||
Command::new("which")
|
||||
.arg(cmd)
|
||||
.output()
|
||||
.ok()
|
||||
.filter(|o| o.status.success())
|
||||
.and_then(|o| String::from_utf8(o.stdout).ok())
|
||||
.map(|s| s.trim().to_string())
|
||||
.filter(|s| !s.is_empty())
|
||||
}
|
||||
|
||||
/// Filter pip list JSON output
|
||||
fn filter_pip_list(output: &str) -> String {
|
||||
let packages: Vec<Package> = match serde_json::from_str(output) {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use crate::tracking;
|
||||
use crate::utils::{detect_package_manager, strip_ansi};
|
||||
use crate::utils::{detect_package_manager, resolve_binary, strip_ansi};
|
||||
use anyhow::{Context, Result};
|
||||
use regex::Regex;
|
||||
use serde::Deserialize;
|
||||
@@ -246,17 +246,17 @@ pub fn run(args: &[String], verbose: u8) -> Result<()> {
|
||||
let pm = detect_package_manager();
|
||||
let mut cmd = match pm {
|
||||
"pnpm" => {
|
||||
let mut c = std::process::Command::new("pnpm");
|
||||
let mut c = std::process::Command::new(resolve_binary("pnpm"));
|
||||
c.arg("exec").arg("--").arg("playwright");
|
||||
c
|
||||
}
|
||||
"yarn" => {
|
||||
let mut c = std::process::Command::new("yarn");
|
||||
let mut c = std::process::Command::new(resolve_binary("yarn"));
|
||||
c.arg("exec").arg("--").arg("playwright");
|
||||
c
|
||||
}
|
||||
_ => {
|
||||
let mut c = std::process::Command::new("npx");
|
||||
let mut c = std::process::Command::new(resolve_binary("npx"));
|
||||
c.arg("--no-install").arg("--").arg("playwright");
|
||||
c
|
||||
}
|
||||
|
||||
+5
-4
@@ -1,4 +1,5 @@
|
||||
use crate::tracking;
|
||||
use crate::utils::resolve_binary;
|
||||
use anyhow::{Context, Result};
|
||||
use serde::Deserialize;
|
||||
use std::collections::HashMap;
|
||||
@@ -294,7 +295,7 @@ pub fn run(cmd: PnpmCommand, args: &[String], verbose: u8) -> Result<()> {
|
||||
fn run_list(depth: usize, args: &[String], verbose: u8) -> Result<()> {
|
||||
let timer = tracking::TimedExecution::start();
|
||||
|
||||
let mut cmd = Command::new("pnpm");
|
||||
let mut cmd = Command::new(resolve_binary("pnpm"));
|
||||
cmd.arg("list");
|
||||
cmd.arg(format!("--depth={}", depth));
|
||||
cmd.arg("--json");
|
||||
@@ -350,7 +351,7 @@ fn run_list(depth: usize, args: &[String], verbose: u8) -> Result<()> {
|
||||
fn run_outdated(args: &[String], verbose: u8) -> Result<()> {
|
||||
let timer = tracking::TimedExecution::start();
|
||||
|
||||
let mut cmd = Command::new("pnpm");
|
||||
let mut cmd = Command::new(resolve_binary("pnpm"));
|
||||
cmd.arg("outdated");
|
||||
cmd.arg("--format");
|
||||
cmd.arg("json");
|
||||
@@ -411,7 +412,7 @@ fn run_install(packages: &[String], args: &[String], verbose: u8) -> Result<()>
|
||||
}
|
||||
}
|
||||
|
||||
let mut cmd = Command::new("pnpm");
|
||||
let mut cmd = Command::new(resolve_binary("pnpm"));
|
||||
cmd.arg("install");
|
||||
|
||||
for pkg in packages {
|
||||
@@ -495,7 +496,7 @@ pub fn run_passthrough(args: &[OsString], verbose: u8) -> Result<()> {
|
||||
if verbose > 0 {
|
||||
eprintln!("pnpm passthrough: {:?}", args);
|
||||
}
|
||||
let status = Command::new("pnpm")
|
||||
let status = Command::new(resolve_binary("pnpm"))
|
||||
.args(args)
|
||||
.status()
|
||||
.context("Failed to run pnpm")?;
|
||||
|
||||
+4
-9
@@ -1,4 +1,5 @@
|
||||
use crate::tracking;
|
||||
use crate::utils::{resolve_binary, tool_exists};
|
||||
use anyhow::{Context, Result};
|
||||
use std::process::Command;
|
||||
|
||||
@@ -26,16 +27,10 @@ pub fn run(cmd: PrismaCommand, args: &[String], verbose: u8) -> Result<()> {
|
||||
|
||||
/// Create a Command that will run prisma (tries global first, then npx)
|
||||
fn create_prisma_command() -> Command {
|
||||
let prisma_exists = Command::new("which")
|
||||
.arg("prisma")
|
||||
.output()
|
||||
.map(|o| o.status.success())
|
||||
.unwrap_or(false);
|
||||
|
||||
if prisma_exists {
|
||||
Command::new("prisma")
|
||||
if tool_exists("prisma") {
|
||||
Command::new(resolve_binary("prisma"))
|
||||
} else {
|
||||
let mut c = Command::new("npx");
|
||||
let mut c = Command::new(resolve_binary("npx"));
|
||||
c.arg("prisma");
|
||||
c
|
||||
}
|
||||
|
||||
+4
-16
@@ -1,5 +1,5 @@
|
||||
use crate::tracking;
|
||||
use crate::utils::truncate;
|
||||
use crate::utils::{resolve_binary, tool_exists, truncate};
|
||||
use anyhow::{Context, Result};
|
||||
use std::process::Command;
|
||||
|
||||
@@ -15,11 +15,11 @@ pub fn run(args: &[String], verbose: u8) -> Result<()> {
|
||||
let timer = tracking::TimedExecution::start();
|
||||
|
||||
// Try to detect pytest command (could be "pytest", "python -m pytest", etc.)
|
||||
let mut cmd = if which_command("pytest").is_some() {
|
||||
Command::new("pytest")
|
||||
let mut cmd = if tool_exists("pytest") {
|
||||
Command::new(resolve_binary("pytest"))
|
||||
} else {
|
||||
// Fallback to python -m pytest
|
||||
let mut c = Command::new("python");
|
||||
let mut c = Command::new(resolve_binary("python"));
|
||||
c.arg("-m").arg("pytest");
|
||||
c
|
||||
};
|
||||
@@ -83,18 +83,6 @@ pub fn run(args: &[String], verbose: u8) -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Check if a command exists in PATH
|
||||
fn which_command(cmd: &str) -> Option<String> {
|
||||
Command::new("which")
|
||||
.arg(cmd)
|
||||
.output()
|
||||
.ok()
|
||||
.filter(|o| o.status.success())
|
||||
.and_then(|o| String::from_utf8(o.stdout).ok())
|
||||
.map(|s| s.trim().to_string())
|
||||
.filter(|s| !s.is_empty())
|
||||
}
|
||||
|
||||
/// Parse pytest output using state machine
|
||||
fn filter_pytest_output(output: &str) -> String {
|
||||
let mut state = ParseState::Header;
|
||||
|
||||
+1
-1
@@ -37,7 +37,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_run_unsupported_returns_none() {
|
||||
assert!(registry::rewrite_command("terraform plan", &[]).is_none());
|
||||
assert!(registry::rewrite_command("htop", &[]).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
+2
-2
@@ -1,5 +1,5 @@
|
||||
use crate::tracking;
|
||||
use crate::utils::truncate;
|
||||
use crate::utils::{resolve_binary, truncate};
|
||||
use anyhow::{Context, Result};
|
||||
use serde::Deserialize;
|
||||
use std::collections::HashMap;
|
||||
@@ -38,7 +38,7 @@ pub fn run(args: &[String], verbose: u8) -> Result<()> {
|
||||
|
||||
let is_format = args.iter().any(|a| a == "format");
|
||||
|
||||
let mut cmd = Command::new("ruff");
|
||||
let mut cmd = Command::new(resolve_binary("ruff"));
|
||||
|
||||
if is_check {
|
||||
// Force JSON output for check command
|
||||
|
||||
+3
-3
@@ -7,6 +7,7 @@
|
||||
//! unless -a flag is present (respecting user intent).
|
||||
|
||||
use crate::tracking;
|
||||
use crate::utils::{resolve_binary, tool_exists};
|
||||
use anyhow::{Context, Result};
|
||||
use std::process::Command;
|
||||
|
||||
@@ -44,8 +45,7 @@ pub fn run(args: &[String], verbose: u8) -> Result<()> {
|
||||
let timer = tracking::TimedExecution::start();
|
||||
|
||||
// Check if tree is installed
|
||||
let tree_check = Command::new("which").arg("tree").output();
|
||||
if tree_check.is_err() || !tree_check.unwrap().status.success() {
|
||||
if !tool_exists("tree") {
|
||||
anyhow::bail!(
|
||||
"tree command not found. Install it first:\n\
|
||||
- macOS: brew install tree\n\
|
||||
@@ -55,7 +55,7 @@ pub fn run(args: &[String], verbose: u8) -> Result<()> {
|
||||
);
|
||||
}
|
||||
|
||||
let mut cmd = Command::new("tree");
|
||||
let mut cmd = Command::new(resolve_binary("tree"));
|
||||
|
||||
// Determine if user wants all files or default behavior
|
||||
let show_all = args.iter().any(|a| a == "-a" || a == "--all");
|
||||
|
||||
+4
-8
@@ -1,5 +1,5 @@
|
||||
use crate::tracking;
|
||||
use crate::utils::truncate;
|
||||
use crate::utils::{resolve_binary, tool_exists, truncate};
|
||||
use anyhow::{Context, Result};
|
||||
use regex::Regex;
|
||||
use std::collections::HashMap;
|
||||
@@ -9,16 +9,12 @@ pub fn run(args: &[String], verbose: u8) -> Result<()> {
|
||||
let timer = tracking::TimedExecution::start();
|
||||
|
||||
// Try tsc directly first, fallback to npx if not found
|
||||
let tsc_exists = Command::new("which")
|
||||
.arg("tsc")
|
||||
.output()
|
||||
.map(|o| o.status.success())
|
||||
.unwrap_or(false);
|
||||
let tsc_exists = tool_exists("tsc");
|
||||
|
||||
let mut cmd = if tsc_exists {
|
||||
Command::new("tsc")
|
||||
Command::new(resolve_binary("tsc"))
|
||||
} else {
|
||||
let mut c = Command::new("npx");
|
||||
let mut c = Command::new(resolve_binary("npx"));
|
||||
c.arg("tsc");
|
||||
c
|
||||
};
|
||||
|
||||
+44
-11
@@ -226,32 +226,39 @@ pub fn detect_package_manager() -> &'static str {
|
||||
}
|
||||
}
|
||||
|
||||
/// Resolve a binary name to its full path, honoring PATHEXT on Windows.
|
||||
/// Falls back to the original name if not found (lets Command::new produce its usual error).
|
||||
pub fn resolve_binary(name: &str) -> std::ffi::OsString {
|
||||
which::which(name)
|
||||
.map(|p| p.into_os_string())
|
||||
.unwrap_or_else(|_| name.into())
|
||||
}
|
||||
|
||||
/// Check if a tool exists on PATH (cross-platform replacement for `Command::new("which")`).
|
||||
pub fn tool_exists(name: &str) -> bool {
|
||||
which::which(name).is_ok()
|
||||
}
|
||||
|
||||
/// Build a Command using the detected package manager's exec mechanism.
|
||||
/// Returns a Command ready to have tool-specific args appended.
|
||||
pub fn package_manager_exec(tool: &str) -> Command {
|
||||
let tool_exists = Command::new("which")
|
||||
.arg(tool)
|
||||
.output()
|
||||
.map(|o| o.status.success())
|
||||
.unwrap_or(false);
|
||||
|
||||
if tool_exists {
|
||||
Command::new(tool)
|
||||
if tool_exists(tool) {
|
||||
Command::new(resolve_binary(tool))
|
||||
} else {
|
||||
let pm = detect_package_manager();
|
||||
match pm {
|
||||
"pnpm" => {
|
||||
let mut c = Command::new("pnpm");
|
||||
let mut c = Command::new(resolve_binary("pnpm"));
|
||||
c.arg("exec").arg("--").arg(tool);
|
||||
c
|
||||
}
|
||||
"yarn" => {
|
||||
let mut c = Command::new("yarn");
|
||||
let mut c = Command::new(resolve_binary("yarn"));
|
||||
c.arg("exec").arg("--").arg(tool);
|
||||
c
|
||||
}
|
||||
_ => {
|
||||
let mut c = Command::new("npx");
|
||||
let mut c = Command::new(resolve_binary("npx"));
|
||||
c.arg("--no-install").arg("--").arg(tool);
|
||||
c
|
||||
}
|
||||
@@ -398,6 +405,32 @@ mod tests {
|
||||
assert_eq!(format_cpt(f64::NAN), "$0.00/MTok"); // NaN
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_resolve_binary_known() {
|
||||
// cargo exists on all dev machines
|
||||
let resolved = resolve_binary("cargo");
|
||||
assert!(!resolved.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_resolve_binary_unknown() {
|
||||
let resolved = resolve_binary("nonexistent_tool_xyz_99999");
|
||||
assert_eq!(
|
||||
resolved,
|
||||
std::ffi::OsString::from("nonexistent_tool_xyz_99999")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_tool_exists_positive() {
|
||||
assert!(tool_exists("cargo"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_tool_exists_negative() {
|
||||
assert!(!tool_exists("nonexistent_tool_xyz_99999"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_detect_package_manager_default() {
|
||||
// In the test environment (rtk repo), there's no JS lockfile
|
||||
|
||||
Reference in new issue
Block a user