Files
sausage/userland/exec-tools/src/env.rs
T
surma b9e3e3677d userland: search and exec tools, spawn library (T14)
rg (ripgrep 15.2), grep (ours, on ripgrep's regex crates), find and xargs
(ours), sed (uutils 0.3 wrapper), diff and cmp (uutils diffutils 0.6 with -r),
fd 10.5, jaq 2.3, env and timeout. userland/spawn is a small Command-like
client of the process interface. search-test.nu: 42 cases match native tools.
2026-11-06 17:21:51 +00:00

21 lines
1.1 KiB
Rust

//! Print the environment or start a command with environment overrides.
use sausage_spawn::Command;
fn run() -> Result<i32, std::io::Error> {
let mut args = std::env::args().skip(1).peekable();
let mut clear = false;
if args.peek().is_some_and(|a| a == "-i" || a == "--ignore-environment") { clear = true; args.next(); }
let mut values = if clear { std::collections::BTreeMap::new() } else { std::env::vars().collect() };
while args.peek().is_some_and(|a| a.contains('=')) {
let arg = args.next().unwrap(); let (key, value) = arg.split_once('=').unwrap();
values.insert(key.to_owned(), value.to_owned());
}
if let Some(program) = args.next() {
let mut command = Command::new(program); command.env_clear();
for (key, value) in values { command.env(key, value); }
command.args(args).status()
} else {
for (key, value) in values { println!("{key}={value}"); } Ok(0)
}
}
fn main() { std::process::exit(match run() { Ok(code) => code, Err(e) => { eprintln!("env: {e}"); if e.kind() == std::io::ErrorKind::NotFound { 127 } else { 126 } } }); }