diff options
author | Jesse Luehrs <doy@tozt.net> | 2022-01-08 00:46:49 -0500 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2022-01-08 00:46:49 -0500 |
commit | 9f7e4412f334dca01f19d275af6a3eaa5b449dba (patch) | |
tree | 1ad97ad7184c62ab8ad6c285f06360d251e6c49f | |
parent | 489fd0db1bf7f1de31a043e315179e83b77921aa (diff) | |
download | nbsh-9f7e4412f334dca01f19d275af6a3eaa5b449dba.tar.gz nbsh-9f7e4412f334dca01f19d275af6a3eaa5b449dba.zip |
implement environment variables
-rw-r--r-- | src/env.rs | 15 | ||||
-rw-r--r-- | src/parse/ast.rs | 5 |
2 files changed, 17 insertions, 3 deletions
@@ -17,6 +17,7 @@ pub struct V0 { )] latest_status: async_std::process::ExitStatus, pwd: std::path::PathBuf, + vars: std::collections::HashMap<std::ffi::OsString, std::ffi::OsString>, } impl Env { @@ -26,6 +27,7 @@ impl Env { idx: 0, latest_status: std::process::ExitStatus::from_raw(0), pwd: std::env::current_dir().unwrap(), + vars: std::env::vars_os().collect(), }) } @@ -83,10 +85,23 @@ impl Env { } } + pub fn var(&self, k: &str) -> String { + match self { + Self::V0(env) => { + env.vars.get(std::ffi::OsStr::new(k)).map_or_else( + || "".to_string(), + |v| v.to_str().unwrap().to_string(), + ) + } + } + } + pub fn apply(&self, cmd: &mut pty_process::Command) { match self { Self::V0(env) => { cmd.current_dir(&env.pwd); + cmd.env_clear(); + cmd.envs(env.vars.iter()); } } } diff --git a/src/parse/ast.rs b/src/parse/ast.rs index 71cb9af..b8e1ca0 100644 --- a/src/parse/ast.rs +++ b/src/parse/ast.rs @@ -171,10 +171,9 @@ impl Word { } } - fn eval(self, _env: &Env) -> String { + fn eval(self, env: &Env) -> String { if self.var { - // TODO - format!("'value-of-${{{}}}'", self.word) + env.var(&self.word) } else { self.word } |