mirror of
https://github.com/nushell/nushell.git
synced 2025-01-24 15:19:29 +01:00
Refactor from_config
This commit is contained in:
parent
6ce5a87c30
commit
a0cedfce8d
@ -1,42 +1,59 @@
|
|||||||
use indexmap::indexmap;
|
use indexmap::IndexMap;
|
||||||
use std::{
|
use nu_protocol::{Primitive, UntaggedValue, Value};
|
||||||
collections::HashMap,
|
use std::{fmt::Debug, path::PathBuf};
|
||||||
fmt::Debug,
|
|
||||||
path::PathBuf,
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
#[derive(Debug, Default)]
|
#[derive(Debug, Default)]
|
||||||
pub struct DirectorySpecificEnvironment {
|
pub struct DirectorySpecificEnvironment {
|
||||||
pub whitelisted_directories: Vec<PathBuf>,
|
pub whitelisted_directories: Vec<PathBuf>,
|
||||||
|
|
||||||
//Directory -> Env key. If an environment var has been added from a .nu in a directory, we track it here so we can remove it when the user leaves the directory.
|
//Directory -> Env key. If an environment var has been added from a .nu in a directory, we track it here so we can remove it when the user leaves the directory.
|
||||||
pub added_env_vars: HashMap<PathBuf, Vec<String>>,
|
pub added_env_vars: IndexMap<PathBuf, Vec<String>>,
|
||||||
|
|
||||||
//Directory -> (env_key, value). If a .nu overwrites some existing environment variables, they are added here so that they can be restored later.
|
//Directory -> (env_key, value). If a .nu overwrites some existing environment variables, they are added here so that they can be restored later.
|
||||||
pub overwritten_env_values: HashMap<PathBuf, Vec<(String, String)>>,
|
pub overwritten_env_values: IndexMap<PathBuf, Vec<(String, String)>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DirectorySpecificEnvironment {
|
impl DirectorySpecificEnvironment {
|
||||||
pub fn new(whitelisted_directories: Vec<PathBuf>) -> DirectorySpecificEnvironment {
|
pub fn new(whitelisted_directories: Option<Value>) -> DirectorySpecificEnvironment {
|
||||||
|
let mut whitelisted_directories = if let Some(Value {
|
||||||
|
value: UntaggedValue::Table(ref wrapped_directories),
|
||||||
|
tag: _,
|
||||||
|
}) = whitelisted_directories
|
||||||
|
{
|
||||||
|
wrapped_directories
|
||||||
|
.iter()
|
||||||
|
.fold(vec![], |mut directories, dirval| {
|
||||||
|
if let Value {
|
||||||
|
value: UntaggedValue::Primitive(Primitive::String(ref dir)),
|
||||||
|
tag: _,
|
||||||
|
} = dirval
|
||||||
|
{
|
||||||
|
directories.push(PathBuf::from(&dir));
|
||||||
|
}
|
||||||
|
directories
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
vec![]
|
||||||
|
};
|
||||||
|
whitelisted_directories.sort();
|
||||||
|
|
||||||
DirectorySpecificEnvironment {
|
DirectorySpecificEnvironment {
|
||||||
whitelisted_directories,
|
whitelisted_directories,
|
||||||
added_env_vars: HashMap::new(),
|
added_env_vars: IndexMap::new(),
|
||||||
overwritten_env_values: HashMap::new(),
|
overwritten_env_values: IndexMap::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn env_vars_to_add(&mut self) -> std::io::Result<HashMap<String, String>> {
|
pub fn env_vars_to_add(&mut self) -> std::io::Result<IndexMap<String, String>> {
|
||||||
let current_dir = std::env::current_dir()?;
|
let current_dir = std::env::current_dir()?;
|
||||||
|
|
||||||
let mut vars_to_add = HashMap::new();
|
let mut vars_to_add = IndexMap::new();
|
||||||
for dir in &self.whitelisted_directories {
|
for dir in &self.whitelisted_directories {
|
||||||
//Start in the current directory, then traverse towards the root with working_dir to see if we are in a subdirectory of a valid directory.
|
//Start in the current directory, then traverse towards the root with working_dir to see if we are in a subdirectory of a valid directory.
|
||||||
let mut working_dir = Some(current_dir.as_path());
|
let mut working_dir = Some(current_dir.as_path());
|
||||||
|
|
||||||
while let Some(wdir) = working_dir {
|
while let Some(wdir) = working_dir {
|
||||||
if wdir == dir.as_path() {
|
if wdir == dir.as_path() {
|
||||||
|
|
||||||
//Read the .nu file and parse it into a nice map
|
//Read the .nu file and parse it into a nice map
|
||||||
let toml_doc = std::fs::read_to_string(wdir.join(".nu").as_path())?
|
let toml_doc = std::fs::read_to_string(wdir.join(".nu").as_path())?
|
||||||
.parse::<toml::Value>()
|
.parse::<toml::Value>()
|
||||||
@ -87,4 +104,4 @@ impl DirectorySpecificEnvironment {
|
|||||||
|
|
||||||
Ok(vars_to_delete)
|
Ok(vars_to_delete)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
26
crates/nu-cli/src/env/environment.rs
vendored
26
crates/nu-cli/src/env/environment.rs
vendored
@ -1,9 +1,9 @@
|
|||||||
use crate::data::config::Conf;
|
use crate::data::config::Conf;
|
||||||
use crate::env::directory_specific_environment::*;
|
use crate::env::directory_specific_environment::*;
|
||||||
use indexmap::{indexmap, IndexSet};
|
use indexmap::{indexmap, IndexSet};
|
||||||
use nu_protocol::{Primitive, UntaggedValue, Value};
|
use nu_protocol::{UntaggedValue, Value};
|
||||||
use std::ffi::OsString;
|
use std::ffi::OsString;
|
||||||
use std::{fmt::Debug, path::PathBuf};
|
use std::fmt::Debug;
|
||||||
|
|
||||||
pub trait Env: Debug + Send {
|
pub trait Env: Debug + Send {
|
||||||
fn env(&self) -> Option<Value>;
|
fn env(&self) -> Option<Value>;
|
||||||
@ -43,7 +43,7 @@ impl Environment {
|
|||||||
Environment {
|
Environment {
|
||||||
environment_vars: None,
|
environment_vars: None,
|
||||||
path_vars: None,
|
path_vars: None,
|
||||||
direnv: DirectorySpecificEnvironment::new(vec![]),
|
direnv: DirectorySpecificEnvironment::new(None),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -51,28 +51,10 @@ impl Environment {
|
|||||||
let env = configuration.env();
|
let env = configuration.env();
|
||||||
let path = configuration.path();
|
let path = configuration.path();
|
||||||
|
|
||||||
let mut directories = vec![];
|
|
||||||
if let Some(Value {
|
|
||||||
value: UntaggedValue::Table(ref directories_as_values),
|
|
||||||
tag: _,
|
|
||||||
}) = configuration.direnv_whitelist()
|
|
||||||
{
|
|
||||||
for dirval in directories_as_values {
|
|
||||||
if let Value {
|
|
||||||
value: UntaggedValue::Primitive(Primitive::String(ref dir)),
|
|
||||||
tag: _,
|
|
||||||
} = dirval
|
|
||||||
{
|
|
||||||
directories.push(PathBuf::from(&dir));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
directories.sort();
|
|
||||||
|
|
||||||
Environment {
|
Environment {
|
||||||
environment_vars: env,
|
environment_vars: env,
|
||||||
path_vars: path,
|
path_vars: path,
|
||||||
direnv: DirectorySpecificEnvironment::new(directories),
|
direnv: DirectorySpecificEnvironment::new(configuration.direnv_whitelist()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user