const GIT_NOISY_SUBCMDS: &[&str] = &["clone", "pull", "fetch", "push", "status"];
use crate::filters::{base_program, is_filterable};
fn has_shell_operators(cmd: &str) -> bool {
cmd.contains(|c| {
matches!(
c,
'|' | '&' | ';' | '>' | '<' | '`' | '$' | '(' | ')' | '\n'
)
})
}
fn is_follow(base: &str, rest: &[&str]) -> bool {
if !matches!(base, "tail" | "journalctl") {
return false;
}
rest.iter().any(|a| {
*a == "--retry"
|| a.starts_with("--follow")
|| (a.starts_with('-') && !a.starts_with("--") && a.contains(['f', 'F']))
})
}
pub fn rewritten(cmd: &str) -> Option<String> {
let trimmed = cmd.trim();
if trimmed.is_empty() || has_shell_operators(trimmed) {
return None;
}
let mut parts = trimmed.split_whitespace();
let base = base_program(parts.next()?);
if base == "mimir" {
return None; }
let rest: Vec<&str> = parts.collect();
if is_follow(base, &rest) {
return None;
}
let eligible = if base == "git" {
rest.first()
.is_some_and(|sub| GIT_NOISY_SUBCMDS.contains(sub))
} else {
is_filterable(base)
};
eligible.then(|| format!("mimir run -- {trimmed}"))
}
pub fn rewrite(cmd_parts: Vec<String>) -> ! {
let cmd = cmd_parts.join(" ");
match rewritten(&cmd) {
Some(new) => {
println!("{new}");
std::process::exit(0)
}
None => std::process::exit(1),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn wraps_known_commands() {
assert_eq!(
rewritten("cargo build").as_deref(),
Some("mimir run -- cargo build")
);
assert_eq!(
rewritten("npm install").as_deref(),
Some("mimir run -- npm install")
);
}
#[test]
fn git_only_noisy_subcommands() {
assert!(rewritten("git clone https://x").is_some());
assert!(rewritten("git status").is_some());
assert!(rewritten("git log").is_none());
assert!(rewritten("git diff").is_none());
}
#[test]
fn wraps_the_expanded_toolchain() {
for cmd in [
"go build ./...",
"make",
"docker build .",
"pip install requests",
"jest",
"tsc --noEmit",
"eslint .",
] {
assert!(rewritten(cmd).is_some(), "should wrap: {cmd}");
}
}
#[test]
fn skips_compound_and_already_wrapped() {
assert!(rewritten("cargo build | grep error").is_none());
assert!(rewritten("cargo build && cargo test").is_none());
assert!(rewritten("mimir run -- cargo build").is_none());
assert!(rewritten("echo hello").is_none()); assert!(rewritten("").is_none());
}
#[test]
fn wraps_content_commands() {
for cmd in [
"cat README.md",
"grep -rn foo src",
"rg pattern",
"find . -name '*.rs'",
"ls -la",
"df -h",
"du -sh .",
"kubectl get pods",
] {
assert!(rewritten(cmd).is_some(), "should wrap: {cmd}");
}
}
#[test]
fn does_not_wrap_follow_streams() {
assert!(rewritten("tail -f /var/log/syslog").is_none());
assert!(rewritten("tail -F app.log").is_none());
assert!(rewritten("tail --follow=name x").is_none());
assert!(rewritten("journalctl -f").is_none());
}
#[test]
fn wraps_non_follow_tail_and_unrelated_dash_f() {
assert!(rewritten("tail -n 5000 big.log").is_some());
assert!(rewritten("grep -f patterns.txt data.txt").is_some());
}
#[test]
fn strips_path_prefix() {
assert_eq!(
rewritten("/usr/bin/cargo test").as_deref(),
Some("mimir run -- /usr/bin/cargo test")
);
}
}