2023-02-04 08:54:27 -05:00
|
|
|
use std::env;
|
2023-01-21 19:20:30 -05:00
|
|
|
|
|
|
|
use super::Cmd;
|
|
|
|
use clap::{Arg, ArgAction, Command};
|
|
|
|
|
2023-04-17 23:27:57 -04:00
|
|
|
#[derive(Debug)]
|
2023-01-21 19:20:30 -05:00
|
|
|
pub struct Printenv;
|
|
|
|
|
|
|
|
impl Cmd for Printenv {
|
|
|
|
fn cli(&self) -> clap::Command {
|
|
|
|
Command::new("printenv")
|
|
|
|
.about("print all or part of environment")
|
|
|
|
.author("Nathan Fisher")
|
|
|
|
.version(env!("CARGO_PKG_VERSION"))
|
|
|
|
.args([
|
|
|
|
Arg::new("null")
|
|
|
|
.short('0')
|
|
|
|
.long("null")
|
|
|
|
.help("end each output line with NUL, not newline")
|
|
|
|
.action(ArgAction::SetTrue),
|
|
|
|
Arg::new("var")
|
|
|
|
.value_name("VARIABLE")
|
|
|
|
.num_args(1..)
|
|
|
|
.required(false),
|
|
|
|
])
|
|
|
|
}
|
|
|
|
|
2023-02-04 08:54:27 -05:00
|
|
|
fn run(&self, matches: &clap::ArgMatches) -> Result<(), Box<dyn std::error::Error>> {
|
2023-01-21 19:20:30 -05:00
|
|
|
if let Some(vars) = matches.get_many::<String>("var") {
|
|
|
|
for var in vars {
|
|
|
|
let val = env::var(var)?;
|
|
|
|
if matches.get_flag("null") {
|
|
|
|
print!("{var}={val}\0");
|
|
|
|
} else {
|
|
|
|
println!("{var}={val}");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
env::vars().for_each(|(var, val)| {
|
|
|
|
if matches.get_flag("null") {
|
|
|
|
print!("{var}={val}\0");
|
|
|
|
} else {
|
|
|
|
println!("{var}={val}");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-02-05 23:50:59 -05:00
|
|
|
fn path(&self) -> Option<shitbox::Path> {
|
|
|
|
Some(shitbox::Path::UsrBin)
|
2023-01-21 19:20:30 -05:00
|
|
|
}
|
|
|
|
}
|