mirror of
https://github.com/nushell/nushell
synced 2024-12-26 21:13:19 +00:00
Merge pull request #906 from jonathandturner/nu_env_vars
Add initial support for env vars
This commit is contained in:
commit
aa64442453
2 changed files with 33 additions and 4 deletions
33
src/cli.rs
33
src/cli.rs
|
@ -163,7 +163,7 @@ fn load_plugins(context: &mut Context) -> Result<(), ShellError> {
|
|||
require_literal_leading_dot: false,
|
||||
};
|
||||
|
||||
set_path_from_config();
|
||||
set_env_from_config();
|
||||
|
||||
for path in search_paths() {
|
||||
let mut pattern = path.to_path_buf();
|
||||
|
@ -474,8 +474,35 @@ fn chomp_newline(s: &str) -> &str {
|
|||
}
|
||||
}
|
||||
|
||||
fn set_path_from_config() {
|
||||
fn set_env_from_config() {
|
||||
let config = crate::data::config::read(Tag::unknown(), &None).unwrap();
|
||||
|
||||
if config.contains_key("env") {
|
||||
// Clear the existing vars, we're about to replace them
|
||||
for (key, _value) in std::env::vars() {
|
||||
std::env::remove_var(key);
|
||||
}
|
||||
|
||||
let value = config.get("env");
|
||||
|
||||
match value {
|
||||
Some(Tagged {
|
||||
item: Value::Row(r),
|
||||
..
|
||||
}) => {
|
||||
for (k, v) in &r.entries {
|
||||
match v.as_string() {
|
||||
Ok(value_string) => {
|
||||
std::env::set_var(k, value_string);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
if config.contains_key("path") {
|
||||
// Override the path with what they give us from config
|
||||
let value = config.get("path");
|
||||
|
@ -565,7 +592,7 @@ async fn process_line(readline: Result<String, ReadlineError>, ctx: &mut Context
|
|||
|
||||
// Check the config to see if we need to update the path
|
||||
// TODO: make sure config is cached so we don't path this load every call
|
||||
set_path_from_config();
|
||||
set_env_from_config();
|
||||
|
||||
loop {
|
||||
let item: Option<ClassifiedCommand> = iter.next();
|
||||
|
|
|
@ -169,7 +169,9 @@ fn evaluate_reference(
|
|||
x if x == "nu:env" => {
|
||||
let mut dict = TaggedDictBuilder::new(&tag);
|
||||
for v in std::env::vars() {
|
||||
dict.insert(v.0, Value::string(v.1));
|
||||
if v.0 != "PATH" && v.0 != "Path" {
|
||||
dict.insert(v.0, Value::string(v.1));
|
||||
}
|
||||
}
|
||||
Ok(dict.into_tagged_value())
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue