2022-08-18 22:24:39 +02:00
|
|
|
use nu_engine::{eval_block, redirect_env, CallExt};
|
|
|
|
use nu_protocol::{
|
|
|
|
ast::Call,
|
2022-11-10 09:21:49 +01:00
|
|
|
engine::{Closure, Command, EngineState, Stack},
|
2023-02-05 22:17:46 +01:00
|
|
|
Category, Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Value,
|
2022-08-18 22:24:39 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct ExportEnv;
|
|
|
|
|
|
|
|
impl Command for ExportEnv {
|
|
|
|
fn name(&self) -> &str {
|
|
|
|
"export-env"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn signature(&self) -> Signature {
|
|
|
|
Signature::build("export-env")
|
2022-11-09 22:55:05 +01:00
|
|
|
.input_output_types(vec![(Type::Nothing, Type::Nothing)])
|
2022-08-18 22:24:39 +02:00
|
|
|
.required(
|
|
|
|
"block",
|
2022-11-10 09:21:49 +01:00
|
|
|
SyntaxShape::Block,
|
2022-08-18 22:24:39 +02:00
|
|
|
"the block to run to set the environment",
|
|
|
|
)
|
|
|
|
.category(Category::Env)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn usage(&self) -> &str {
|
|
|
|
"Run a block and preserve its environment in a current scope."
|
|
|
|
}
|
|
|
|
|
|
|
|
fn run(
|
|
|
|
&self,
|
|
|
|
engine_state: &EngineState,
|
|
|
|
caller_stack: &mut Stack,
|
|
|
|
call: &Call,
|
|
|
|
input: PipelineData,
|
2023-02-05 22:17:46 +01:00
|
|
|
) -> Result<PipelineData, ShellError> {
|
2022-11-10 09:21:49 +01:00
|
|
|
let capture_block: Closure = call.req(engine_state, caller_stack, 0)?;
|
2022-08-18 22:24:39 +02:00
|
|
|
let block = engine_state.get_block(capture_block.block_id);
|
|
|
|
let mut callee_stack = caller_stack.captures_to_stack(&capture_block.captures);
|
|
|
|
|
|
|
|
let _ = eval_block(
|
|
|
|
engine_state,
|
|
|
|
&mut callee_stack,
|
|
|
|
block,
|
|
|
|
input,
|
|
|
|
call.redirect_stdout,
|
|
|
|
call.redirect_stderr,
|
|
|
|
);
|
|
|
|
|
|
|
|
redirect_env(engine_state, caller_stack, &callee_stack);
|
|
|
|
|
2022-12-07 19:31:57 +01:00
|
|
|
Ok(PipelineData::empty())
|
2022-08-18 22:24:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn examples(&self) -> Vec<Example> {
|
2022-11-09 22:55:05 +01:00
|
|
|
vec![
|
|
|
|
Example {
|
|
|
|
description: "Set an environment variable",
|
2023-06-30 21:57:51 +02:00
|
|
|
example: r#"export-env { $env.SPAM = 'eggs' }"#,
|
2023-09-03 16:27:29 +02:00
|
|
|
result: Some(Value::nothing(Span::test_data())),
|
2022-11-09 22:55:05 +01:00
|
|
|
},
|
|
|
|
Example {
|
|
|
|
description: "Set an environment variable and examine its value",
|
2023-06-30 21:57:51 +02:00
|
|
|
example: r#"export-env { $env.SPAM = 'eggs' }; $env.SPAM"#,
|
2022-12-24 10:25:38 +01:00
|
|
|
result: Some(Value::test_string("eggs")),
|
2022-11-09 22:55:05 +01:00
|
|
|
},
|
|
|
|
]
|
2022-08-18 22:24:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_examples() {
|
|
|
|
use crate::test_examples;
|
|
|
|
|
|
|
|
test_examples(ExportEnv {})
|
|
|
|
}
|
|
|
|
}
|