nushell/crates/nu-command/src/commands/env/with_env.rs
Hristo Filaretov 88817a8f10
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
2021-08-26 08:15:58 -05:00

133 lines
4.0 KiB
Rust

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::{
hir::CapturedBlock, Signature, SpannedTypeName, SyntaxShape, UntaggedValue, Value,
};
pub struct WithEnv;
#[derive(Deserialize, Debug)]
struct WithEnvArgs {
variable: Value,
block: CapturedBlock,
}
impl WholeStreamCommand for WithEnv {
fn name(&self) -> &str {
"with-env"
}
fn signature(&self) -> Signature {
Signature::build("with-env")
.required(
"variable",
SyntaxShape::Any,
"the environment variable to temporarily set",
)
.required(
"block",
SyntaxShape::Block,
"the block to run once the variable is set",
)
}
fn usage(&self) -> &str {
"Runs a block with an environment variable set."
}
fn run_with_actions(&self, args: CommandArgs) -> Result<ActionStream, ShellError> {
with_env(args)
}
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "Set the MYENV environment variable",
example: r#"with-env [MYENV "my env value"] { echo $nu.env.MYENV }"#,
result: Some(vec![Value::from("my env value")]),
},
Example {
description: "Set by primitive value list",
example: r#"with-env [X Y W Z] { echo $nu.env.X $nu.env.W }"#,
result: Some(vec![Value::from("Y"), Value::from("Z")]),
},
Example {
description: "Set by single row table",
example: r#"with-env [[X W]; [Y Z]] { echo $nu.env.X $nu.env.W }"#,
result: Some(vec![Value::from("Y"), Value::from("Z")]),
},
Example {
description: "Set by row(e.g. `open x.json` or `from json`)",
example: r#"echo '{"X":"Y","W":"Z"}'|from json|with-env $it { echo $nu.env.X $nu.env.W }"#,
result: None,
},
]
}
}
fn with_env(args: CommandArgs) -> Result<ActionStream, ShellError> {
let external_redirection = args.call_info.args.external_redirection;
let context = &args.context;
let variable: Value = args.req(0)?;
let block: CapturedBlock = args.req(1)?;
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.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]).try_into()?);
}
}
}
}
// when get object by `open x.json` or `from json`
UntaggedValue::Row(row) => {
for (k, v) in &row.entries {
env.insert(k.clone(), v.try_into()?);
}
}
_ => {
return Err(ShellError::type_error(
"string list or single row",
variable.spanned_type_name(),
));
}
};
context.scope.enter_scope();
context.scope.add_env(env);
context.scope.add_vars(&block.captured.entries);
let result = run_block(&block.block, context, args.input, external_redirection);
context.scope.exit_scope();
result.map(|x| x.into_action_stream())
}
#[cfg(test)]
mod tests {
use super::ShellError;
use super::WithEnv;
#[test]
fn examples_work_as_expected() -> Result<(), ShellError> {
use crate::examples::test as test_examples;
test_examples(WithEnv {})
}
}