Nicer errors
This commit is contained in:
parent
b0db689065
commit
16b5e82a7e
|
@ -2,14 +2,13 @@ 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 nu_errors::ShellError;
|
||||||
use nu_source::Span;
|
|
||||||
use std::{
|
use std::{
|
||||||
collections::hash_map::DefaultHasher,
|
collections::hash_map::DefaultHasher,
|
||||||
ffi::OsString,
|
ffi::OsString,
|
||||||
fmt::Debug,
|
fmt::Debug,
|
||||||
hash::{Hash, Hasher},
|
hash::{Hash, Hasher},
|
||||||
io::{Error, ErrorKind},
|
io::{Error, ErrorKind},
|
||||||
path::PathBuf,
|
path::{Path, PathBuf},
|
||||||
};
|
};
|
||||||
|
|
||||||
type EnvKey = String;
|
type EnvKey = String;
|
||||||
|
@ -34,10 +33,7 @@ impl DirectorySpecificEnvironment {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn toml_if_directory_is_trusted(
|
fn toml_if_directory_is_trusted(&self, wdirenv: &PathBuf) -> Result<toml::Value, ShellError> {
|
||||||
&self,
|
|
||||||
wdirenv: PathBuf,
|
|
||||||
) -> Result<toml::Value, ShellError> {
|
|
||||||
if let Some(trusted) = &self.trusted {
|
if let Some(trusted) = &self.trusted {
|
||||||
let content = std::fs::read_to_string(&wdirenv)?;
|
let content = std::fs::read_to_string(&wdirenv)?;
|
||||||
let mut hasher = DefaultHasher::new();
|
let mut hasher = DefaultHasher::new();
|
||||||
|
@ -50,7 +46,8 @@ impl DirectorySpecificEnvironment {
|
||||||
))
|
))
|
||||||
})?);
|
})?);
|
||||||
}
|
}
|
||||||
return Err(ShellError::untagged_runtime_error("Found untrusted .nu-env file in this directory. Run 'autoenv trust' and restart nushell to allow it. This needs to be done after each change to the file."));
|
return Err(ShellError::untagged_runtime_error(
|
||||||
|
format!("{:?} is untrusted. Run 'autoenv trust {:?}' and restart nushell to trust it.\nThis needs to be done after each change to the file.", wdirenv, wdirenv.parent().unwrap_or_else(|| &Path::new("")))));
|
||||||
}
|
}
|
||||||
Err(ShellError::untagged_runtime_error("No trusted directories"))
|
Err(ShellError::untagged_runtime_error("No trusted directories"))
|
||||||
}
|
}
|
||||||
|
@ -64,12 +61,16 @@ impl DirectorySpecificEnvironment {
|
||||||
while let Some(wdir) = working_dir {
|
while let Some(wdir) = working_dir {
|
||||||
let wdirenv = wdir.join(".nu-env");
|
let wdirenv = wdir.join(".nu-env");
|
||||||
if wdirenv.exists() {
|
if wdirenv.exists() {
|
||||||
let toml_doc = self.toml_if_directory_is_trusted(wdirenv)?;
|
let toml_doc = self.toml_if_directory_is_trusted(&wdirenv)?;
|
||||||
toml_doc
|
toml_doc
|
||||||
.get("env")
|
.get("env")
|
||||||
.ok_or_else(|| Error::new(ErrorKind::InvalidData, "env section missing in .nu-env"))?
|
.ok_or_else(|| {
|
||||||
|
Error::new(ErrorKind::InvalidData, format!("[env] section missing in {:?}", wdirenv))
|
||||||
|
})?
|
||||||
.as_table()
|
.as_table()
|
||||||
.ok_or_else(|| Error::new(ErrorKind::InvalidData, "malformed env section in .nu-env"))?
|
.ok_or_else(|| {
|
||||||
|
Error::new(ErrorKind::InvalidData, format!("[env] section malformed in {:?}", wdirenv))
|
||||||
|
})?
|
||||||
.iter()
|
.iter()
|
||||||
.for_each(|(dir_env_key, dir_env_val)| {
|
.for_each(|(dir_env_key, dir_env_val)| {
|
||||||
let dir_env_val: EnvVal = dir_env_val.as_str().unwrap().into();
|
let dir_env_val: EnvVal = dir_env_val.as_str().unwrap().into();
|
||||||
|
|
1
crates/nu-cli/src/env/environment_syncer.rs
vendored
1
crates/nu-cli/src/env/environment_syncer.rs
vendored
|
@ -3,7 +3,6 @@ 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;
|
|
||||||
use nu_source::Text;
|
use nu_source::Text;
|
||||||
|
|
||||||
pub struct EnvironmentSyncer {
|
pub struct EnvironmentSyncer {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user