mirror of
https://github.com/nushell/nushell.git
synced 2025-05-29 22:29:06 +02:00
I am in a state of distress
This commit is contained in:
parent
0f0485957a
commit
da2751da54
53
crates/nu-cli/src/env/environment_syncer.rs
vendored
53
crates/nu-cli/src/env/environment_syncer.rs
vendored
@ -1,13 +1,14 @@
|
|||||||
use crate::context::Context;
|
use crate::context::Context;
|
||||||
use crate::data::config::{Conf, NuConfig};
|
use crate::data::config::{Conf, NuConfig};
|
||||||
use crate::env::environment::{Env, Environment};
|
use crate::env::environment::{Env, Environment};
|
||||||
use nu_protocol::{UntaggedValue, Value, Primitive};
|
use nu_protocol::{Primitive, UntaggedValue, Value};
|
||||||
use parking_lot::Mutex;
|
use parking_lot::Mutex;
|
||||||
use std::io::Read;
|
use std::io::Read;
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
use std::{
|
use std::{
|
||||||
fs::{File, OpenOptions},
|
fs::{File, OpenOptions},
|
||||||
sync::Arc, path::PathBuf,
|
path::{Path, PathBuf},
|
||||||
|
sync::Arc,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub struct EnvironmentSyncer {
|
pub struct EnvironmentSyncer {
|
||||||
@ -47,19 +48,20 @@ impl EnvironmentSyncer {
|
|||||||
environment.morph(&*self.config);
|
environment.morph(&*self.config);
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO: Add authentication by saving the path to the .nurc file in some variable?
|
|
||||||
//For directory wd in whitelisted directories
|
//For directory wd in whitelisted directories
|
||||||
//if current directory is wd or subdir to wd, add env vars from .nurc in wd
|
//if current directory is wd or subdir to wd, add env vars from .nurc in wd
|
||||||
|
//vars: envtest, moretest, anothertest
|
||||||
pub fn add_nurc(&self) -> std::io::Result<()> {
|
pub fn add_nurc(&self) -> std::io::Result<()> {
|
||||||
|
let mut environment = self.env.lock();
|
||||||
let mut file = OpenOptions::new()
|
let mut file = OpenOptions::new()
|
||||||
.write(true)
|
.write(true)
|
||||||
.append(true)
|
.append(true)
|
||||||
.create(true)
|
.create(true)
|
||||||
.open("vars.txt")?;
|
.open("dirs.txt")?;
|
||||||
|
|
||||||
let conf = Arc::clone(&self.config);
|
let conf = Arc::clone(&self.config);
|
||||||
|
|
||||||
let mut directories = vec![];
|
let mut directories = vec![]; //TODO: sort the directories to achieve desired functionality about overwrites
|
||||||
if let Some(Value {
|
if let Some(Value {
|
||||||
value: UntaggedValue::Table(ref directories_as_values),
|
value: UntaggedValue::Table(ref directories_as_values),
|
||||||
tag: _,
|
tag: _,
|
||||||
@ -69,32 +71,47 @@ impl EnvironmentSyncer {
|
|||||||
if let Value {
|
if let Value {
|
||||||
value: UntaggedValue::Primitive(Primitive::String(ref dir)),
|
value: UntaggedValue::Primitive(Primitive::String(ref dir)),
|
||||||
tag: _,
|
tag: _,
|
||||||
} = dirval {
|
} = dirval
|
||||||
let path = PathBuf::from(dir);
|
{
|
||||||
directories.push(path);
|
directories.push(PathBuf::from(&dir));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
directories.sort();
|
||||||
|
|
||||||
write!(&mut file, "variables so far: {:?}\n", directories).unwrap();
|
write!(&mut file, "sorted dirs: {:?}\n", directories).unwrap();
|
||||||
|
|
||||||
// let mut file = File::open(".nurc")?;
|
let current_dir = std::env::current_dir()?;
|
||||||
// let mut contents = String::new();
|
|
||||||
// file.read_to_string(&mut contents)?;
|
|
||||||
|
|
||||||
// let toml_doc = contents.parse::<toml::Value>().unwrap();
|
for mut dir in directories {
|
||||||
// let nurc_vars = toml_doc.get("env").unwrap().as_table().unwrap();
|
let mut working_dir = Some(current_dir.as_path());
|
||||||
|
|
||||||
// nurc_vars.iter().for_each(|(k, v)| {
|
while working_dir.is_some() {
|
||||||
// env.insert(k.clone(), v.as_str().unwrap().to_string());
|
if working_dir.unwrap() == dir.as_path() {
|
||||||
// });
|
dir.push(".nurc");
|
||||||
|
let mut file = File::open(dir.as_path())?;
|
||||||
|
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)| {
|
||||||
|
environment.add_env(&k, &v.as_str().unwrap().to_string());
|
||||||
|
});
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
working_dir = working_dir.unwrap().parent();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
environment.add_env("envtest", "I overwrote successfully");
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn sync_env_vars(&mut self, ctx: &mut Context) {
|
pub fn sync_env_vars(&mut self, ctx: &mut Context) {
|
||||||
let mut environment = self.env.lock();
|
|
||||||
self.add_nurc();
|
self.add_nurc();
|
||||||
|
let mut environment = self.env.lock();
|
||||||
if environment.env().is_some() {
|
if environment.env().is_some() {
|
||||||
for (name, value) in ctx.with_host(|host| host.vars()) {
|
for (name, value) in ctx.with_host(|host| host.vars()) {
|
||||||
if name != "path" && name != "PATH" {
|
if name != "path" && name != "PATH" {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user