Skip to main content

shell_effect

Function shell_effect 

Source
pub fn shell_effect(cmd: &str) -> ShellEffect
Expand description

Classify one shell command by its text alone; nothing is run. A command that writes a file is an edit whatever else it does, so a write outranks the read or search it starts with; otherwise the leading command decides, and a pipe into grep makes a search. A redirection to /dev/null or to another descriptor (2>&1) writes nothing and is not an edit, and neither is a > inside a quoted argument, which is text, not a redirection.

use majordomus_cli::economics::usage::{shell_effect, ShellEffect};
assert_eq!(shell_effect("cat > tests/test_x.py <<'EOF'"), ShellEffect::Edit);
assert_eq!(shell_effect("sed -i '' 's/a/b/' f.py"), ShellEffect::Edit);
assert_eq!(shell_effect("python3 -m unittest -q 2>&1 | tail -3"), ShellEffect::Other);
assert_eq!(shell_effect("grep -rn vat ledgerlite"), ShellEffect::Search);
assert_eq!(shell_effect("cat ledgerlite/tax.py"), ShellEffect::Read);
assert_eq!(shell_effect("cat a.py > b.py"), ShellEffect::Edit, "the write outranks the read");
assert_eq!(shell_effect("ls src > /dev/null"), ShellEffect::Search);
assert_eq!(shell_effect("grep -rn \"total > limit\" ledgerlite"), ShellEffect::Search);
assert_eq!(shell_effect("echo done > 'notes.md'"), ShellEffect::Edit, "a quoted target");