Parse .nurc file

This commit is contained in:
Sam Hedin 2020-06-04 02:14:49 +02:00
parent 8eaaddca8f
commit 48e4bb60d0

View File

@ -4,7 +4,7 @@ use nu_protocol::{UntaggedValue, Value};
use std::ffi::OsString; use std::ffi::OsString;
use std::fmt::Debug; use std::fmt::Debug;
use std::fs::File; use std::fs::File;
use std::io::Write; use std::io::prelude::*;
pub trait Env: Debug + Send { pub trait Env: Debug + Send {
fn env(&self) -> Option<Value>; fn env(&self) -> Option<Value>;
@ -57,14 +57,18 @@ impl Environment {
} }
//Add env vars specified in the current dirs .nurc, if it exists. //Add env vars specified in the current dirs .nurc, if it exists.
pub fn add_nurc(&mut self) { //TODO: Remove env vars after leaving the directory. Save added vars in env?
let key = "envtest"; //TODO: Add authentication by saving the path to the .nurc file in some variable?
let value = "I am here!"; pub fn add_nurc(&mut self) -> std::io::Result<()> {
let mut file = File::open(".nurc")?;
let mut contents = String::new();
file.read_to_string(&mut contents)?;
let mut file = File::create("env.txt").unwrap(); let value = contents.parse::<toml::Value>().unwrap();
write!(&mut file, "{:?}", "somedata").unwrap(); let nurc_vars = value.get("env").unwrap().as_table().unwrap();
self.add_env(key, value); nurc_vars.iter().for_each(|(k, v)| self.add_env(k, v.as_str().unwrap()));
Ok(())
} }
pub fn morph<T: Conf>(&mut self, configuration: &T) { pub fn morph<T: Conf>(&mut self, configuration: &T) {