From 8498c673bd74e6903aacb801fb3d9b5f270d9448 Mon Sep 17 00:00:00 2001 From: Sam Hedin Date: Sat, 6 Jun 2020 01:47:09 +0200 Subject: [PATCH] Refactoring --- crates/nu-cli/src/env/environment.rs | 119 ++++++++++++++------ crates/nu-cli/src/env/environment_syncer.rs | 68 ++--------- post.org | 1 - 3 files changed, 93 insertions(+), 95 deletions(-) diff --git a/crates/nu-cli/src/env/environment.rs b/crates/nu-cli/src/env/environment.rs index 91c47db9e1..a7f63ca622 100644 --- a/crates/nu-cli/src/env/environment.rs +++ b/crates/nu-cli/src/env/environment.rs @@ -1,14 +1,14 @@ use crate::data::config::Conf; use indexmap::{indexmap, IndexSet}; -use nu_protocol::{UntaggedValue, Value}; +use nu_protocol::{Primitive, UntaggedValue, Value}; use std::ffi::OsString; -use std::fmt::Debug; +use std::{collections::HashMap, fmt::Debug, fs::File, io::Read, path::PathBuf}; pub trait Env: Debug + Send { fn env(&self) -> Option; fn path(&self) -> Option; - fn add_env(&mut self, key: &str, value: &str); + fn add_env(&mut self, key: &str, value: &str, overwrite_existing: bool); fn add_path(&mut self, new_path: OsString); } @@ -21,8 +21,8 @@ impl Env for Box { (**self).path() } - fn add_env(&mut self, key: &str, value: &str) { - (**self).add_env(key, value); + fn add_env(&mut self, key: &str, value: &str, overwrite_existing: bool) { + (**self).add_env(key, value, overwrite_existing); } fn add_path(&mut self, new_path: OsString) { @@ -30,10 +30,62 @@ impl Env for Box { } } +#[derive(Debug, Default)] +struct DirectorySpecificEnvironment { + pub whitelisted_directories: Vec, + pub added_env_vars: HashMap>, //Directory -> Env key. If an environment var has been added from a .nu in a directory, we track it here so we can remove it when the user leaves the directory. + pub overwritten_env_values: HashMap>, //Directory -> (env_key, value). If a .nu overwrites some existing environment variables, they are added here so that they can be restored later. +} + +impl DirectorySpecificEnvironment { + pub fn new(whitelisted_directories: Vec) -> DirectorySpecificEnvironment { + DirectorySpecificEnvironment { + whitelisted_directories, + added_env_vars: HashMap::new(), + overwritten_env_values: HashMap::new(), + } + } + + pub fn env_vars_to_add(&mut self) -> std::io::Result> { + let current_dir = std::env::current_dir()?; + + let mut vars_to_add = HashMap::new(); + for dir in &self.whitelisted_directories { + + //Start in the current directory, then traverse towards the root directory with working_dir to check for .nu files + let mut working_dir = Some(current_dir.as_path()); + + while let Some(wdir) = working_dir { + if wdir == dir.as_path() { + let mut dir = dir.clone(); + dir.push(".nu"); + + //Read the .nu file and parse it into a nice map + let mut file = File::open(dir.as_path())?; + let mut contents = String::new(); + file.read_to_string(&mut contents)?; + let toml_doc = contents.parse::().unwrap(); + let vars_in_current_file = toml_doc.get("env").unwrap().as_table().unwrap(); + + for (k, v) in vars_in_current_file { + vars_to_add.insert(k.clone(), v.as_str().unwrap().to_string()); + } + break; + } else { + working_dir = working_dir.unwrap().parent(); + } + } + } + + Ok(vars_to_add) + } +} + #[derive(Debug, Default)] pub struct Environment { environment_vars: Option, path_vars: Option, + direnv: DirectorySpecificEnvironment, } impl Environment { @@ -41,43 +93,46 @@ impl Environment { Environment { environment_vars: None, path_vars: None, + direnv: DirectorySpecificEnvironment::new(vec![]), } } + pub fn from_config(configuration: &T) -> Environment { let env = configuration.env(); let path = configuration.path(); + let mut directories = vec![]; + if let Some(Value { + value: UntaggedValue::Table(ref directories_as_values), + tag: _, + }) = configuration.direnv_whitelist() + { + for dirval in directories_as_values { + if let Value { + value: UntaggedValue::Primitive(Primitive::String(ref dir)), + tag: _, + } = dirval + { + directories.push(PathBuf::from(&dir)); + } + } + }; + directories.sort(); + Environment { environment_vars: env, path_vars: path, + direnv: DirectorySpecificEnvironment::new(directories), } } - - pub fn add_env_force(&mut self, key: &str, value: &str) { - let value = UntaggedValue::string(value); - - let new_envs = { - if let Some(Value { - value: UntaggedValue::Row(ref envs), - ref tag, - }) = self.environment_vars - { - let mut new_envs = envs.clone(); - - new_envs.insert_data_at_key(key, value.into_value(tag.clone())); - - Value { - value: UntaggedValue::Row(new_envs), - tag: tag.clone(), - } - } else { - UntaggedValue::Row(indexmap! { key.into() => value.into_untagged_value() }.into()) - .into_untagged_value() - } - }; - self.environment_vars = Some(new_envs); + pub fn maintain_directory_environment(&mut self) -> std::io::Result<()> { + let vars_to_add = self.direnv.env_vars_to_add()?; + vars_to_add.iter().for_each(|(k, v)| { + self.add_env(&k, &v, true); + }); + Ok(()) } pub fn morph(&mut self, configuration: &T) { @@ -103,9 +158,7 @@ impl Env for Environment { None } - - - fn add_env(&mut self, key: &str, value: &str) { + fn add_env(&mut self, key: &str, value: &str, overwrite_existing: bool) { let value = UntaggedValue::string(value); let new_envs = { @@ -116,7 +169,7 @@ impl Env for Environment { { let mut new_envs = envs.clone(); - if !new_envs.contains_key(key) { + if !new_envs.contains_key(key) || overwrite_existing { new_envs.insert_data_at_key(key, value.into_value(tag.clone())); } diff --git a/crates/nu-cli/src/env/environment_syncer.rs b/crates/nu-cli/src/env/environment_syncer.rs index a8654a5e59..c29eeaba85 100644 --- a/crates/nu-cli/src/env/environment_syncer.rs +++ b/crates/nu-cli/src/env/environment_syncer.rs @@ -48,75 +48,21 @@ impl EnvironmentSyncer { environment.morph(&*self.config); } - //For directory wd in whitelisted directories - //if current directory is wd or subdir to wd, add env vars from .nurc in wd - //vars: envtest, moretest, anothertest - pub fn add_nurc(&self) -> std::io::Result<()> { - let mut environment = self.env.lock(); - let mut file = OpenOptions::new() - .write(true) - .append(true) - .create(true) - .open("dirs.txt")?; - - let conf = Arc::clone(&self.config); - - let mut directories = vec![]; //TODO: sort the directories to achieve desired functionality about overwrites - if let Some(Value { - value: UntaggedValue::Table(ref directories_as_values), - tag: _, - }) = conf.direnv_whitelist() - { - for dirval in directories_as_values { - if let Value { - value: UntaggedValue::Primitive(Primitive::String(ref dir)), - tag: _, - } = dirval - { - directories.push(PathBuf::from(&dir)); - } - } - }; - directories.sort(); - - write!(&mut file, "sorted dirs: {:?}\n", directories).unwrap(); - - let current_dir = std::env::current_dir()?; - - for mut dir in directories { - let mut working_dir = Some(current_dir.as_path()); - - while working_dir.is_some() { - if working_dir.unwrap() == dir.as_path() { - dir.push(".nu"); - let mut file = File::open(dir.as_path())?; - let mut contents = String::new(); - file.read_to_string(&mut contents)?; - - let toml_doc = contents.parse::().unwrap(); - let nurc_vars = toml_doc.get("env").unwrap().as_table().unwrap(); - - nurc_vars.iter().for_each(|(k, v)| { - environment.add_env_force(&k, &v.as_str().unwrap().to_string()); - }); - break; - } else { - working_dir = working_dir.unwrap().parent(); - } - } - } - Ok(()) - } pub fn sync_env_vars(&mut self, ctx: &mut Context) { - self.add_nurc(); let mut environment = self.env.lock(); + + match environment.maintain_directory_environment() { + Ok(_) => {} + Err(e) => {panic!(e)} + } + if environment.env().is_some() { for (name, value) in ctx.with_host(|host| host.vars()) { if name != "path" && name != "PATH" { // account for new env vars present in the current session // that aren't loaded from config. - environment.add_env(&name, &value); + environment.add_env(&name, &value, false); // clear the env var from the session // we are about to replace them diff --git a/post.org b/post.org index f2eb879153..2c304e37b1 100644 --- a/post.org +++ b/post.org @@ -3,7 +3,6 @@ For #86 Environment variables are added if you have created a file called .nu inside a whitelisted directory, formatted as shown below. (I am, of course, open to change everything about this) ``` -#inside a .nu-file in a whitelisted directory [env] var = "value" anothervar = "anothervalue"