2022-01-12 05:06:56 +01:00
|
|
|
use nu_engine::{eval_block, CallExt};
|
2021-11-27 18:49:03 +01:00
|
|
|
use nu_protocol::{
|
2021-11-29 07:52:23 +01:00
|
|
|
ast::Call,
|
2022-01-12 05:06:56 +01:00
|
|
|
engine::{CaptureBlock, Command, EngineState, Stack},
|
2021-12-19 08:46:13 +01:00
|
|
|
Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, SyntaxShape, Value,
|
2021-11-27 18:49:03 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct All;
|
|
|
|
|
|
|
|
impl Command for All {
|
|
|
|
fn name(&self) -> &str {
|
|
|
|
"all?"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn signature(&self) -> Signature {
|
|
|
|
Signature::build(self.name())
|
|
|
|
.required(
|
|
|
|
"predicate",
|
|
|
|
SyntaxShape::RowCondition,
|
|
|
|
"the predicate that must match",
|
|
|
|
)
|
|
|
|
.category(Category::Filters)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn usage(&self) -> &str {
|
|
|
|
"Test if every element of the input matches a predicate."
|
|
|
|
}
|
|
|
|
|
|
|
|
fn examples(&self) -> Vec<Example> {
|
|
|
|
vec![
|
|
|
|
Example {
|
|
|
|
description: "Find if services are running",
|
|
|
|
example: "echo [[status]; [UP] [UP]] | all? status == UP",
|
2021-12-19 08:46:13 +01:00
|
|
|
result: Some(Value::test_bool(true)),
|
2021-11-27 18:49:03 +01:00
|
|
|
},
|
|
|
|
Example {
|
|
|
|
description: "Check that all values are even",
|
|
|
|
example: "echo [2 4 6 8] | all? ($it mod 2) == 0",
|
2021-12-19 08:46:13 +01:00
|
|
|
result: Some(Value::test_bool(true)),
|
2021-11-27 18:49:03 +01:00
|
|
|
},
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
fn run(
|
|
|
|
&self,
|
|
|
|
engine_state: &EngineState,
|
|
|
|
stack: &mut Stack,
|
|
|
|
call: &Call,
|
|
|
|
input: PipelineData,
|
|
|
|
) -> Result<PipelineData, ShellError> {
|
2022-01-12 05:06:56 +01:00
|
|
|
// let predicate = &call.positional[0];
|
2021-12-03 00:11:25 +01:00
|
|
|
let span = call.head;
|
|
|
|
|
2022-01-12 05:06:56 +01:00
|
|
|
let capture_block: CaptureBlock = call.req(engine_state, stack, 0)?;
|
|
|
|
let block_id = capture_block.block_id;
|
2021-11-27 18:49:03 +01:00
|
|
|
|
|
|
|
let block = engine_state.get_block(block_id);
|
|
|
|
let var_id = block.signature.get_positional(0).and_then(|arg| arg.var_id);
|
2022-01-12 05:06:56 +01:00
|
|
|
let mut stack = stack.captures_to_stack(&capture_block.captures);
|
2021-11-27 18:49:03 +01:00
|
|
|
|
|
|
|
let ctrlc = engine_state.ctrlc.clone();
|
|
|
|
let engine_state = engine_state.clone();
|
|
|
|
|
2021-12-19 08:46:13 +01:00
|
|
|
Ok(Value::Bool {
|
|
|
|
val: input.into_interruptible_iter(ctrlc).all(move |value| {
|
2021-11-27 18:49:03 +01:00
|
|
|
if let Some(var_id) = var_id {
|
|
|
|
stack.add_var(var_id, value);
|
|
|
|
}
|
|
|
|
|
2022-02-21 23:22:21 +01:00
|
|
|
eval_block(
|
|
|
|
&engine_state,
|
|
|
|
&mut stack,
|
|
|
|
block,
|
|
|
|
PipelineData::new(span),
|
|
|
|
call.redirect_stdout,
|
|
|
|
call.redirect_stderr,
|
|
|
|
)
|
|
|
|
.map_or(false, |pipeline_data| {
|
|
|
|
pipeline_data.into_value(span).is_true()
|
|
|
|
})
|
2021-12-19 08:46:13 +01:00
|
|
|
}),
|
|
|
|
span,
|
|
|
|
}
|
|
|
|
.into_pipeline_data())
|
2021-11-27 18:49:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_examples() {
|
|
|
|
use crate::test_examples;
|
|
|
|
|
|
|
|
test_examples(All)
|
|
|
|
}
|
|
|
|
}
|