forked from extern/nushell
Add the load-env command (#3484)
* Add the load-env command load-env can be used to add environment variables dynamically via an InputStream. This allows developers to create tools that output environment variables as key-value pairs, then have the user load those variables in using load-env. This supplants most of the need for an `eval` command, which is mostly used in POSIX envs for setting env vars. Fixes #3481 * fixup! Add the load-env command
This commit is contained in:
parent
65ee7aa372
commit
1ee51f2afa
@ -79,6 +79,7 @@ pub(crate) mod length;
|
||||
pub(crate) mod let_;
|
||||
pub(crate) mod let_env;
|
||||
pub(crate) mod lines;
|
||||
pub(crate) mod load_env;
|
||||
pub(crate) mod ls;
|
||||
pub(crate) mod math;
|
||||
pub(crate) mod merge;
|
||||
@ -226,6 +227,7 @@ pub(crate) use length::Length;
|
||||
pub(crate) use let_::Let;
|
||||
pub(crate) use let_env::LetEnv;
|
||||
pub(crate) use lines::Lines;
|
||||
pub(crate) use load_env::LoadEnv;
|
||||
pub(crate) use ls::Ls;
|
||||
pub(crate) use math::{
|
||||
Math, MathAbs, MathAverage, MathCeil, MathEval, MathFloor, MathMaximum, MathMedian,
|
||||
|
@ -13,6 +13,7 @@ pub fn create_default_context(interactive: bool) -> Result<EvaluationContext, Bo
|
||||
whole_stream_command(NuPlugin),
|
||||
whole_stream_command(Let),
|
||||
whole_stream_command(LetEnv),
|
||||
whole_stream_command(LoadEnv),
|
||||
whole_stream_command(Def),
|
||||
whole_stream_command(Source),
|
||||
// System/file operations
|
||||
|
95
crates/nu-command/src/commands/load_env.rs
Normal file
95
crates/nu-command/src/commands/load_env.rs
Normal file
@ -0,0 +1,95 @@
|
||||
use crate::prelude::*;
|
||||
use nu_engine::WholeStreamCommand;
|
||||
|
||||
use nu_errors::ShellError;
|
||||
use nu_protocol::{Signature, SyntaxShape, Value};
|
||||
|
||||
pub struct LoadEnv;
|
||||
|
||||
impl WholeStreamCommand for LoadEnv {
|
||||
fn name(&self) -> &str {
|
||||
"load-env"
|
||||
}
|
||||
|
||||
fn signature(&self) -> Signature {
|
||||
Signature::build("load-env").optional(
|
||||
"environ",
|
||||
SyntaxShape::Any,
|
||||
"Optional environment table to load in. If not provided, will use the table provided on the input stream",
|
||||
)
|
||||
}
|
||||
|
||||
fn usage(&self) -> &str {
|
||||
"Set environment variables using a table stream"
|
||||
}
|
||||
|
||||
fn run_with_actions(&self, args: CommandArgs) -> Result<ActionStream, ShellError> {
|
||||
load_env(args)
|
||||
}
|
||||
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
vec![
|
||||
Example {
|
||||
description: "Load variables from an input stream",
|
||||
example: r#"echo [[name, value]; ["NAME", "JT"] ["AGE", "UNKNOWN"]] | load-env; echo $nu.env.NAME"#,
|
||||
result: Some(vec![Value::from("JT")]),
|
||||
},
|
||||
Example {
|
||||
description: "Load variables from an argument",
|
||||
example: r#"load-env [[name, value]; ["NAME", "JT"] ["AGE", "UNKNOWN"]]; echo $nu.env.NAME"#,
|
||||
result: Some(vec![Value::from("JT")]),
|
||||
},
|
||||
Example {
|
||||
description: "Load variables from an argument and an input stream",
|
||||
example: r#"echo [[name, value]; ["NAME", "JT"]] | load-env [[name, value]; ["VALUE", "FOO"]]; echo $nu.env.NAME $nu.env.VALUE"#,
|
||||
result: Some(vec![Value::from("JT"), Value::from("UNKNOWN")]),
|
||||
},
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
fn load_env_from_table(
|
||||
values: impl IntoIterator<Item = Value>,
|
||||
ctx: &EvaluationContext,
|
||||
) -> Result<(), ShellError> {
|
||||
for value in values {
|
||||
let mut var_name = None;
|
||||
let mut var_value = None;
|
||||
|
||||
let tag = value.tag();
|
||||
|
||||
for (key, value) in value.row_entries() {
|
||||
if key == "name" {
|
||||
var_name = Some(value.as_string()?);
|
||||
} else if key == "value" {
|
||||
var_value = Some(value.as_string()?);
|
||||
}
|
||||
}
|
||||
|
||||
match (var_name, var_value) {
|
||||
(Some(name), Some(value)) => ctx.scope.add_env_var(name, value),
|
||||
_ => {
|
||||
return Err(ShellError::labeled_error(
|
||||
r#"Expected each row in the table to have a "name" and "value" field."#,
|
||||
r#"expected a "name" and "value" field"#,
|
||||
tag,
|
||||
))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn load_env(args: CommandArgs) -> Result<ActionStream, ShellError> {
|
||||
let ctx = EvaluationContext::from_args(&args);
|
||||
let args = args.evaluate_once()?;
|
||||
|
||||
if let Some(values) = args.opt::<Vec<Value>>(0)? {
|
||||
load_env_from_table(values, &ctx)?;
|
||||
}
|
||||
|
||||
load_env_from_table(args.input, &ctx)?;
|
||||
|
||||
Ok(ActionStream::empty())
|
||||
}
|
@ -368,6 +368,68 @@ fn proper_shadow_let_env_aliases() {
|
||||
assert_eq!(actual.out, "truefalsetrue");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn load_env_variable() {
|
||||
let actual = nu!(
|
||||
cwd: ".",
|
||||
r#"
|
||||
echo [[name, value]; [TESTENVVAR, "hello world"]] | load-env
|
||||
echo $nu.env.TESTENVVAR
|
||||
"#
|
||||
);
|
||||
|
||||
assert_eq!(actual.out, "hello world");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn load_env_variable_arg() {
|
||||
let actual = nu!(
|
||||
cwd: ".",
|
||||
r#"
|
||||
load-env [[name, value]; [TESTENVVAR, "hello world"]]
|
||||
echo $nu.env.TESTENVVAR
|
||||
"#
|
||||
);
|
||||
|
||||
assert_eq!(actual.out, "hello world");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn load_env_variable_arg_and_stream() {
|
||||
let actual = nu!(
|
||||
cwd: ".",
|
||||
r#"
|
||||
echo [[name, value]; [TESTVARSTREAM, "true"]] | load-env [[name, value]; [TESTVARARG, "false"]]
|
||||
echo $nu.env | format "{TESTVARSTREAM} {TESTVARARG}"
|
||||
"#
|
||||
);
|
||||
|
||||
assert_eq!(actual.out, "true false");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn load_env_doesnt_leak() {
|
||||
let actual = nu!(
|
||||
cwd: ".",
|
||||
r#"
|
||||
do { echo [[name, value]; [xyz, "my message"]] | load-env }; echo $nu.env.xyz
|
||||
"#
|
||||
);
|
||||
|
||||
assert!(actual.err.contains("did you mean"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn proper_shadow_load_env_aliases() {
|
||||
let actual = nu!(
|
||||
cwd: ".",
|
||||
r#"
|
||||
let-env DEBUG = true; echo $nu.env.DEBUG | autoview; do { echo [[name, value]; [DEBUG, false]] | load-env; echo $nu.env.DEBUG } | autoview; echo $nu.env.DEBUG
|
||||
"#
|
||||
);
|
||||
assert_eq!(actual.out, "truefalsetrue");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn proper_shadow_let_aliases() {
|
||||
let actual = nu!(
|
||||
|
Loading…
Reference in New Issue
Block a user