mirror of
https://github.com/nushell/nushell.git
synced 2025-05-19 09:20:45 +02:00
nicer errors
This commit is contained in:
parent
4e0a0df8a8
commit
2f4f8632de
@ -8,6 +8,7 @@ use std::{
|
||||
io::{Error, ErrorKind, Result},
|
||||
path::PathBuf,
|
||||
};
|
||||
use nu_errors::ShellError;
|
||||
|
||||
type EnvKey = String;
|
||||
type EnvVal = OsString;
|
||||
@ -82,12 +83,23 @@ impl DirectorySpecificEnvironment {
|
||||
Ok(keyvals_to_restore)
|
||||
}
|
||||
|
||||
pub fn env_vars_to_add(&mut self) -> Result<IndexMap<EnvKey, EnvVal>> {
|
||||
pub fn env_vars_to_add(&mut self) -> std::result::Result<IndexMap<EnvKey, EnvVal>, ShellError> {
|
||||
let current_dir = std::env::current_dir()?;
|
||||
let mut working_dir = Some(current_dir.as_path());
|
||||
|
||||
let mut vars_to_add = IndexMap::new();
|
||||
|
||||
// let mut file = OpenOptions::new()
|
||||
// .write(true)
|
||||
// .append(true)
|
||||
// .create(true)
|
||||
// .open("toadd.txt")
|
||||
// .unwrap(
|
||||
// );
|
||||
|
||||
// write!(&mut file, "1: {:?}\n", vars_to_add).unwrap();
|
||||
//WE CRASHING SOMEWHERE HERE
|
||||
|
||||
//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 {
|
||||
if self.allowed_directories.contains(wdir) {
|
||||
@ -96,9 +108,9 @@ impl DirectorySpecificEnvironment {
|
||||
|
||||
toml_doc
|
||||
.get("env")
|
||||
.ok_or_else(|| Error::new(ErrorKind::InvalidData, "env section missing"))?
|
||||
.ok_or_else(|| Err(ShellError::untagged_runtime_error("env section missing")))?
|
||||
.as_table()
|
||||
.ok_or_else(|| Error::new(ErrorKind::InvalidData, "env section malformed"))?
|
||||
.ok_or_else(|| Err(ShellError::untagged_runtime_error("env section malformed")))?
|
||||
.iter()
|
||||
.for_each(|(directory_env_key, directory_env_val)| {
|
||||
if !vars_to_add.contains_key(directory_env_key) {
|
||||
|
25
crates/nu-cli/src/env/environment.rs
vendored
25
crates/nu-cli/src/env/environment.rs
vendored
@ -60,20 +60,21 @@ impl Environment {
|
||||
}
|
||||
|
||||
pub fn maintain_directory_environment(&mut self) -> std::io::Result<()> {
|
||||
self.direnv.env_vars_to_delete()?.iter().for_each(|k| {
|
||||
self.remove_env(&k);
|
||||
});
|
||||
// self.direnv.env_vars_to_delete()?.iter().for_each(|k| {
|
||||
// self.remove_env(&k);
|
||||
// });
|
||||
|
||||
self.direnv
|
||||
.overwritten_values_to_restore()?
|
||||
.iter()
|
||||
.for_each(|(k, v)| {
|
||||
self.add_env(&k, &v.to_string_lossy(), true);
|
||||
});
|
||||
// self.direnv
|
||||
// .overwritten_values_to_restore()?
|
||||
// .iter()
|
||||
// .for_each(|(k, v)| {
|
||||
// self.add_env(&k, &v.to_string_lossy(), true);
|
||||
// });
|
||||
|
||||
self.direnv.env_vars_to_add()?.iter().for_each(|(k, v)| {
|
||||
self.add_env(&k, &v.to_string_lossy(), true);
|
||||
});
|
||||
// self.direnv.env_vars_to_add()?.iter().for_each(|(k, v)| {
|
||||
// std::env::set_var(k, v);
|
||||
// self.add_env(&k, &v.to_string_lossy(), true);
|
||||
// });
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
6
crates/nu-cli/src/env/environment_syncer.rs
vendored
6
crates/nu-cli/src/env/environment_syncer.rs
vendored
@ -3,6 +3,7 @@ use crate::data::config::{Conf, NuConfig};
|
||||
use crate::env::environment::{Env, Environment};
|
||||
use parking_lot::Mutex;
|
||||
use std::sync::Arc;
|
||||
use nu_errors::ShellError;
|
||||
|
||||
pub struct EnvironmentSyncer {
|
||||
pub env: Arc<Mutex<Box<Environment>>>,
|
||||
@ -41,10 +42,9 @@ impl EnvironmentSyncer {
|
||||
environment.morph(&*self.config);
|
||||
}
|
||||
|
||||
pub fn sync_env_vars(&mut self, ctx: &mut Context) {
|
||||
pub fn sync_env_vars(&mut self, ctx: &mut Context) -> Result<(), ShellError> {
|
||||
let mut environment = self.env.lock();
|
||||
|
||||
environment.maintain_directory_environment().ok();
|
||||
if environment.env().is_some() {
|
||||
for (name, value) in ctx.with_host(|host| host.vars()) {
|
||||
if name != "path" && name != "PATH" {
|
||||
@ -52,6 +52,7 @@ impl EnvironmentSyncer {
|
||||
// that aren't loaded from config.
|
||||
environment.add_env(&name, &value, false);
|
||||
|
||||
environment.maintain_directory_environment()?;
|
||||
|
||||
// clear the env var from the session
|
||||
// we are about to replace them
|
||||
@ -72,6 +73,7 @@ impl EnvironmentSyncer {
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn sync_path_vars(&mut self, ctx: &mut Context) {
|
||||
|
Loading…
Reference in New Issue
Block a user