mirror of
https://github.com/nushell/nushell.git
synced 2025-05-03 01:24:29 +02:00
Error handling
This commit is contained in:
parent
fb1839971a
commit
688df20a30
@ -237,7 +237,7 @@ pub fn create_default_context(
|
|||||||
syncer.load_environment();
|
syncer.load_environment();
|
||||||
|
|
||||||
let mut context = Context::basic()?;
|
let mut context = Context::basic()?;
|
||||||
syncer.sync_env_vars(&mut context);
|
syncer.sync_env_vars(&mut context)?;
|
||||||
syncer.sync_path_vars(&mut context);
|
syncer.sync_path_vars(&mut context);
|
||||||
|
|
||||||
{
|
{
|
||||||
@ -668,7 +668,7 @@ pub async fn cli(
|
|||||||
// TODO: make sure config is cached so we don't path this load every call
|
// TODO: make sure config is cached so we don't path this load every call
|
||||||
// FIXME: we probably want to be a bit more graceful if we can't set the environment
|
// FIXME: we probably want to be a bit more graceful if we can't set the environment
|
||||||
syncer.reload();
|
syncer.reload();
|
||||||
syncer.sync_env_vars(&mut context);
|
syncer.sync_env_vars(&mut context)?;
|
||||||
syncer.sync_path_vars(&mut context);
|
syncer.sync_path_vars(&mut context);
|
||||||
|
|
||||||
match line {
|
match line {
|
||||||
|
3
crates/nu-cli/src/env/TODO.org
vendored
3
crates/nu-cli/src/env/TODO.org
vendored
@ -19,4 +19,5 @@ returning =None=, which completely skips running the code for dealing with direc
|
|||||||
- If a directory contains a .nu with an environment variable that was already set, the old value will be overwritten.
|
- If a directory contains a .nu with an environment variable that was already set, the old value will be overwritten.
|
||||||
- This holds even if the old value was set by a .nu in a parent directory. The overwritten value is restored when you leave the directory.
|
- This holds even if the old value was set by a .nu in a parent directory. The overwritten value is restored when you leave the directory.
|
||||||
** Security
|
** Security
|
||||||
https://github.com/nushell/nushell/issues/1965
|
https://github.com/nushell/nushell/issues/1965
|
||||||
|
** Nice errors
|
@ -1,4 +1,5 @@
|
|||||||
use indexmap::{IndexMap, IndexSet};
|
use indexmap::{IndexMap, IndexSet};
|
||||||
|
use nu_errors::ShellError;
|
||||||
use nu_protocol::{Primitive, UntaggedValue, Value};
|
use nu_protocol::{Primitive, UntaggedValue, Value};
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
use std::{
|
use std::{
|
||||||
@ -8,7 +9,6 @@ use std::{
|
|||||||
io::{Error, ErrorKind, Result},
|
io::{Error, ErrorKind, Result},
|
||||||
path::PathBuf,
|
path::PathBuf,
|
||||||
};
|
};
|
||||||
use nu_errors::ShellError;
|
|
||||||
|
|
||||||
type EnvKey = String;
|
type EnvKey = String;
|
||||||
type EnvVal = OsString;
|
type EnvVal = OsString;
|
||||||
@ -83,22 +83,12 @@ impl DirectorySpecificEnvironment {
|
|||||||
Ok(keyvals_to_restore)
|
Ok(keyvals_to_restore)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn env_vars_to_add(&mut self) -> std::result::Result<IndexMap<EnvKey, EnvVal>, ShellError> {
|
pub fn env_vars_to_add(&mut self) -> std::io::Result<IndexMap<EnvKey, EnvVal>> {
|
||||||
let current_dir = std::env::current_dir()?;
|
let current_dir = std::env::current_dir()?;
|
||||||
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();
|
let mut vars_to_add = IndexMap::new();
|
||||||
|
|
||||||
// let mut file = OpenOptions::new()
|
// //WE CRASHING SOMEWHERE HERE
|
||||||
// .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.
|
//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 {
|
||||||
@ -108,42 +98,49 @@ impl DirectorySpecificEnvironment {
|
|||||||
|
|
||||||
toml_doc
|
toml_doc
|
||||||
.get("env")
|
.get("env")
|
||||||
.ok_or_else(|| Err(ShellError::untagged_runtime_error("env section missing")))?
|
.ok_or_else(|| Error::new(ErrorKind::InvalidData, "env section missing"))?
|
||||||
.as_table()
|
.as_table()
|
||||||
.ok_or_else(|| Err(ShellError::untagged_runtime_error("env section malformed")))?
|
.ok_or_else(|| Error::new(ErrorKind::InvalidData, "env section malformed"))?
|
||||||
.iter()
|
.iter()
|
||||||
.for_each(|(directory_env_key, directory_env_val)| {
|
.for_each(|(dir_env_key, dir_env_val)| {
|
||||||
if !vars_to_add.contains_key(directory_env_key) {
|
let dir_env_val: EnvVal = dir_env_val.as_str().unwrap().into();
|
||||||
let directory_env_val: EnvVal =
|
|
||||||
directory_env_val.as_str().unwrap().into();
|
|
||||||
|
|
||||||
//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.
|
||||||
if let Some(existing_val) = std::env::var_os(directory_env_key) {
|
if let Some(existing_val) = std::env::var_os(dir_env_key) {
|
||||||
if existing_val != directory_env_val {
|
// if existing_val != dir_env_val {
|
||||||
self.overwritten_env_vars
|
// self.overwritten_env_vars
|
||||||
.entry(wdir.to_path_buf())
|
// .entry(wdir.to_path_buf())
|
||||||
.or_insert(IndexMap::new())
|
// .or_insert(IndexMap::new())
|
||||||
.insert(directory_env_key.clone(), existing_val);
|
// .insert(dir_env_key.clone(), existing_val);
|
||||||
|
|
||||||
vars_to_add.insert(directory_env_key.clone(), directory_env_val);
|
// vars_to_add.insert(dir_env_key.clone(), dir_env_val);
|
||||||
}
|
// }
|
||||||
} else {
|
} else {
|
||||||
//Otherwise, we just track that we added it here
|
//Otherwise, we just track that we added it here
|
||||||
self.added_env_vars
|
self.added_env_vars
|
||||||
.entry(wdir.to_path_buf())
|
.entry(wdir.to_path_buf())
|
||||||
.or_insert(IndexSet::new())
|
.or_insert(IndexSet::new())
|
||||||
.insert(directory_env_key.clone());
|
.insert(dir_env_key.clone());
|
||||||
|
|
||||||
vars_to_add.insert(directory_env_key.clone(), directory_env_val);
|
vars_to_add.insert(dir_env_key.clone(), dir_env_val);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
working_dir = working_dir //Keep going up in the directory structure with .parent()
|
working_dir = working_dir //Keep going up in the directory structure with .parent()
|
||||||
.expect("This should not be None because of the while condition")
|
.expect("This should not be None because of the while condition")
|
||||||
.parent();
|
.parent();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let mut file = OpenOptions::new()
|
||||||
|
.write(true)
|
||||||
|
.append(true)
|
||||||
|
.create(true)
|
||||||
|
.open("toadd.txt")
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
write!(&mut file, "adding: {:?}\n", vars_to_add).unwrap();
|
||||||
|
|
||||||
Ok(vars_to_add)
|
Ok(vars_to_add)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
9
crates/nu-cli/src/env/environment.rs
vendored
9
crates/nu-cli/src/env/environment.rs
vendored
@ -5,6 +5,7 @@ use indexmap::{indexmap, IndexSet};
|
|||||||
use nu_protocol::{UntaggedValue, Value};
|
use nu_protocol::{UntaggedValue, Value};
|
||||||
use std::ffi::OsString;
|
use std::ffi::OsString;
|
||||||
use std::{fs::OpenOptions, fmt::Debug};
|
use std::{fs::OpenOptions, fmt::Debug};
|
||||||
|
use nu_errors::ShellError;
|
||||||
|
|
||||||
pub trait Env: Debug + Send {
|
pub trait Env: Debug + Send {
|
||||||
fn env(&self) -> Option<Value>;
|
fn env(&self) -> Option<Value>;
|
||||||
@ -71,10 +72,10 @@ impl Environment {
|
|||||||
// self.add_env(&k, &v.to_string_lossy(), true);
|
// self.add_env(&k, &v.to_string_lossy(), true);
|
||||||
// });
|
// });
|
||||||
|
|
||||||
// self.direnv.env_vars_to_add()?.iter().for_each(|(k, v)| {
|
self.direnv.env_vars_to_add()?.iter().for_each(|(k, v)| {
|
||||||
// std::env::set_var(k, v);
|
// std::env::set_var(k, v);
|
||||||
// self.add_env(&k, &v.to_string_lossy(), true);
|
self.add_env(&k, &v.to_string_lossy(), true);
|
||||||
// });
|
});
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
3
crates/nu-cli/src/env/environment_syncer.rs
vendored
3
crates/nu-cli/src/env/environment_syncer.rs
vendored
@ -45,6 +45,7 @@ impl EnvironmentSyncer {
|
|||||||
pub fn sync_env_vars(&mut self, ctx: &mut Context) -> Result<(), ShellError> {
|
pub fn sync_env_vars(&mut self, ctx: &mut Context) -> Result<(), ShellError> {
|
||||||
let mut environment = self.env.lock();
|
let mut environment = self.env.lock();
|
||||||
|
|
||||||
|
environment.maintain_directory_environment().ok();
|
||||||
if environment.env().is_some() {
|
if environment.env().is_some() {
|
||||||
for (name, value) in ctx.with_host(|host| host.vars()) {
|
for (name, value) in ctx.with_host(|host| host.vars()) {
|
||||||
if name != "path" && name != "PATH" {
|
if name != "path" && name != "PATH" {
|
||||||
@ -52,8 +53,6 @@ impl EnvironmentSyncer {
|
|||||||
// that aren't loaded from config.
|
// that aren't loaded from config.
|
||||||
environment.add_env(&name, &value, false);
|
environment.add_env(&name, &value, false);
|
||||||
|
|
||||||
environment.maintain_directory_environment()?;
|
|
||||||
|
|
||||||
// clear the env var from the session
|
// clear the env var from the session
|
||||||
// we are about to replace them
|
// we are about to replace them
|
||||||
ctx.with_host(|host| host.env_rm(std::ffi::OsString::from(name)));
|
ctx.with_host(|host| host.env_rm(std::ffi::OsString::from(name)));
|
||||||
|
Loading…
Reference in New Issue
Block a user