From 0f0485957af9ff619b1c6caf3e106b0f65ed570f Mon Sep 17 00:00:00 2001 From: Sam Hedin Date: Fri, 5 Jun 2020 04:00:52 +0200 Subject: [PATCH] Parse config directories --- crates/nu-cli/src/cli.rs | 25 ++------- crates/nu-cli/src/data/config/conf.rs | 6 ++- crates/nu-cli/src/data/config/nuconfig.rs | 12 +++++ crates/nu-cli/src/env/environment.rs | 4 -- crates/nu-cli/src/env/environment_syncer.rs | 56 +++++++++++++++++++-- 5 files changed, 74 insertions(+), 29 deletions(-) diff --git a/crates/nu-cli/src/cli.rs b/crates/nu-cli/src/cli.rs index 2715396fd..c0d8a2901 100644 --- a/crates/nu-cli/src/cli.rs +++ b/crates/nu-cli/src/cli.rs @@ -22,8 +22,8 @@ use rustyline::{ KeyPress, Movement, Word, }; use std::error::Error; -use std::fs::File; -use std::io::{BufRead, BufReader, Write, Read}; +use std::fs::{File, OpenOptions}; +use std::io::{BufRead, BufReader, Read, Write}; use std::iter::Iterator; use std::path::{Path, PathBuf}; use std::sync::atomic::Ordering; @@ -741,23 +741,6 @@ enum LineResult { Break, } -//TODO: Add authentication by saving the path to the .nurc file in some variable? -//For directory wd in whitelisted directories -//if current directory is wd or subdir to wd, add env vars from .nurc in wd -pub fn add_nurc(env: &mut IndexMap) -> std::io::Result<()> { - let mut file = File::open(".nurc")?; - let mut contents = String::new(); - file.read_to_string(&mut contents)?; - - let toml_doc = contents.parse::().unwrap(); - let nurc_vars = toml_doc.get("env").unwrap().as_table().unwrap(); - - nurc_vars.iter().for_each(|(k, v)| { - env.insert(k.clone(), v.as_str().unwrap().to_string()); - }); - - Ok(()) -} /// Process the line by parsing the text to turn it into commands, classify those commands so that we understand what is being called in the pipeline, and then run this pipeline async fn process_line( readline: Result, @@ -906,8 +889,8 @@ async fn process_line( classified_block.block.expand_it_usage(); trace!("{:#?}", classified_block); - let mut env = ctx.get_env(); - add_nurc(&mut env); + let env = ctx.get_env(); + match run_block( &classified_block.block, ctx, diff --git a/crates/nu-cli/src/data/config/conf.rs b/crates/nu-cli/src/data/config/conf.rs index b3504441e..efd44d396 100644 --- a/crates/nu-cli/src/data/config/conf.rs +++ b/crates/nu-cli/src/data/config/conf.rs @@ -4,7 +4,7 @@ use std::fmt::Debug; pub trait Conf: Debug + Send { fn env(&self) -> Option; fn path(&self) -> Option; - + fn direnv_whitelist(&self) -> Option; fn reload(&self); } @@ -13,6 +13,10 @@ impl Conf for Box { (**self).env() } + fn direnv_whitelist(&self) -> Option { + (**self).direnv_whitelist() + } + fn path(&self) -> Option { (**self).path() } diff --git a/crates/nu-cli/src/data/config/nuconfig.rs b/crates/nu-cli/src/data/config/nuconfig.rs index 3444e1f7a..97e486fb7 100644 --- a/crates/nu-cli/src/data/config/nuconfig.rs +++ b/crates/nu-cli/src/data/config/nuconfig.rs @@ -20,6 +20,10 @@ impl Conf for NuConfig { self.path() } + fn direnv_whitelist(&self) -> Option { + self.direnv_whitelist() + } + fn reload(&self) { let mut vars = self.vars.lock(); @@ -52,6 +56,14 @@ impl NuConfig { None } + pub fn direnv_whitelist(&self) -> Option { + let vars = self.vars.lock(); + if let Some(dirs) = vars.get("nurc_dirs") { + return Some(dirs.clone()); + } + None + } + pub fn path(&self) -> Option { let vars = self.vars.lock(); diff --git a/crates/nu-cli/src/env/environment.rs b/crates/nu-cli/src/env/environment.rs index 0f0cbc234..9b586eded 100644 --- a/crates/nu-cli/src/env/environment.rs +++ b/crates/nu-cli/src/env/environment.rs @@ -1,12 +1,8 @@ use crate::data::config::Conf; use indexmap::{indexmap, IndexSet}; use nu_protocol::{UntaggedValue, Value}; -use std::collections::{HashMap}; use std::ffi::OsString; use std::fmt::Debug; -use std::fs::File; -use std::io::prelude::*; -use std::path::PathBuf; pub trait Env: Debug + Send { fn env(&self) -> Option; diff --git a/crates/nu-cli/src/env/environment_syncer.rs b/crates/nu-cli/src/env/environment_syncer.rs index 2fb0f87a1..31f72c2c3 100644 --- a/crates/nu-cli/src/env/environment_syncer.rs +++ b/crates/nu-cli/src/env/environment_syncer.rs @@ -1,8 +1,14 @@ use crate::context::Context; use crate::data::config::{Conf, NuConfig}; use crate::env::environment::{Env, Environment}; +use nu_protocol::{UntaggedValue, Value, Primitive}; use parking_lot::Mutex; -use std::sync::Arc; +use std::io::Read; +use std::io::Write; +use std::{ + fs::{File, OpenOptions}, + sync::Arc, path::PathBuf, +}; pub struct EnvironmentSyncer { pub env: Arc>>, @@ -41,9 +47,54 @@ impl EnvironmentSyncer { environment.morph(&*self.config); } + //TODO: Add authentication by saving the path to the .nurc file in some variable? + //For directory wd in whitelisted directories + //if current directory is wd or subdir to wd, add env vars from .nurc in wd + pub fn add_nurc(&self) -> std::io::Result<()> { + let mut file = OpenOptions::new() + .write(true) + .append(true) + .create(true) + .open("vars.txt")?; + + let conf = Arc::clone(&self.config); + + let mut directories = vec![]; + if let Some(Value { + value: UntaggedValue::Table(ref directories_as_values), + tag: _, + }) = conf.direnv_whitelist() + { + for dirval in directories_as_values { + if let Value { + value: UntaggedValue::Primitive(Primitive::String(ref dir)), + tag: _, + } = dirval { + let path = PathBuf::from(dir); + directories.push(path); + } + } + }; + + write!(&mut file, "variables so far: {:?}\n", directories).unwrap(); + + // let mut file = File::open(".nurc")?; + // let mut contents = String::new(); + // file.read_to_string(&mut contents)?; + + // let toml_doc = contents.parse::().unwrap(); + // let nurc_vars = toml_doc.get("env").unwrap().as_table().unwrap(); + + // nurc_vars.iter().for_each(|(k, v)| { + // env.insert(k.clone(), v.as_str().unwrap().to_string()); + // }); + + Ok(()) + } + pub fn sync_env_vars(&mut self, ctx: &mut Context) { let mut environment = self.env.lock(); - + self.add_nurc(); if environment.env().is_some() { for (name, value) in ctx.with_host(|host| host.vars()) { if name != "path" && name != "PATH" { @@ -70,7 +121,6 @@ impl EnvironmentSyncer { } } } - } pub fn sync_path_vars(&mut self, ctx: &mut Context) {