use std::collections::BTreeMap; use std::env; use std::os::unix::process::ExitStatusExt; use std::process::{ExitStatus, Output}; /// export /// /// The builtin `export` command. Used to set global environment variables for /// the current instance of the shell. /// /// # Aliases /// * export /// * set /// /// # Shell Examples /// ```sh /// export FOO=BAR /// ``` pub fn incant(clause: &Option>, uout: bool) -> Output { let status = 0; let mut out: Vec = Vec::new(); let err: Vec = Vec::new(); match clause { Some(clause) => { for stanza in clause { let (key, val) = match stanza.split_once("=") { Some((key, val)) => (key, val), None => continue, }; env::set_var(key, val); } } None => { let mut lines = Vec::new(); let sorted: BTreeMap<_, _> = env::vars().into_iter().collect(); for (key, val) in sorted { lines.push(format!("{}={}", key, val)); } if uout { out.append(&mut format!("{}\n", lines.join("\n")).as_bytes().to_vec()); } else { println!("{}", lines.join("\n")); } } } Output { status: ExitStatus::from_raw(status), stdout: out, stderr: err, } } /// unset /// /// The builtin `unset` command. Used to remove global environment variables /// from the current instance of the shell, since `export` may be called with /// an empty string as the value. /// /// # Shell Examples /// ```sh /// unset FOO /// ``` pub fn unincant(clause: &Option>, uerr: bool) -> Output { let mut status = 0; let out: Vec = Vec::new(); let mut err: Vec = Vec::new(); match clause { Some(clause) => { for stanza in clause { env::remove_var(stanza); } } None => { status = 1; if uerr { err.append(&mut "unset: not enough arguments\n".as_bytes().to_vec()); } else { eprintln!("unset: not enough arguments"); } } } Output { status: ExitStatus::from_raw(status), stdout: out, stderr: err, } }