Rewrite starting

This commit is contained in:
Sam Hedin 2020-06-09 10:15:31 +02:00
parent a40ed5fef2
commit 2fcde8daa9

View File

@ -16,7 +16,7 @@ pub struct DirectorySpecificEnvironment {
impl DirectorySpecificEnvironment { impl DirectorySpecificEnvironment {
pub fn new(allowed_directories: Option<Value>) -> DirectorySpecificEnvironment { pub fn new(allowed_directories: Option<Value>) -> DirectorySpecificEnvironment {
let mut allowed_directories = if let Some(Value { let allowed_directories = if let Some(Value {
value: UntaggedValue::Table(ref wrapped_directories), value: UntaggedValue::Table(ref wrapped_directories),
tag: _, tag: _,
}) = allowed_directories }) = allowed_directories
@ -35,16 +35,11 @@ impl DirectorySpecificEnvironment {
}) })
.collect() .collect()
} else { } else {
vec![] IndexSet::new()
}; };
allowed_directories.sort();
let mut allowed = IndexSet::new();
for d in allowed_directories {
allowed.insert(d);
}
DirectorySpecificEnvironment { DirectorySpecificEnvironment {
allowed_directories: allowed, allowed_directories,
added_env_vars: IndexMap::new(), added_env_vars: IndexMap::new(),
overwritten_env_values: IndexMap::new(), overwritten_env_values: IndexMap::new(),
} }
@ -97,67 +92,48 @@ impl DirectorySpecificEnvironment {
pub fn env_vars_to_add(&mut self) -> Result<IndexMap<String, String>> { pub fn env_vars_to_add(&mut self) -> Result<IndexMap<String, String>> {
let current_dir = std::env::current_dir()?; let current_dir = std::env::current_dir()?;
let mut vars_to_add = IndexMap::new();
let mut working_dir = Some(current_dir.as_path()); let mut working_dir = Some(current_dir.as_path());
let mut vars_to_add = IndexMap::new();
//Start in the current directory, then traverse towards the root with working_dir to see if we are in a subdirectory of a valid directory. //Start in the current directory, then traverse towards the root with working_dir to see if we are in a subdirectory of a valid directory.
while let Some(wdir) = working_dir { while let Some(wdir) = working_dir {
if self.allowed_directories.contains(wdir) { if self.allowed_directories.contains(wdir) {
let toml_doc = match std::fs::read_to_string(wdir.join(".nu").as_path()) { let toml_doc = std::fs::read_to_string(wdir.join(".nu").as_path())
Ok(doc) => doc.parse::<toml::Value>()?, .unwrap_or_else(|_| "[env]".to_string())
Err(_) => return Ok(vars_to_add), .parse::<toml::Value>()?;
};
let vars_in_current_file = toml_doc toml_doc
.get("env") .get("env")
.ok_or_else(|| { .expect("No env section in file")
std::io::Error::new(
std::io::ErrorKind::InvalidData,
"No [env] section in .nu-file",
)
})?
.as_table() .as_table()
.ok_or_else(|| { .ok_or_else(|| {
Error::new( Error::new(
ErrorKind::InvalidData, ErrorKind::InvalidData,
"Malformed [env] section in .nu-file", "Malformed [env] section in .nu-file",
) )
})?;
let mut keys_in_current_nufile = vec![];
for (k, v) in vars_in_current_file {
if !vars_to_add.contains_key(k) {
vars_to_add.insert(
k.clone(),
v.as_str()
.ok_or_else(|| {
Error::new(
ErrorKind::InvalidData,
format!("Could not read environment variable: {}\n", v),
)
})? })?
.to_string(), .iter()
); //This is used to add variables to the environment .for_each(|(k, v)| {
} if !vars_to_add.contains_key(k) {
keys_in_current_nufile.push(k.clone()); //this is used to keep track of which directory added which variables vars_to_add.insert(k.clone(), v.as_str().unwrap().to_string());
}
//If we are about to overwrite any environment variables, we save them first so they can be restored later. //If we are about to overwrite any environment variables, we save them first so they can be restored later.
self.overwritten_env_values.insert( if let Some(val) = std::env::var_os(k) {
wdir.to_path_buf(), let entry = self
keys_in_current_nufile .overwritten_env_values
.iter() .entry(wdir.to_path_buf())
.filter_map(|key| { .or_insert(vec![]);
if let Some(val) = std::env::var_os(key) { entry.push((k.clone(), val));
return Some((key.clone(), val));
} }
None
})
.collect(),
);
self.added_env_vars let entry = self
.insert(wdir.to_path_buf(), keys_in_current_nufile); .added_env_vars
.entry(wdir.to_path_buf())
.or_insert(vec![]);
entry.push(k.clone());
}
});
} }
working_dir = working_dir =
working_dir //Keep going up in the directory structure with .parent() working_dir //Keep going up in the directory structure with .parent()