forked from extern/nushell
Allow environment variables to be hidden (#3950)
* Allow environment variables to be hidden This change allows environment variables in Nushell to have a value of `Nothing`, which can be set by the user by passing `$nothing` to `let-env` and friends. Environment variables with a value of Nothing behave as if they are not set at all. This allows a user to shadow the value of an environment variable in a parent scope, effectively removing it from their current scope. This was not possible before, because a scope can not affect its parent scopes. This is a workaround for issues like #3920. Additionally, this allows a user to simultaneously set, change and remove multiple environment variables via `load-env`. Any environment variables set to $nothing will be hidden and thus act as if they are removed. This simplifies working with virtual environments, which rely on setting multiple environment variables, including PATH, to specific values, and remove/change them on deactivation. One surprising behavior is that an environment variable set to $nothing will act as if it is not set when querying it (via $nu.env.X), but it is still possible to remove it entirely via `unlet-env`. If the same environment variable is present in the parent scope, the value in the parent scope will be visible to the user. This might be surprising behavior to users who are not familiar with the implementation details. An additional corner case is the the shorthand form of `with-env` does not work with this feature. Using `X=$nothing` will set $nu.env.X to the string "$nothing". The long-form works as expected: `with-env [X $nothing] {...}`. * Remove unused import * Allow all primitives to be convert to strings
This commit is contained in:
@ -1,5 +1,7 @@
|
||||
use std::convert::TryInto;
|
||||
|
||||
use crate::prelude::*;
|
||||
use nu_engine::{evaluate_baseline_expr, WholeStreamCommand};
|
||||
use nu_engine::{evaluate_baseline_expr, EnvVar, WholeStreamCommand};
|
||||
|
||||
use nu_errors::ShellError;
|
||||
use nu_protocol::{hir::CapturedBlock, hir::ClassifiedCommand, Signature, SyntaxShape};
|
||||
@ -90,9 +92,7 @@ pub fn set_env(args: CommandArgs) -> Result<ActionStream, ShellError> {
|
||||
|
||||
ctx.scope.exit_scope();
|
||||
|
||||
let value = value?;
|
||||
let value = value.as_string()?;
|
||||
|
||||
let value: EnvVar = value?.try_into()?;
|
||||
let name = name.item;
|
||||
|
||||
// Note: this is a special case for setting the context from a command
|
||||
|
13
crates/nu-command/src/commands/env/load_env.rs
vendored
13
crates/nu-command/src/commands/env/load_env.rs
vendored
@ -1,5 +1,7 @@
|
||||
use std::convert::TryInto;
|
||||
|
||||
use crate::prelude::*;
|
||||
use nu_engine::WholeStreamCommand;
|
||||
use nu_engine::{EnvVar, WholeStreamCommand};
|
||||
|
||||
use nu_errors::ShellError;
|
||||
use nu_protocol::{Signature, SyntaxShape, Value};
|
||||
@ -60,14 +62,17 @@ fn load_env_from_table(
|
||||
|
||||
for (key, value) in value.row_entries() {
|
||||
if key == "name" {
|
||||
var_name = Some(value.as_string()?);
|
||||
var_name = Some(value);
|
||||
} else if key == "value" {
|
||||
var_value = Some(value.as_string()?);
|
||||
var_value = Some(value);
|
||||
}
|
||||
}
|
||||
|
||||
match (var_name, var_value) {
|
||||
(Some(name), Some(value)) => ctx.scope.add_env_var(name, value),
|
||||
(Some(name), Some(value)) => {
|
||||
let env_var: EnvVar = value.try_into()?;
|
||||
ctx.scope.add_env_var(name.as_string()?, env_var);
|
||||
}
|
||||
_ => {
|
||||
return Err(ShellError::labeled_error(
|
||||
r#"Expected each row in the table to have a "name" and "value" field."#,
|
||||
|
11
crates/nu-command/src/commands/env/with_env.rs
vendored
11
crates/nu-command/src/commands/env/with_env.rs
vendored
@ -1,5 +1,8 @@
|
||||
use std::convert::TryInto;
|
||||
|
||||
use crate::prelude::*;
|
||||
use nu_engine::run_block;
|
||||
use nu_engine::EnvVar;
|
||||
use nu_engine::WholeStreamCommand;
|
||||
use nu_errors::ShellError;
|
||||
use nu_protocol::{
|
||||
@ -73,20 +76,20 @@ fn with_env(args: CommandArgs) -> Result<ActionStream, ShellError> {
|
||||
let variable: Value = args.req(0)?;
|
||||
let block: CapturedBlock = args.req(1)?;
|
||||
|
||||
let mut env = IndexMap::new();
|
||||
let mut env: IndexMap<String, EnvVar> = IndexMap::new();
|
||||
|
||||
match &variable.value {
|
||||
UntaggedValue::Table(table) => {
|
||||
if table.len() == 1 {
|
||||
// single row([[X W]; [Y Z]])
|
||||
for (k, v) in table[0].row_entries() {
|
||||
env.insert(k.clone(), v.convert_to_string());
|
||||
env.insert(k.clone(), v.try_into()?);
|
||||
}
|
||||
} else {
|
||||
// primitive values([X Y W Z])
|
||||
for row in table.chunks(2) {
|
||||
if row.len() == 2 && row[0].is_primitive() && row[1].is_primitive() {
|
||||
env.insert(row[0].convert_to_string(), row[1].convert_to_string());
|
||||
env.insert(row[0].convert_to_string(), (&row[1]).try_into()?);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -94,7 +97,7 @@ fn with_env(args: CommandArgs) -> Result<ActionStream, ShellError> {
|
||||
// when get object by `open x.json` or `from json`
|
||||
UntaggedValue::Row(row) => {
|
||||
for (k, v) in &row.entries {
|
||||
env.insert(k.clone(), v.convert_to_string());
|
||||
env.insert(k.clone(), v.try_into()?);
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
|
Reference in New Issue
Block a user