Merge pull request #906 from jonathandturner/nu_env_vars

Add initial support for env vars
This commit is contained in:
Jonathan Turner 2019-11-02 17:12:13 +13:00 committed by GitHub
commit aa64442453
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 4 deletions

View file

@ -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();

View file

@ -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())
}