2020-04-20 08:41:51 +02:00
|
|
|
use crate::commands::classified::block::run_block;
|
2020-04-27 04:04:54 +02:00
|
|
|
use crate::commands::WholeStreamCommand;
|
2020-04-13 09:59:57 +02:00
|
|
|
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-29 21:09:14 +02:00
|
|
|
use nu_protocol::{
|
|
|
|
hir::Block, hir::Expression, hir::SpannedExpression, hir::Synthetic, ReturnSuccess, Signature,
|
|
|
|
SyntaxShape,
|
|
|
|
};
|
2020-04-13 09:59:57 +02:00
|
|
|
|
|
|
|
pub struct Each;
|
|
|
|
|
2020-04-27 04:04:54 +02:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
pub struct EachArgs {
|
|
|
|
block: Block,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl WholeStreamCommand for Each {
|
2020-04-13 09:59:57 +02:00
|
|
|
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,
|
2020-04-27 04:04:54 +02:00
|
|
|
args: CommandArgs,
|
2020-04-13 09:59:57 +02:00
|
|
|
registry: &CommandRegistry,
|
|
|
|
) -> Result<OutputStream, ShellError> {
|
2020-04-27 04:04:54 +02:00
|
|
|
Ok(args.process_raw(registry, each)?.run())
|
|
|
|
}
|
2020-05-12 07:17:17 +02:00
|
|
|
|
|
|
|
fn examples(&self) -> &[Example] {
|
|
|
|
&[Example {
|
|
|
|
description: "Print the name of each file",
|
|
|
|
example: "ls | each { echo $it.name }",
|
|
|
|
}]
|
|
|
|
}
|
2020-04-27 04:04:54 +02:00
|
|
|
}
|
2020-04-13 09:59:57 +02:00
|
|
|
|
2020-04-29 21:09:14 +02:00
|
|
|
fn is_expanded_it_usage(head: &SpannedExpression) -> bool {
|
|
|
|
match &*head {
|
|
|
|
SpannedExpression {
|
|
|
|
expr: Expression::Synthetic(Synthetic::String(s)),
|
|
|
|
..
|
|
|
|
} if s == "expanded-each" => true,
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-27 04:04:54 +02:00
|
|
|
fn each(
|
|
|
|
each_args: EachArgs,
|
|
|
|
context: RunnableContext,
|
|
|
|
raw_args: RawCommandArgs,
|
|
|
|
) -> Result<OutputStream, ShellError> {
|
|
|
|
let block = each_args.block;
|
2020-04-27 08:10:34 +02:00
|
|
|
let scope = raw_args.call_info.scope.clone();
|
2020-04-27 04:04:54 +02:00
|
|
|
let registry = context.registry.clone();
|
|
|
|
let mut input_stream = context.input;
|
|
|
|
let stream = async_stream! {
|
|
|
|
while let Some(input) = input_stream.next().await {
|
|
|
|
let mut context = Context::from_raw(&raw_args, ®istry);
|
2020-04-29 21:09:14 +02:00
|
|
|
|
2020-04-27 04:04:54 +02:00
|
|
|
let input_clone = input.clone();
|
2020-04-29 21:09:14 +02:00
|
|
|
let input_stream = if is_expanded_it_usage(&raw_args.call_info.args.head) {
|
|
|
|
InputStream::empty()
|
|
|
|
} else {
|
|
|
|
once(async { Ok(input) }).to_input_stream()
|
|
|
|
};
|
2020-04-13 09:59:57 +02:00
|
|
|
|
2020-04-27 04:04:54 +02:00
|
|
|
let result = run_block(
|
|
|
|
&block,
|
|
|
|
&mut context,
|
|
|
|
input_stream,
|
2020-04-27 08:10:34 +02:00
|
|
|
&scope.clone().set_it(input_clone),
|
2020-04-27 04:04:54 +02:00
|
|
|
).await;
|
2020-04-13 09:59:57 +02:00
|
|
|
|
2020-04-27 04:04:54 +02:00
|
|
|
match result {
|
|
|
|
Ok(mut stream) => {
|
2020-05-04 05:29:32 +02:00
|
|
|
while let Some(result) = stream.next().await {
|
|
|
|
yield Ok(ReturnSuccess::Value(result));
|
|
|
|
}
|
|
|
|
|
2020-04-27 04:04:54 +02:00
|
|
|
let errors = context.get_errors();
|
|
|
|
if let Some(error) = errors.first() {
|
|
|
|
yield Err(error.clone());
|
|
|
|
}
|
2020-04-13 09:59:57 +02:00
|
|
|
}
|
2020-04-27 04:04:54 +02:00
|
|
|
Err(e) => {
|
|
|
|
yield Err(e);
|
2020-04-13 09:59:57 +02:00
|
|
|
}
|
2020-04-27 04:04:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2020-04-13 09:59:57 +02:00
|
|
|
|
2020-04-27 04:04:54 +02:00
|
|
|
Ok(stream.to_output_stream())
|
2020-04-13 09:59:57 +02:00
|
|
|
}
|