Debug by logging to file

This commit is contained in:
Sam Hedin 2020-06-04 23:17:55 +02:00
parent fe4a51eef4
commit 24d2e88e0f

View File

@ -4,9 +4,11 @@ use nu_protocol::{UntaggedValue, Value};
use std::collections::{HashMap}; use std::collections::{HashMap};
use std::ffi::OsString; use std::ffi::OsString;
use std::fmt::Debug; use std::fmt::Debug;
use std::fs::OpenOptions;
use std::fs::File; use std::fs::File;
use std::io::prelude::*; use std::io::prelude::*;
use std::path::PathBuf; use std::path::PathBuf;
use std::io::Write;
pub trait Env: Debug + Send { pub trait Env: Debug + Send {
fn env(&self) -> Option<Value>; fn env(&self) -> Option<Value>;
@ -68,10 +70,11 @@ impl Environment {
//TODO: handle errors //TODO: handle errors
pub fn maintain_nurc_environment_vars(&mut self) { pub fn maintain_nurc_environment_vars(&mut self) {
match self.clear_vars_from_unvisited_dirs() { match self.add_nurc() {
_ => {} _ => {}
}; };
match self.add_nurc() {
match self.clear_vars_from_unvisited_dirs() {
_ => {} _ => {}
}; };
} }
@ -84,6 +87,14 @@ impl Environment {
let toml_doc = contents.parse::<toml::Value>().unwrap(); let toml_doc = contents.parse::<toml::Value>().unwrap();
let nurc_vars = toml_doc.get("env").unwrap().as_table().unwrap(); let nurc_vars = toml_doc.get("env").unwrap().as_table().unwrap();
let mut file = OpenOptions::new()
.write(true)
.append(true)
.create(true)
.open("env.txt").unwrap();
write!(&mut file, "keys to add: {:?}\n", nurc_vars).unwrap();
nurc_vars.iter().for_each(|(k, v)| { nurc_vars.iter().for_each(|(k, v)| {
self.add_env(k, v.as_str().unwrap()); self.add_env(k, v.as_str().unwrap());
}); });
@ -98,6 +109,7 @@ impl Environment {
//If the user has left directories which added env vars through .nurc, we clear those vars //If the user has left directories which added env vars through .nurc, we clear those vars
//For each directory d in nurc_env_vars: //For each directory d in nurc_env_vars:
//if current_dir does not have d as a parent (possibly recursive), the vars set by d should be removed //if current_dir does not have d as a parent (possibly recursive), the vars set by d should be removed
//TODO: Seems like vars are re-added immediately after being removed
pub fn clear_vars_from_unvisited_dirs(&mut self) -> std::io::Result<()> { pub fn clear_vars_from_unvisited_dirs(&mut self) -> std::io::Result<()> {
let current_dir = std::env::current_dir()?; let current_dir = std::env::current_dir()?;
@ -121,6 +133,13 @@ impl Environment {
} }
} }
let mut file = OpenOptions::new()
.write(true)
.append(true)
.create(true)
.open("env.txt")?;
write!(&mut file, "keys to remove: {:?}\n", vars_to_delete).unwrap();
vars_to_delete.iter().for_each(|env_var| self.remove_env(env_var)); vars_to_delete.iter().for_each(|env_var| self.remove_env(env_var));
self.nurc_env_keys = new_nurc_env_vars; self.nurc_env_keys = new_nurc_env_vars;
@ -133,6 +152,7 @@ impl Environment {
tag: _, tag: _,
}) = &mut self.environment_vars }) = &mut self.environment_vars
{ {
envs.entries.remove(key); envs.entries.remove(key);
} }
} }