Parse config directories

This commit is contained in:
Sam Hedin 2020-06-05 04:00:52 +02:00
parent e97e883d1f
commit 0f0485957a
5 changed files with 74 additions and 29 deletions

View File

@ -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<String, String>) -> 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::<toml::Value>().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<String, ReadlineError>,
@ -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,

View File

@ -4,7 +4,7 @@ use std::fmt::Debug;
pub trait Conf: Debug + Send {
fn env(&self) -> Option<Value>;
fn path(&self) -> Option<Value>;
fn direnv_whitelist(&self) -> Option<Value>;
fn reload(&self);
}
@ -13,6 +13,10 @@ impl Conf for Box<dyn Conf> {
(**self).env()
}
fn direnv_whitelist(&self) -> Option<Value> {
(**self).direnv_whitelist()
}
fn path(&self) -> Option<Value> {
(**self).path()
}

View File

@ -20,6 +20,10 @@ impl Conf for NuConfig {
self.path()
}
fn direnv_whitelist(&self) -> Option<Value> {
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<Value> {
let vars = self.vars.lock();
if let Some(dirs) = vars.get("nurc_dirs") {
return Some(dirs.clone());
}
None
}
pub fn path(&self) -> Option<Value> {
let vars = self.vars.lock();

View File

@ -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<Value>;

View File

@ -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<Mutex<Box<Environment>>>,
@ -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::<toml::Value>().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) {