Refactoring, add comment

This commit is contained in:
Sam Hedin 2020-06-04 05:30:28 +02:00
parent de0c252e27
commit 6e72386360

View File

@ -2,12 +2,10 @@ use crate::data::config::Conf;
use indexmap::{indexmap, IndexSet};
use nu_protocol::{UntaggedValue, Value};
use std::collections::{HashMap};
use indexmap::IndexMap;
use std::ffi::OsString;
use std::fmt::Debug;
use std::fs::File;
use std::io::prelude::*;
use std::io::Write;
use std::path::PathBuf;
pub trait Env: Debug + Send {
@ -40,7 +38,7 @@ impl Env for Box<dyn Env> {
pub struct Environment {
environment_vars: Option<Value>,
path_vars: Option<Value>,
nurc_env_vars: HashMap<PathBuf, Vec<String>>,
nurc_env_keys: HashMap<PathBuf, Vec<String>>, //Directory -> Env key. If an environment var has been added from a .nurc in a directory, we track it here so we can remove it when the user leaves the directory.
}
impl Environment {
@ -48,7 +46,7 @@ impl Environment {
Environment {
environment_vars: None,
path_vars: None,
nurc_env_vars: HashMap::new(),
nurc_env_keys: HashMap::new(),
}
}
@ -59,7 +57,7 @@ impl Environment {
Environment {
environment_vars: env,
path_vars: path,
nurc_env_vars: HashMap::new(),
nurc_env_keys: HashMap::new(),
}
}
@ -90,7 +88,7 @@ impl Environment {
self.add_env(k, v.as_str().unwrap());
});
self.nurc_env_vars.insert(
self.nurc_env_keys.insert(
std::env::current_dir()?,
nurc_vars.keys().map(|k| k.clone()).collect(),
); //Maybe could do without clone here, but leave for now
@ -99,17 +97,16 @@ impl Environment {
//If the user has left directories which added env vars through .nurc, we clear those vars
//For each directory d in nurc_env_vars:
//if current_dir does not have d as a parent (possibly recursive),
//the vars set by d should be removed
//if current_dir does not have d as a parent (possibly recursive), the vars set by d should be removed
pub fn clear_vars_from_unvisited_dirs(&mut self) -> std::io::Result<()> {
let current_dir = std::env::current_dir()?;
let mut vars_to_keep = HashMap::new();
for (d, v) in self.nurc_env_vars.iter() {
let mut new_nurc_env_vars = HashMap::new();
for (d, v) in self.nurc_env_keys.iter() {
let mut working_dir = Some(current_dir.as_path());
while working_dir.is_some() {
if working_dir.unwrap() == d {
vars_to_keep.insert(d.clone(), v.clone());
new_nurc_env_vars.insert(d.clone(), v.clone());
break;
} else {
working_dir = working_dir.unwrap().parent();
@ -118,33 +115,25 @@ impl Environment {
}
let mut vars_to_delete = vec![];
for (path, vals) in self.nurc_env_vars.iter() {
if !vars_to_keep.contains_key(path) {
for (path, vals) in self.nurc_env_keys.iter() {
if !new_nurc_env_vars.contains_key(path) {
vars_to_delete.extend(vals.clone());
}
}
vars_to_delete.iter().for_each(|var| self.remove_env(var));
vars_to_delete.iter().for_each(|env_var| self.remove_env(env_var));
self.nurc_env_vars = vars_to_keep;
self.nurc_env_keys = new_nurc_env_vars;
Ok(())
}
// environment_vars: Option<Value>,
//
pub fn remove_env(&mut self, key: &str) {
if let Some(Value {
value: UntaggedValue::Row(envs),
tag: _,
}) = &mut self.environment_vars
{
let mut file = File::create("env.txt").unwrap();
write!(&mut file, "env: {:?}", envs.entries).unwrap();
let mut file = File::create("removenv.txt").unwrap();
let removed = envs.remove_key(key);
write!(&mut file, "removed {:?}", (key, removed)).unwrap();
envs.remove_key(key);
}
}