2020-04-15 07:43:23 +02:00
|
|
|
use crate::commands::classified::pipeline::run_pipeline;
|
|
|
|
use crate::prelude::*;
|
|
|
|
|
|
|
|
use derive_new::new;
|
|
|
|
use nu_errors::ShellError;
|
|
|
|
use nu_protocol::{
|
|
|
|
hir::ClassifiedPipeline, hir::Commands, CallInfo, ReturnSuccess, Scope, Signature, SyntaxShape,
|
|
|
|
Value,
|
|
|
|
};
|
|
|
|
|
|
|
|
#[derive(new)]
|
|
|
|
pub struct AliasCommand {
|
|
|
|
name: String,
|
|
|
|
args: Vec<String>,
|
|
|
|
block: Commands,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PerItemCommand for AliasCommand {
|
|
|
|
fn name(&self) -> &str {
|
|
|
|
&self.name
|
|
|
|
}
|
|
|
|
|
|
|
|
fn signature(&self) -> Signature {
|
|
|
|
let mut alias = Signature::build(&self.name);
|
|
|
|
|
|
|
|
for arg in &self.args {
|
|
|
|
alias = alias.required(arg, SyntaxShape::Any, "");
|
|
|
|
}
|
|
|
|
|
|
|
|
alias
|
|
|
|
}
|
|
|
|
|
|
|
|
fn usage(&self) -> &str {
|
|
|
|
""
|
|
|
|
}
|
|
|
|
|
|
|
|
fn run(
|
|
|
|
&self,
|
|
|
|
call_info: &CallInfo,
|
|
|
|
registry: &CommandRegistry,
|
|
|
|
raw_args: &RawCommandArgs,
|
|
|
|
input: Value,
|
|
|
|
) -> Result<OutputStream, ShellError> {
|
|
|
|
let tag = call_info.name_tag.clone();
|
|
|
|
let call_info = call_info.clone();
|
|
|
|
let registry = registry.clone();
|
|
|
|
let raw_args = raw_args.clone();
|
|
|
|
let block = self.block.clone();
|
|
|
|
|
2020-04-16 06:25:24 +02:00
|
|
|
let mut scope = Scope::it_value(input.clone());
|
2020-04-15 07:43:23 +02:00
|
|
|
if let Some(positional) = &call_info.args.positional {
|
|
|
|
for (pos, arg) in positional.iter().enumerate() {
|
|
|
|
scope = scope.set_var(self.args[pos].to_string(), arg.clone());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let stream = async_stream! {
|
|
|
|
let mut context = Context::from_raw(&raw_args, ®istry);
|
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_clone = Ok(input.clone());
|
|
|
|
let input_stream = futures::stream::once(async { input_clone }).boxed().to_input_stream();
|
2020-04-15 07:43:23 +02:00
|
|
|
|
|
|
|
let result = run_pipeline(
|
|
|
|
ClassifiedPipeline::new(block.clone(), None),
|
|
|
|
&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-15 07:43:23 +02:00
|
|
|
&scope
|
|
|
|
).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(stream) if stream.is_empty() => {
|
2020-04-15 07:43:23 +02:00
|
|
|
yield Err(ShellError::labeled_error(
|
|
|
|
"Expected a block",
|
|
|
|
"each needs a block",
|
|
|
|
tag,
|
|
|
|
));
|
|
|
|
}
|
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) => {
|
|
|
|
// We collect first to ensure errors are put into the context
|
|
|
|
while let Some(result) = stream.next().await {
|
|
|
|
yield Ok(ReturnSuccess::Value(result));
|
|
|
|
}
|
|
|
|
|
|
|
|
let errors = context.get_errors();
|
|
|
|
if let Some(error) = errors.first() {
|
|
|
|
yield Err(error.clone());
|
|
|
|
}
|
|
|
|
}
|
2020-04-15 07:43:23 +02:00
|
|
|
Err(e) => {
|
|
|
|
yield Err(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(stream.to_output_stream())
|
|
|
|
}
|
|
|
|
}
|