mirror of
https://github.com/nushell/nushell.git
synced 2024-12-25 08:29:07 +01:00
Working?
This commit is contained in:
parent
6974eb0994
commit
c618538cf8
@ -1,7 +1,7 @@
|
||||
use indexmap::IndexMap;
|
||||
use nu_protocol::{Primitive, UntaggedValue, Value};
|
||||
use std::io::Write;
|
||||
use std::{ffi::OsString, fmt::Debug, path::PathBuf, fs::OpenOptions};
|
||||
use std::{ffi::OsString, fmt::Debug, fs::OpenOptions, path::PathBuf};
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub struct DirectorySpecificEnvironment {
|
||||
@ -52,6 +52,8 @@ impl DirectorySpecificEnvironment {
|
||||
let current_dir = std::env::current_dir()?;
|
||||
|
||||
let mut keyvals_to_restore = IndexMap::new();
|
||||
let mut new_overwritten = IndexMap::new();
|
||||
|
||||
for (directory, keyvals) in &self.overwritten_env_values {
|
||||
let mut working_dir = Some(current_dir.as_path());
|
||||
|
||||
@ -59,6 +61,7 @@ impl DirectorySpecificEnvironment {
|
||||
while let Some(wdir) = working_dir {
|
||||
if wdir == directory.as_path() {
|
||||
readd = false;
|
||||
new_overwritten.insert(directory.clone(), keyvals.clone());
|
||||
break;
|
||||
} else {
|
||||
working_dir = working_dir.unwrap().parent();
|
||||
@ -71,6 +74,7 @@ impl DirectorySpecificEnvironment {
|
||||
}
|
||||
}
|
||||
|
||||
self.overwritten_env_values = new_overwritten;
|
||||
Ok(keyvals_to_restore)
|
||||
}
|
||||
|
||||
@ -102,58 +106,20 @@ impl DirectorySpecificEnvironment {
|
||||
wdir.to_path_buf(),
|
||||
keys_in_file.iter().fold(vec![], |mut keyvals, key| {
|
||||
if let Some(val) = std::env::var_os(key) {
|
||||
|
||||
let mut file = OpenOptions::new()
|
||||
.write(true)
|
||||
.append(true)
|
||||
.create(true)
|
||||
.open("restore.txt").unwrap();
|
||||
|
||||
write!(&mut file, "about to overwrite: {:?}\n", val).unwrap();
|
||||
|
||||
keyvals.push((key.clone(), val));
|
||||
keyvals
|
||||
} else {
|
||||
keyvals
|
||||
}
|
||||
keyvals
|
||||
}),
|
||||
);
|
||||
|
||||
self.added_env_vars.insert(wdir.to_path_buf(), keys_in_file);
|
||||
|
||||
break;
|
||||
} else {
|
||||
working_dir = working_dir.unwrap().parent();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(vars_to_add)
|
||||
}
|
||||
|
||||
//If the user has left directories which added env vars through .nu, we clear those vars
|
||||
pub fn env_vars_to_delete(&mut self) -> std::io::Result<Vec<String>> {
|
||||
let current_dir = std::env::current_dir()?;
|
||||
|
||||
//Gather up all environment variables that should be deleted.
|
||||
//If we are not in a directory or one of its subdirectories, mark the env_vals it maps to for removal.
|
||||
let vars_to_delete = self.added_env_vars.iter().fold(
|
||||
Vec::new(),
|
||||
|mut vars_to_delete, (directory, env_vars)| {
|
||||
let mut working_dir = Some(current_dir.as_path());
|
||||
|
||||
while let Some(wdir) = working_dir {
|
||||
if &wdir == directory {
|
||||
return vars_to_delete;
|
||||
} else {
|
||||
working_dir = working_dir.unwrap().parent();
|
||||
}
|
||||
}
|
||||
//only delete vars from directories we are not in
|
||||
vars_to_delete.extend(env_vars.clone());
|
||||
vars_to_delete
|
||||
},
|
||||
);
|
||||
|
||||
Ok(vars_to_delete)
|
||||
}
|
||||
}
|
||||
|
6
crates/nu-cli/src/env/environment.rs
vendored
6
crates/nu-cli/src/env/environment.rs
vendored
@ -35,7 +35,7 @@ impl Env for Box<dyn Env> {
|
||||
pub struct Environment {
|
||||
environment_vars: Option<Value>,
|
||||
path_vars: Option<Value>,
|
||||
direnv: DirectorySpecificEnvironment,
|
||||
pub direnv: DirectorySpecificEnvironment,
|
||||
}
|
||||
|
||||
impl Environment {
|
||||
@ -63,13 +63,11 @@ impl Environment {
|
||||
self.add_env(&k, &v, true);
|
||||
});
|
||||
|
||||
self.direnv.env_vars_to_delete()?.iter().for_each(|v| {
|
||||
self.remove_env(v);
|
||||
});
|
||||
|
||||
self.direnv.overwritten_values_to_restore()?.iter().for_each(|(k, v)| {
|
||||
self.add_env(&k, &v, true);
|
||||
});
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
27
crates/nu-cli/src/env/environment_syncer.rs
vendored
27
crates/nu-cli/src/env/environment_syncer.rs
vendored
@ -41,14 +41,13 @@ impl EnvironmentSyncer {
|
||||
environment.morph(&*self.config);
|
||||
}
|
||||
|
||||
|
||||
pub fn sync_env_vars(&mut self, ctx: &mut Context) {
|
||||
let mut environment = self.env.lock();
|
||||
|
||||
match environment.maintain_directory_environment() {
|
||||
Ok(_) => {}
|
||||
Err(e) => {panic!(e)}
|
||||
}
|
||||
// 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()) {
|
||||
@ -57,6 +56,24 @@ impl EnvironmentSyncer {
|
||||
// that aren't loaded from config.
|
||||
environment.add_env(&name, &value, false);
|
||||
|
||||
environment
|
||||
.direnv
|
||||
.env_vars_to_add()
|
||||
.unwrap()
|
||||
.iter()
|
||||
.for_each(|(k, v)| {
|
||||
environment.add_env(&k, &v, true);
|
||||
});
|
||||
|
||||
environment
|
||||
.direnv
|
||||
.overwritten_values_to_restore()
|
||||
.unwrap()
|
||||
.iter()
|
||||
.for_each(|(k, v)| {
|
||||
environment.add_env(&k, &v, true);
|
||||
});
|
||||
|
||||
// clear the env var from the session
|
||||
// we are about to replace them
|
||||
ctx.with_host(|host| host.env_rm(std::ffi::OsString::from(name)));
|
||||
|
6
post.org
6
post.org
@ -12,8 +12,8 @@ In order for a .nu-file to be read, the directory it is in must be listed in the
|
||||
```
|
||||
nu_env_dirs = ["/home/sam", "/home/sam/github", "/home/sam/github/test"]
|
||||
```
|
||||
This was implemented for the sake of security. I do not believe that it is appropiate for nushell to pick up random .nu-files unless
|
||||
the user has explicitly said that it's alright. An adversary could hide a .nu-file in an otherwise unsuspicous folder that you download,
|
||||
This was implemented for the sake of security. I do not believe that it is appropiate for nushell to pick up random .nu-files unless
|
||||
the user has explicitly said that it's alright. An adversary could hide a .nu-file in an otherwise unsuspicous folder that you download,
|
||||
and now you suddenly have nushell picking up whatever environment variables they set.
|
||||
This meant that the code necessarily become more involved than just looking for a .nu-file in the current directory and applying its variables,
|
||||
but this extra code also allowed for some other nice features and behavior which is listed below.
|
||||
@ -30,8 +30,6 @@ Questions:
|
||||
Just using what I think was expected, envs.entries.remove(key), does not work.
|
||||
If this is a problem, how can I make it work better with existing code?
|
||||
|
||||
TODO: take care of situation where a directory overwrites an existing .nu conf.
|
||||
|
||||
----
|
||||
|
||||
#
|
Loading…
Reference in New Issue
Block a user