mirror of
https://github.com/nushell/nushell.git
synced 2024-12-26 17:09:06 +01:00
Move work to cli.rs
This commit is contained in:
parent
3aeddee2fe
commit
e97e883d1f
@ -22,7 +22,8 @@ use rustyline::{
|
||||
KeyPress, Movement, Word,
|
||||
};
|
||||
use std::error::Error;
|
||||
use std::io::{BufRead, BufReader, Write};
|
||||
use std::fs::File;
|
||||
use std::io::{BufRead, BufReader, Write, Read};
|
||||
use std::iter::Iterator;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::atomic::Ordering;
|
||||
@ -740,6 +741,23 @@ 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>,
|
||||
@ -888,7 +906,8 @@ async fn process_line(
|
||||
classified_block.block.expand_it_usage();
|
||||
|
||||
trace!("{:#?}", classified_block);
|
||||
let env = ctx.get_env();
|
||||
let mut env = ctx.get_env();
|
||||
add_nurc(&mut env);
|
||||
match run_block(
|
||||
&classified_block.block,
|
||||
ctx,
|
||||
|
83
crates/nu-cli/src/env/environment.rs
vendored
83
crates/nu-cli/src/env/environment.rs
vendored
@ -38,7 +38,6 @@ impl Env for Box<dyn Env> {
|
||||
pub struct Environment {
|
||||
environment_vars: Option<Value>,
|
||||
path_vars: Option<Value>,
|
||||
nurc_env_keys: HashMap<PathBuf, Vec<String>>, //Directory -> Env key. If an environment var has been added from a .nurc in a directory, we track it here so we can remove it when the user leaves the directory.
|
||||
}
|
||||
|
||||
impl Environment {
|
||||
@ -46,7 +45,6 @@ impl Environment {
|
||||
Environment {
|
||||
environment_vars: None,
|
||||
path_vars: None,
|
||||
nurc_env_keys: HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
@ -57,87 +55,6 @@ impl Environment {
|
||||
Environment {
|
||||
environment_vars: env,
|
||||
path_vars: path,
|
||||
nurc_env_keys: HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
//Add env vars specified in the current dirs .nurc, if it exists.
|
||||
//TODO: Remove env vars after leaving the directory. Save added vars in env?
|
||||
//Map directory to vars
|
||||
//TODO: Add authentication by saving the path to the .nurc file in some variable?
|
||||
//TODO: handle errors
|
||||
|
||||
pub fn maintain_nurc_environment_vars(&mut self) {
|
||||
match self.add_nurc() {
|
||||
_ => {}
|
||||
};
|
||||
|
||||
match self.clear_vars_from_unvisited_dirs() {
|
||||
_ => {}
|
||||
};
|
||||
}
|
||||
|
||||
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 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)| {
|
||||
self.add_env(k, v.as_str().unwrap());
|
||||
});
|
||||
|
||||
self.nurc_env_keys.insert(
|
||||
std::env::current_dir()?,
|
||||
nurc_vars.keys().map(|k| k.clone()).collect(),
|
||||
); //Maybe could do without clone here, but leave for now
|
||||
Ok(())
|
||||
}
|
||||
|
||||
//If the user has left directories which added env vars through .nurc, we clear those 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
|
||||
//TODO: Seems like vars are re-added immediately after being removed
|
||||
pub fn clear_vars_from_unvisited_dirs(&mut self) -> std::io::Result<()> {
|
||||
let current_dir = std::env::current_dir()?;
|
||||
|
||||
let mut new_nurc_env_vars = HashMap::new();
|
||||
for (d, v) in self.nurc_env_keys.iter() {
|
||||
let mut working_dir = Some(current_dir.as_path());
|
||||
while working_dir.is_some() {
|
||||
if working_dir.unwrap() == d {
|
||||
new_nurc_env_vars.insert(d.clone(), v.clone());
|
||||
break;
|
||||
} else {
|
||||
working_dir = working_dir.unwrap().parent();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let mut vars_to_delete = vec![];
|
||||
for (path, vals) in self.nurc_env_keys.iter() {
|
||||
if !new_nurc_env_vars.contains_key(path) {
|
||||
vars_to_delete.extend(vals.clone());
|
||||
}
|
||||
}
|
||||
|
||||
vars_to_delete.iter().for_each(|env_var| self.remove_env(env_var));
|
||||
|
||||
self.nurc_env_keys = new_nurc_env_vars;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn remove_env(&mut self, key: &str) {
|
||||
if let Some(Value {
|
||||
value: UntaggedValue::Row(envs),
|
||||
tag: _,
|
||||
}) = &mut self.environment_vars
|
||||
{
|
||||
|
||||
envs.entries.remove(key);
|
||||
std::env::remove_var(key);
|
||||
}
|
||||
}
|
||||
|
||||
|
1
crates/nu-cli/src/env/environment_syncer.rs
vendored
1
crates/nu-cli/src/env/environment_syncer.rs
vendored
@ -44,7 +44,6 @@ impl EnvironmentSyncer {
|
||||
pub fn sync_env_vars(&mut self, ctx: &mut Context) {
|
||||
let mut environment = self.env.lock();
|
||||
|
||||
environment.maintain_nurc_environment_vars();
|
||||
if environment.env().is_some() {
|
||||
for (name, value) in ctx.with_host(|host| host.vars()) {
|
||||
if name != "path" && name != "PATH" {
|
||||
|
Loading…
Reference in New Issue
Block a user