2020-04-20 08:41:51 +02:00
|
|
|
use crate::commands::classified::block::run_block;
|
2020-04-13 09:59:57 +02:00
|
|
|
use crate::commands::PerItemCommand;
|
|
|
|
use crate::context::CommandRegistry;
|
|
|
|
use crate::prelude::*;
|
Move external closer to internal (#1611)
* Refactor InputStream and affected commands.
First, making `values` private and leaning on the `Stream` implementation makes
consumes of `InputStream` less likely to have to change in the future, if we
change what an `InputStream` is internally.
Second, we're dropping `Option<InputStream>` as the input to pipelines,
internals, and externals. Instead, `InputStream.is_empty` can be used to check
for "emptiness". Empty streams are typically only ever used as the first input
to a pipeline.
* Add run_external internal command.
We want to push external commands closer to internal commands, eventually
eliminating the concept of "external" completely. This means we can consolidate
a couple of things:
- Variable evaluation (for example, `$it`, `$nu`, alias vars)
- Behaviour of whole stream vs per-item external execution
It should also make it easier for us to start introducing argument signatures
for external commands,
* Update run_external.rs
* Update run_external.rs
* Update run_external.rs
* Update run_external.rs
Co-authored-by: Jonathan Turner <jonathandturner@users.noreply.github.com>
2020-04-20 05:30:44 +02:00
|
|
|
|
|
|
|
use futures::stream::once;
|
|
|
|
|
2020-04-13 09:59:57 +02:00
|
|
|
use nu_errors::ShellError;
|
2020-04-20 08:41:51 +02:00
|
|
|
use nu_protocol::{CallInfo, ReturnSuccess, Scope, Signature, SyntaxShape, UntaggedValue, Value};
|
2020-04-13 09:59:57 +02:00
|
|
|
|
|
|
|
pub struct Each;
|
|
|
|
|
|
|
|
impl PerItemCommand for Each {
|
|
|
|
fn name(&self) -> &str {
|
|
|
|
"each"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn signature(&self) -> Signature {
|
|
|
|
Signature::build("each").required(
|
|
|
|
"block",
|
|
|
|
SyntaxShape::Block,
|
|
|
|
"the block to run on each row",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn usage(&self) -> &str {
|
|
|
|
"Run a block on each row of the table."
|
|
|
|
}
|
|
|
|
|
|
|
|
fn run(
|
|
|
|
&self,
|
|
|
|
call_info: &CallInfo,
|
|
|
|
registry: &CommandRegistry,
|
|
|
|
raw_args: &RawCommandArgs,
|
|
|
|
input: Value,
|
|
|
|
) -> Result<OutputStream, ShellError> {
|
|
|
|
let call_info = call_info.clone();
|
|
|
|
let registry = registry.clone();
|
|
|
|
let raw_args = raw_args.clone();
|
|
|
|
let stream = async_stream! {
|
|
|
|
match call_info.args.expect_nth(0)? {
|
|
|
|
Value {
|
|
|
|
value: UntaggedValue::Block(block),
|
|
|
|
tag
|
|
|
|
} => {
|
|
|
|
let mut context = Context::from_raw(&raw_args, ®istry);
|
2020-04-16 06:25:24 +02:00
|
|
|
let input_clone = input.clone();
|
Move external closer to internal (#1611)
* Refactor InputStream and affected commands.
First, making `values` private and leaning on the `Stream` implementation makes
consumes of `InputStream` less likely to have to change in the future, if we
change what an `InputStream` is internally.
Second, we're dropping `Option<InputStream>` as the input to pipelines,
internals, and externals. Instead, `InputStream.is_empty` can be used to check
for "emptiness". Empty streams are typically only ever used as the first input
to a pipeline.
* Add run_external internal command.
We want to push external commands closer to internal commands, eventually
eliminating the concept of "external" completely. This means we can consolidate
a couple of things:
- Variable evaluation (for example, `$it`, `$nu`, alias vars)
- Behaviour of whole stream vs per-item external execution
It should also make it easier for us to start introducing argument signatures
for external commands,
* Update run_external.rs
* Update run_external.rs
* Update run_external.rs
* Update run_external.rs
Co-authored-by: Jonathan Turner <jonathandturner@users.noreply.github.com>
2020-04-20 05:30:44 +02:00
|
|
|
let input_stream = once(async { Ok(input) }).to_input_stream();
|
2020-04-13 09:59:57 +02:00
|
|
|
|
2020-04-20 08:41:51 +02:00
|
|
|
let result = run_block(
|
|
|
|
block,
|
2020-04-13 09:59:57 +02:00
|
|
|
&mut context,
|
Move external closer to internal (#1611)
* Refactor InputStream and affected commands.
First, making `values` private and leaning on the `Stream` implementation makes
consumes of `InputStream` less likely to have to change in the future, if we
change what an `InputStream` is internally.
Second, we're dropping `Option<InputStream>` as the input to pipelines,
internals, and externals. Instead, `InputStream.is_empty` can be used to check
for "emptiness". Empty streams are typically only ever used as the first input
to a pipeline.
* Add run_external internal command.
We want to push external commands closer to internal commands, eventually
eliminating the concept of "external" completely. This means we can consolidate
a couple of things:
- Variable evaluation (for example, `$it`, `$nu`, alias vars)
- Behaviour of whole stream vs per-item external execution
It should also make it easier for us to start introducing argument signatures
for external commands,
* Update run_external.rs
* Update run_external.rs
* Update run_external.rs
* Update run_external.rs
Co-authored-by: Jonathan Turner <jonathandturner@users.noreply.github.com>
2020-04-20 05:30:44 +02:00
|
|
|
input_stream,
|
2020-04-16 06:25:24 +02:00
|
|
|
&Scope::new(input_clone),
|
2020-04-13 09:59:57 +02:00
|
|
|
).await;
|
|
|
|
|
|
|
|
match result {
|
Move external closer to internal (#1611)
* Refactor InputStream and affected commands.
First, making `values` private and leaning on the `Stream` implementation makes
consumes of `InputStream` less likely to have to change in the future, if we
change what an `InputStream` is internally.
Second, we're dropping `Option<InputStream>` as the input to pipelines,
internals, and externals. Instead, `InputStream.is_empty` can be used to check
for "emptiness". Empty streams are typically only ever used as the first input
to a pipeline.
* Add run_external internal command.
We want to push external commands closer to internal commands, eventually
eliminating the concept of "external" completely. This means we can consolidate
a couple of things:
- Variable evaluation (for example, `$it`, `$nu`, alias vars)
- Behaviour of whole stream vs per-item external execution
It should also make it easier for us to start introducing argument signatures
for external commands,
* Update run_external.rs
* Update run_external.rs
* Update run_external.rs
* Update run_external.rs
Co-authored-by: Jonathan Turner <jonathandturner@users.noreply.github.com>
2020-04-20 05:30:44 +02:00
|
|
|
Ok(mut stream) => {
|
2020-04-16 06:25:24 +02:00
|
|
|
let errors = context.get_errors();
|
|
|
|
if let Some(error) = errors.first() {
|
|
|
|
yield Err(error.clone());
|
|
|
|
return;
|
|
|
|
}
|
2020-04-13 09:59:57 +02:00
|
|
|
|
Move external closer to internal (#1611)
* Refactor InputStream and affected commands.
First, making `values` private and leaning on the `Stream` implementation makes
consumes of `InputStream` less likely to have to change in the future, if we
change what an `InputStream` is internally.
Second, we're dropping `Option<InputStream>` as the input to pipelines,
internals, and externals. Instead, `InputStream.is_empty` can be used to check
for "emptiness". Empty streams are typically only ever used as the first input
to a pipeline.
* Add run_external internal command.
We want to push external commands closer to internal commands, eventually
eliminating the concept of "external" completely. This means we can consolidate
a couple of things:
- Variable evaluation (for example, `$it`, `$nu`, alias vars)
- Behaviour of whole stream vs per-item external execution
It should also make it easier for us to start introducing argument signatures
for external commands,
* Update run_external.rs
* Update run_external.rs
* Update run_external.rs
* Update run_external.rs
Co-authored-by: Jonathan Turner <jonathandturner@users.noreply.github.com>
2020-04-20 05:30:44 +02:00
|
|
|
while let Some(result) = stream.next().await {
|
2020-04-13 09:59:57 +02:00
|
|
|
yield Ok(ReturnSuccess::Value(result));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
yield Err(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Value { tag, .. } => {
|
|
|
|
yield Err(ShellError::labeled_error(
|
|
|
|
"Expected a block",
|
|
|
|
"each needs a block",
|
|
|
|
tag,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(stream.to_output_stream())
|
|
|
|
}
|
|
|
|
}
|