2020-05-06 05:56:31 +02:00
|
|
|
use crate::prelude::*;
|
2021-01-10 03:50:49 +01:00
|
|
|
use nu_engine::run_block;
|
|
|
|
use nu_engine::WholeStreamCommand;
|
2020-05-06 05:56:31 +02:00
|
|
|
use nu_errors::ShellError;
|
2020-09-26 01:40:02 +02:00
|
|
|
use nu_protocol::{
|
2020-12-18 08:53:49 +01:00
|
|
|
hir::CapturedBlock, Signature, SpannedTypeName, SyntaxShape, UntaggedValue, Value,
|
2020-09-26 01:40:02 +02:00
|
|
|
};
|
2020-05-06 05:56:31 +02:00
|
|
|
|
|
|
|
pub struct WithEnv;
|
|
|
|
|
|
|
|
#[derive(Deserialize, Debug)]
|
|
|
|
struct WithEnvArgs {
|
2020-09-11 08:17:35 +02:00
|
|
|
variable: Value,
|
2020-12-18 08:53:49 +01:00
|
|
|
block: CapturedBlock,
|
2020-05-06 05:56:31 +02:00
|
|
|
}
|
2020-05-29 10:22:52 +02:00
|
|
|
|
|
|
|
#[async_trait]
|
2020-05-06 05:56:31 +02:00
|
|
|
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 {
|
2021-03-08 00:57:58 +01:00
|
|
|
"Runs a block with an environment variable set."
|
2020-05-06 05:56:31 +02:00
|
|
|
}
|
|
|
|
|
2020-12-18 08:53:49 +01:00
|
|
|
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
|
|
|
|
with_env(args).await
|
2020-05-06 05:56:31 +02:00
|
|
|
}
|
2020-05-12 17:54:29 +02:00
|
|
|
|
2020-05-18 14:56:01 +02:00
|
|
|
fn examples(&self) -> Vec<Example> {
|
2020-09-10 09:23:28 +02:00
|
|
|
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 {
|
2020-09-11 08:17:35 +02:00
|
|
|
description: "Set by primitive value list",
|
2020-09-10 09:23:28 +02:00
|
|
|
example: r#"with-env [X Y W Z] { echo $nu.env.X $nu.env.W }"#,
|
|
|
|
result: Some(vec![Value::from("Y"), Value::from("Z")]),
|
|
|
|
},
|
2020-09-11 08:17:35 +02:00
|
|
|
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,
|
|
|
|
},
|
2020-09-10 09:23:28 +02:00
|
|
|
]
|
2020-05-12 17:54:29 +02:00
|
|
|
}
|
2020-05-06 05:56:31 +02:00
|
|
|
}
|
|
|
|
|
2020-12-18 08:53:49 +01:00
|
|
|
async fn with_env(raw_args: CommandArgs) -> Result<OutputStream, ShellError> {
|
2021-02-15 08:14:16 +01:00
|
|
|
let redirection = raw_args.call_info.args.external_redirection;
|
2021-02-06 01:34:26 +01:00
|
|
|
let context = EvaluationContext::from_args(&raw_args);
|
2021-02-15 08:14:16 +01:00
|
|
|
let (
|
|
|
|
WithEnvArgs {
|
|
|
|
variable,
|
|
|
|
mut block,
|
|
|
|
},
|
|
|
|
input,
|
|
|
|
) = raw_args.process().await?;
|
2020-05-27 06:50:26 +02:00
|
|
|
|
2021-02-15 08:14:16 +01:00
|
|
|
block.block.set_redirect(redirection);
|
2021-02-18 03:56:14 +01:00
|
|
|
|
2020-09-26 01:40:02 +02:00
|
|
|
let mut env = IndexMap::new();
|
|
|
|
|
2020-09-11 08:17:35 +02:00
|
|
|
match &variable.value {
|
|
|
|
UntaggedValue::Table(table) => {
|
|
|
|
if table.len() == 1 {
|
|
|
|
// single row([[X W]; [Y Z]])
|
|
|
|
for (k, v) in table[0].row_entries() {
|
2020-09-26 01:40:02 +02:00
|
|
|
env.insert(k.clone(), v.convert_to_string());
|
2020-09-11 08:17:35 +02:00
|
|
|
}
|
|
|
|
} 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() {
|
2020-09-26 01:40:02 +02:00
|
|
|
env.insert(row[0].convert_to_string(), row[1].convert_to_string());
|
2020-09-11 08:17:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-09-10 09:23:28 +02:00
|
|
|
}
|
2020-09-11 08:17:35 +02:00
|
|
|
// when get object by `open x.json` or `from json`
|
|
|
|
UntaggedValue::Row(row) => {
|
|
|
|
for (k, v) in &row.entries {
|
2020-09-26 01:40:02 +02:00
|
|
|
env.insert(k.clone(), v.convert_to_string());
|
2020-09-11 08:17:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
return Err(ShellError::type_error(
|
|
|
|
"string list or single row",
|
|
|
|
variable.spanned_type_name(),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
};
|
2020-05-16 05:18:24 +02:00
|
|
|
|
2020-12-18 08:53:49 +01:00
|
|
|
context.scope.enter_scope();
|
|
|
|
context.scope.add_env(env);
|
|
|
|
context.scope.add_vars(&block.captured.entries);
|
2020-09-26 01:40:02 +02:00
|
|
|
|
2020-12-18 08:53:49 +01:00
|
|
|
let result = run_block(&block.block, &context, input).await;
|
|
|
|
context.scope.exit_scope();
|
2020-05-06 05:56:31 +02:00
|
|
|
|
2020-06-14 00:01:44 +02:00
|
|
|
result.map(|x| x.to_output_stream())
|
2020-05-06 05:56:31 +02:00
|
|
|
}
|
2020-05-18 14:56:01 +02:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2020-10-03 16:06:02 +02:00
|
|
|
use super::ShellError;
|
2020-05-18 14:56:01 +02:00
|
|
|
use super::WithEnv;
|
|
|
|
|
|
|
|
#[test]
|
2020-10-03 16:06:02 +02:00
|
|
|
fn examples_work_as_expected() -> Result<(), ShellError> {
|
2020-05-18 14:56:01 +02:00
|
|
|
use crate::examples::test as test_examples;
|
|
|
|
|
2021-02-12 11:13:14 +01:00
|
|
|
test_examples(WithEnv {})
|
2020-05-18 14:56:01 +02:00
|
|
|
}
|
|
|
|
}
|