This commit is contained in:
Sam Hedin 2020-06-22 16:50:52 +02:00
parent 0215e4c1b6
commit 59cd51b132
7 changed files with 30 additions and 26 deletions

View File

@ -238,7 +238,7 @@ pub fn create_default_context(
syncer.load_environment();
let mut context = Context::basic()?;
syncer.sync_env_vars(&mut context)?;
syncer.sync_env_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
// FIXME: we probably want to be a bit more graceful if we can't set the environment
syncer.reload();
syncer.sync_env_vars(&mut context)?;
syncer.sync_env_vars(&mut context);
syncer.sync_path_vars(&mut context);
match line {

View File

@ -4,8 +4,8 @@ use nu_errors::ShellError;
use nu_protocol::{ReturnSuccess, Signature, UntaggedValue};
use serde::Deserialize;
use serde::Serialize;
use std::path::PathBuf;
use std::io::Read;
use std::path::PathBuf;
pub struct Autoenv;
@ -26,7 +26,7 @@ impl Trusted {
.read(true)
.create(true)
.write(true)
.open(config_path.clone())
.open(config_path)
{
Ok(p) => p,
Err(_) => {

View File

@ -5,7 +5,7 @@ use nu_errors::ShellError;
use nu_protocol::SyntaxShape;
use nu_protocol::{Primitive, ReturnSuccess, Signature, UntaggedValue, Value};
use std::hash::{Hash, Hasher};
use std::{fs, collections::hash_map::DefaultHasher, path::PathBuf};
use std::{collections::hash_map::DefaultHasher, fs, path::PathBuf};
pub struct AutoenvTrust;
#[async_trait]

View File

@ -1,3 +1,4 @@
use super::autoenv::Trusted;
use crate::commands::WholeStreamCommand;
use crate::prelude::*;
use nu_errors::ShellError;
@ -5,10 +6,8 @@ use nu_protocol::SyntaxShape;
use nu_protocol::{Primitive, ReturnSuccess, Signature, UntaggedValue, Value};
use std::io::Read;
use std::{fs, path::PathBuf};
use super::autoenv::Trusted;
pub struct AutoenvUnTrust;
#[async_trait]
impl WholeStreamCommand for AutoenvUnTrust {
fn name(&self) -> &str {
@ -16,7 +15,11 @@ impl WholeStreamCommand for AutoenvUnTrust {
}
fn signature(&self) -> Signature {
Signature::build("autoenv untrust").optional("dir", SyntaxShape::String, "Directory to disallow")
Signature::build("autoenv untrust").optional(
"dir",
SyntaxShape::String,
"Directory to disallow",
)
}
fn usage(&self) -> &str {
@ -28,7 +31,6 @@ impl WholeStreamCommand for AutoenvUnTrust {
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
let tag = args.call_info.name_tag.clone();
let file_to_untrust = match args.call_info.evaluate(registry).await?.args.nth(0) {
Some(Value {
@ -52,19 +54,21 @@ impl WholeStreamCommand for AutoenvUnTrust {
.read(true)
.create(true)
.write(true)
.open(config_path.clone()) {
Ok(p) => p,
Err(_) => {
return Err(ShellError::untagged_runtime_error("Couldn't open nu-env.toml"));
}
};
.open(config_path.clone())
{
Ok(p) => p,
Err(_) => {
return Err(ShellError::untagged_runtime_error(
"Couldn't open nu-env.toml",
));
}
};
let mut doc = String::new();
file.read_to_string(&mut doc)?;
let mut allowed: Trusted = toml::from_str(doc.as_str()).unwrap_or_else(|_| Trusted::new());
let file_to_untrust = file_to_untrust.to_string_lossy().to_string();
allowed.files.remove(&file_to_untrust);
@ -81,4 +85,4 @@ impl WholeStreamCommand for AutoenvUnTrust {
fn examples(&self) -> Vec<Example> {
Vec::new()
}
}
}

View File

@ -2,11 +2,12 @@ use crate::commands::{self, autoenv::Trusted};
use commands::autoenv;
use indexmap::{IndexMap, IndexSet};
use std::{
collections::hash_map::DefaultHasher,
ffi::OsString,
fmt::Debug,
hash::{Hash, Hasher},
io::{Error, ErrorKind},
path::PathBuf,
hash::{Hash, Hasher}, collections::hash_map::DefaultHasher
};
type EnvKey = String;
@ -39,9 +40,10 @@ impl DirectorySpecificEnvironment {
let mut hasher = DefaultHasher::new();
content.hash(&mut hasher);
if trusted.files.get(wdirenv.to_str().unwrap())
== Some(&hasher.finish().to_string()) {
return Ok(content.parse::<toml::Value>()?);
}
== Some(&hasher.finish().to_string())
{
return Ok(content.parse::<toml::Value>()?);
}
}
}
Err(Error::new(ErrorKind::Other, "No trusted directories"))
@ -55,7 +57,7 @@ impl DirectorySpecificEnvironment {
//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 let Ok(toml_doc) = self.toml_if_directory_is_trusted(wdir.to_path_buf()) {
toml_doc
toml_doc
.get("env")
.ok_or_else(|| Error::new(ErrorKind::InvalidData, "env section missing"))?
.as_table()

View File

@ -47,7 +47,7 @@ impl Environment {
}
}
pub fn from_config<T: Conf>(configuration: &T) -> Environment {
pub fn from_config<T: Conf>(configuration: &T) -> Environment {
let env = configuration.env();
let path = configuration.path();
Environment {

View File

@ -3,7 +3,6 @@ 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>>>,
@ -42,7 +41,7 @@ impl EnvironmentSyncer {
environment.morph(&*self.config);
}
pub fn sync_env_vars(&mut self, ctx: &mut Context) -> Result<(), ShellError> {
pub fn sync_env_vars(&mut self, ctx: &mut Context) {
let mut environment = self.env.lock();
environment.maintain_directory_environment().ok();
@ -72,7 +71,6 @@ impl EnvironmentSyncer {
}
}
}
Ok(())
}
pub fn sync_path_vars(&mut self, ctx: &mut Context) {