2021-10-12 05:28:39 +02:00
|
|
|
use nu_engine::{eval_block, CallExt};
|
2021-09-29 20:25:05 +02:00
|
|
|
use nu_protocol::ast::Call;
|
2022-01-12 05:06:56 +01:00
|
|
|
use nu_protocol::engine::{CaptureBlock, Command, EngineState, Stack};
|
2022-02-18 15:30:16 +01:00
|
|
|
use nu_protocol::{Category, Example, PipelineData, Signature, SyntaxShape, Value};
|
2021-09-29 20:25:05 +02:00
|
|
|
|
2021-10-25 06:01:02 +02:00
|
|
|
#[derive(Clone)]
|
2021-09-29 20:25:05 +02:00
|
|
|
pub struct Do;
|
|
|
|
|
|
|
|
impl Command for Do {
|
|
|
|
fn name(&self) -> &str {
|
|
|
|
"do"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn usage(&self) -> &str {
|
|
|
|
"Run a block"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn signature(&self) -> nu_protocol::Signature {
|
2021-10-12 05:28:39 +02:00
|
|
|
Signature::build("do")
|
2022-03-09 15:56:19 +01:00
|
|
|
.required("block", SyntaxShape::Any, "the block to run")
|
2021-12-23 22:41:29 +01:00
|
|
|
.switch(
|
|
|
|
"ignore-errors",
|
|
|
|
"ignore errors as the block runs",
|
|
|
|
Some('i'),
|
|
|
|
)
|
2021-10-12 05:28:39 +02:00
|
|
|
.rest("rest", SyntaxShape::Any, "the parameter(s) for the block")
|
2021-11-17 05:22:37 +01:00
|
|
|
.category(Category::Core)
|
2021-09-29 20:25:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn run(
|
|
|
|
&self,
|
2021-10-25 08:31:39 +02:00
|
|
|
engine_state: &EngineState,
|
|
|
|
stack: &mut Stack,
|
2021-09-29 20:25:05 +02:00
|
|
|
call: &Call,
|
2021-10-25 06:01:02 +02:00
|
|
|
input: PipelineData,
|
|
|
|
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
|
2022-01-12 05:06:56 +01:00
|
|
|
let block: CaptureBlock = call.req(engine_state, stack, 0)?;
|
2021-10-25 08:31:39 +02:00
|
|
|
let rest: Vec<Value> = call.rest(engine_state, stack, 1)?;
|
2022-01-12 05:06:56 +01:00
|
|
|
let ignore_errors = call.has_flag("ignore-errors");
|
2021-09-29 20:25:05 +02:00
|
|
|
|
2022-01-12 05:06:56 +01:00
|
|
|
let mut stack = stack.captures_to_stack(&block.captures);
|
|
|
|
let block = engine_state.get_block(block.block_id);
|
2021-10-12 05:28:39 +02:00
|
|
|
|
|
|
|
let params: Vec<_> = block
|
|
|
|
.signature
|
|
|
|
.required_positional
|
|
|
|
.iter()
|
|
|
|
.chain(block.signature.optional_positional.iter())
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
for param in params.iter().zip(&rest) {
|
|
|
|
if let Some(var_id) = param.0.var_id {
|
2021-10-25 08:31:39 +02:00
|
|
|
stack.add_var(var_id, param.1.clone())
|
2021-10-12 05:28:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(param) = &block.signature.rest_positional {
|
|
|
|
if rest.len() > params.len() {
|
|
|
|
let mut rest_items = vec![];
|
|
|
|
|
|
|
|
for r in rest.into_iter().skip(params.len()) {
|
|
|
|
rest_items.push(r);
|
|
|
|
}
|
|
|
|
|
|
|
|
let span = if let Some(rest_item) = rest_items.first() {
|
|
|
|
rest_item.span()?
|
|
|
|
} else {
|
|
|
|
call.head
|
|
|
|
};
|
|
|
|
|
2021-10-25 08:31:39 +02:00
|
|
|
stack.add_var(
|
2021-10-12 05:28:39 +02:00
|
|
|
param
|
|
|
|
.var_id
|
|
|
|
.expect("Internal error: rest positional parameter lacks var_id"),
|
|
|
|
Value::List {
|
|
|
|
vals: rest_items,
|
|
|
|
span,
|
|
|
|
},
|
|
|
|
)
|
2021-09-29 20:25:05 +02:00
|
|
|
}
|
|
|
|
}
|
2022-02-21 23:22:21 +01:00
|
|
|
let result = eval_block(
|
|
|
|
engine_state,
|
|
|
|
&mut stack,
|
|
|
|
block,
|
|
|
|
input,
|
|
|
|
call.redirect_stdout,
|
|
|
|
ignore_errors,
|
|
|
|
);
|
2021-12-23 22:41:29 +01:00
|
|
|
|
|
|
|
if ignore_errors {
|
|
|
|
match result {
|
|
|
|
Ok(x) => Ok(x),
|
|
|
|
Err(_) => Ok(PipelineData::new(call.head)),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
result
|
|
|
|
}
|
2021-09-29 20:25:05 +02:00
|
|
|
}
|
2022-02-18 15:30:16 +01:00
|
|
|
|
|
|
|
fn examples(&self) -> Vec<Example> {
|
|
|
|
vec![
|
|
|
|
Example {
|
|
|
|
description: "Run the block",
|
|
|
|
example: r#"do { echo hello }"#,
|
|
|
|
result: Some(Value::test_string("hello")),
|
|
|
|
},
|
|
|
|
Example {
|
|
|
|
description: "Run the block and ignore errors",
|
|
|
|
example: r#"do -i { thisisnotarealcommand }"#,
|
|
|
|
result: None,
|
|
|
|
},
|
2022-03-09 15:56:19 +01:00
|
|
|
Example {
|
|
|
|
description: "Run the block, with a positional parameter",
|
|
|
|
example: r#"do {|x| 100 + $x } 50"#,
|
|
|
|
result: Some(Value::test_int(150)),
|
|
|
|
},
|
2022-02-18 15:30:16 +01:00
|
|
|
]
|
|
|
|
}
|
2021-09-29 20:25:05 +02:00
|
|
|
}
|
2022-03-09 15:56:19 +01:00
|
|
|
|
|
|
|
mod test {
|
|
|
|
#[test]
|
|
|
|
fn test_examples() {
|
|
|
|
use super::Do;
|
|
|
|
use crate::test_examples;
|
|
|
|
test_examples(Do {})
|
|
|
|
}
|
|
|
|
}
|