Use trust functionality when setting vars

This commit is contained in:
Sam Hedin 2020-06-22 16:18:36 +02:00
parent 0d44ddd798
commit a53368ccc8

View File

@ -1,12 +1,13 @@
use crate::commands::{self, autoenv::Trusted};
use commands::autoenv;
use indexmap::{IndexMap, IndexSet}; use indexmap::{IndexMap, IndexSet};
use std::{ use std::{
ffi::OsString, ffi::OsString,
fmt::Debug, fmt::Debug,
io::{Error, ErrorKind}, io::{Error, ErrorKind},
path::PathBuf, path::PathBuf,
hash::{Hash, Hasher}, collections::hash_map::DefaultHasher
}; };
use crate::commands::{autoenv::Trusted, self};
use commands::autoenv;
type EnvKey = String; type EnvKey = String;
type EnvVal = OsString; type EnvVal = OsString;
@ -22,7 +23,7 @@ impl DirectorySpecificEnvironment {
pub fn new() -> DirectorySpecificEnvironment { pub fn new() -> DirectorySpecificEnvironment {
let trusted = match autoenv::Trusted::read_trusted() { let trusted = match autoenv::Trusted::read_trusted() {
Ok(t) => Some(t), Ok(t) => Some(t),
Err(_) => None Err(_) => None,
}; };
DirectorySpecificEnvironment { DirectorySpecificEnvironment {
trusted, trusted,
@ -30,20 +31,21 @@ impl DirectorySpecificEnvironment {
} }
} }
// fn check_hashes(&self, wdir: PathBuf) -> std::io::Result<bool> { fn toml_if_trusted(&self, mut wdirenv: PathBuf) -> std::io::Result<toml::Value> {
// if let Some(trusted) = &self.trusted { if let Some(trusted) = &self.trusted {
// let wdirenv = wdir.join(".nu-env"); wdirenv.push(".nu-env");
// if wdirenv.exists() { if wdirenv.exists() {
// 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();
// content.hash(&mut hasher); content.hash(&mut hasher);
// return Ok(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>()?);
// } }
}
// Ok(true) }
// } Err(Error::new(ErrorKind::Other, "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) -> std::io::Result<IndexMap<EnvKey, EnvVal>> {
let current_dir = std::env::current_dir()?; let current_dir = std::env::current_dir()?;
@ -52,11 +54,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. //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 self.allowed_directories.contains(wdir) { if let Ok(toml_doc) = self.toml_if_trusted(wdir.to_path_buf()) {
if true {
let toml_doc = std::fs::read_to_string(wdir.join(".nu-env").as_path())?
.parse::<toml::Value>()?;
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"))?