mirror of
https://github.com/nushell/nushell.git
synced 2024-11-26 18:33:50 +01:00
Non-working errors
This commit is contained in:
parent
632c6d1cd9
commit
3d213a63c5
@ -238,7 +238,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);
|
||||||
|
|
||||||
{
|
{
|
||||||
@ -677,7 +677,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 {
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
use crate::commands::{self, autoenv::Trusted};
|
use crate::commands::{self, autoenv::Trusted};
|
||||||
use commands::autoenv;
|
use commands::autoenv;
|
||||||
use indexmap::{IndexMap, IndexSet};
|
use indexmap::{IndexMap, IndexSet};
|
||||||
|
use nu_errors::ShellError;
|
||||||
use std::{
|
use std::{
|
||||||
collections::hash_map::DefaultHasher,
|
collections::hash_map::DefaultHasher,
|
||||||
ffi::OsString,
|
ffi::OsString,
|
||||||
@ -32,7 +33,10 @@ impl DirectorySpecificEnvironment {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn toml_if_directory_is_trusted(&self, mut wdirenv: PathBuf) -> std::io::Result<toml::Value> {
|
fn toml_if_directory_is_trusted(
|
||||||
|
&self,
|
||||||
|
mut wdirenv: PathBuf,
|
||||||
|
) -> Result<toml::Value, ShellError> {
|
||||||
if let Some(trusted) = &self.trusted {
|
if let Some(trusted) = &self.trusted {
|
||||||
wdirenv.push(".nu-env");
|
wdirenv.push(".nu-env");
|
||||||
if wdirenv.exists() {
|
if wdirenv.exists() {
|
||||||
@ -42,21 +46,29 @@ impl DirectorySpecificEnvironment {
|
|||||||
if trusted.files.get(wdirenv.to_str().unwrap())
|
if trusted.files.get(wdirenv.to_str().unwrap())
|
||||||
== Some(&hasher.finish().to_string())
|
== Some(&hasher.finish().to_string())
|
||||||
{
|
{
|
||||||
return Ok(content.parse::<toml::Value>()?);
|
return Ok(content.parse::<toml::Value>().or_else(|_| {
|
||||||
|
Err(
|
||||||
|
ShellError::untagged_runtime_error(
|
||||||
|
"Could not parse .nu-env file. Is it well-formed?",
|
||||||
|
),
|
||||||
|
)
|
||||||
|
})?);
|
||||||
|
} else {
|
||||||
|
return Err(ShellError::untagged_runtime_error("Found .nu-env file in this directory, but it is not trusted. You can run 'autoenv trust' to allow it. This needs to be done after each change to the file"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(Error::new(ErrorKind::Other, "No trusted directories"))
|
Err(ShellError::untagged_runtime_error("No trusted directories"))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn env_vars_to_add(&mut self) -> std::io::Result<IndexMap<EnvKey, EnvVal>> {
|
pub fn env_vars_to_add(&mut self) -> Result<IndexMap<EnvKey, EnvVal>, ShellError> {
|
||||||
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();
|
||||||
|
|
||||||
//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 let Ok(toml_doc) = self.toml_if_directory_is_trusted(wdir.to_path_buf()) {
|
let toml_doc = self.toml_if_directory_is_trusted(wdir.to_path_buf())?;
|
||||||
toml_doc
|
toml_doc
|
||||||
.get("env")
|
.get("env")
|
||||||
.ok_or_else(|| Error::new(ErrorKind::InvalidData, "env section missing"))?
|
.ok_or_else(|| Error::new(ErrorKind::InvalidData, "env section missing"))?
|
||||||
@ -76,7 +88,6 @@ impl DirectorySpecificEnvironment {
|
|||||||
.insert(dir_env_key.clone());
|
.insert(dir_env_key.clone());
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
|
||||||
|
|
||||||
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")
|
||||||
@ -88,7 +99,7 @@ impl DirectorySpecificEnvironment {
|
|||||||
|
|
||||||
//If the user has left directories which added env vars through .nu, we clear those vars
|
//If the user has left directories which added env vars through .nu, we clear those vars
|
||||||
//once they are marked for deletion, remove them from added_env_vars
|
//once they are marked for deletion, remove them from added_env_vars
|
||||||
pub fn env_vars_to_delete(&mut self) -> std::io::Result<IndexSet<EnvKey>> {
|
pub fn env_vars_to_delete(&mut self) -> Result<IndexSet<EnvKey>, ShellError> {
|
||||||
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());
|
||||||
|
|
||||||
|
3
crates/nu-cli/src/env/environment.rs
vendored
3
crates/nu-cli/src/env/environment.rs
vendored
@ -4,6 +4,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::fmt::Debug;
|
use std::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>;
|
||||||
@ -57,7 +58,7 @@ impl Environment {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn maintain_directory_environment(&mut self) -> std::io::Result<()> {
|
pub fn maintain_directory_environment(&mut self) -> Result<(), ShellError> {
|
||||||
self.direnv.env_vars_to_delete()?.iter().for_each(|k| {
|
self.direnv.env_vars_to_delete()?.iter().for_each(|k| {
|
||||||
self.remove_env(&k);
|
self.remove_env(&k);
|
||||||
});
|
});
|
||||||
|
4
crates/nu-cli/src/env/environment_syncer.rs
vendored
4
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 crate::env::environment::{Env, Environment};
|
||||||
use parking_lot::Mutex;
|
use parking_lot::Mutex;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
use nu_errors::ShellError;
|
||||||
|
|
||||||
pub struct EnvironmentSyncer {
|
pub struct EnvironmentSyncer {
|
||||||
pub env: Arc<Mutex<Box<Environment>>>,
|
pub env: Arc<Mutex<Box<Environment>>>,
|
||||||
@ -41,7 +42,7 @@ impl EnvironmentSyncer {
|
|||||||
environment.morph(&*self.config);
|
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();
|
let mut environment = self.env.lock();
|
||||||
|
|
||||||
environment.maintain_directory_environment().ok();
|
environment.maintain_directory_environment().ok();
|
||||||
@ -71,6 +72,7 @@ impl EnvironmentSyncer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn sync_path_vars(&mut self, ctx: &mut Context) {
|
pub fn sync_path_vars(&mut self, ctx: &mut Context) {
|
||||||
|
Loading…
Reference in New Issue
Block a user