From 48e4bb60d0a5e0060b43d503712fed102c69f3ce Mon Sep 17 00:00:00 2001 From: Sam Hedin Date: Thu, 4 Jun 2020 02:14:49 +0200 Subject: [PATCH] Parse .nurc file --- crates/nu-cli/src/env/environment.rs | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/crates/nu-cli/src/env/environment.rs b/crates/nu-cli/src/env/environment.rs index 9194580a0a..9c85c2c4f2 100644 --- a/crates/nu-cli/src/env/environment.rs +++ b/crates/nu-cli/src/env/environment.rs @@ -4,7 +4,7 @@ use nu_protocol::{UntaggedValue, Value}; use std::ffi::OsString; use std::fmt::Debug; use std::fs::File; -use std::io::Write; +use std::io::prelude::*; pub trait Env: Debug + Send { fn env(&self) -> Option; @@ -57,14 +57,18 @@ impl Environment { } //Add env vars specified in the current dirs .nurc, if it exists. - pub fn add_nurc(&mut self) { - let key = "envtest"; - let value = "I am here!"; + //TODO: Remove env vars after leaving the directory. Save added vars in env? + //TODO: Add authentication by saving the path to the .nurc file in some variable? + 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(); - write!(&mut file, "{:?}", "somedata").unwrap(); + let value = contents.parse::().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(&mut self, configuration: &T) {