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-15 07:43:23 +02:00
|
|
|
use crate::prelude::*;
|
|
|
|
|
|
|
|
use derive_new::new;
|
|
|
|
use nu_errors::ShellError;
|
2020-04-27 08:10:34 +02:00
|
|
|
use nu_protocol::{hir::Block, ReturnSuccess, Signature, SyntaxShape};
|
2020-04-15 07:43:23 +02:00
|
|
|
|
2020-04-27 04:04:54 +02:00
|
|
|
#[derive(new, Clone)]
|
2020-04-15 07:43:23 +02:00
|
|
|
pub struct AliasCommand {
|
|
|
|
name: String,
|
|
|
|
args: Vec<String>,
|
2020-04-20 08:41:51 +02:00
|
|
|
block: Block,
|
2020-04-15 07:43:23 +02:00
|
|
|
}
|
|
|
|
|
2020-05-29 10:22:52 +02:00
|
|
|
#[async_trait]
|
2020-04-27 04:04:54 +02:00
|
|
|
impl WholeStreamCommand for AliasCommand {
|
2020-04-15 07:43:23 +02:00
|
|
|
fn name(&self) -> &str {
|
|
|
|
&self.name
|
|
|
|
}
|
|
|
|
|
|
|
|
fn signature(&self) -> Signature {
|
|
|
|
let mut alias = Signature::build(&self.name);
|
|
|
|
|
|
|
|
for arg in &self.args {
|
2020-04-20 06:04:40 +02:00
|
|
|
alias = alias.optional(arg, SyntaxShape::Any, "");
|
2020-04-15 07:43:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
alias
|
|
|
|
}
|
|
|
|
|
|
|
|
fn usage(&self) -> &str {
|
|
|
|
""
|
|
|
|
}
|
|
|
|
|
2020-05-29 10:22:52 +02:00
|
|
|
async fn run(
|
2020-04-15 07:43:23 +02:00
|
|
|
&self,
|
2020-04-27 04:04:54 +02:00
|
|
|
args: CommandArgs,
|
2020-04-15 07:43:23 +02:00
|
|
|
registry: &CommandRegistry,
|
|
|
|
) -> Result<OutputStream, ShellError> {
|
2020-04-27 04:04:54 +02:00
|
|
|
let tag = args.call_info.name_tag.clone();
|
|
|
|
let call_info = args.call_info.clone();
|
2020-04-15 07:43:23 +02:00
|
|
|
let registry = registry.clone();
|
|
|
|
let block = self.block.clone();
|
2020-04-27 04:04:54 +02:00
|
|
|
let alias_command = self.clone();
|
|
|
|
let mut context = Context::from_args(&args, ®istry);
|
2020-04-27 08:10:34 +02:00
|
|
|
let input = args.input;
|
2020-04-15 07:43:23 +02:00
|
|
|
|
|
|
|
let stream = async_stream! {
|
2020-04-27 08:10:34 +02:00
|
|
|
let mut scope = call_info.scope.clone();
|
2020-05-16 05:18:24 +02:00
|
|
|
let evaluated = call_info.evaluate(®istry).await?;
|
2020-04-27 08:10:34 +02:00
|
|
|
if let Some(positional) = &evaluated.args.positional {
|
|
|
|
for (pos, arg) in positional.iter().enumerate() {
|
2020-05-27 06:50:26 +02:00
|
|
|
scope.vars.insert(alias_command.args[pos].to_string(), arg.clone());
|
2020-04-27 04:04:54 +02:00
|
|
|
}
|
2020-04-27 08:10:34 +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
|
|
|
|
2020-04-27 08:10:34 +02:00
|
|
|
let result = run_block(
|
|
|
|
&block,
|
|
|
|
&mut context,
|
|
|
|
input,
|
2020-05-27 06:50:26 +02:00
|
|
|
&scope.it,
|
|
|
|
&scope.vars,
|
|
|
|
&scope.env,
|
2020-04-27 08:10:34 +02:00
|
|
|
).await;
|
2020-04-15 07:43:23 +02:00
|
|
|
|
2020-04-27 08:10:34 +02:00
|
|
|
match result {
|
|
|
|
Ok(stream) if stream.is_empty() => {
|
|
|
|
yield Err(ShellError::labeled_error(
|
|
|
|
"Expected a block",
|
|
|
|
"alias needs a block",
|
|
|
|
tag,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
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));
|
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
|
|
|
}
|
|
|
|
|
2020-04-27 08:10:34 +02:00
|
|
|
let errors = context.get_errors();
|
|
|
|
if let Some(x) = errors.first() {
|
2020-04-21 06:11:34 +02:00
|
|
|
yield Err(ShellError::labeled_error_with_secondary(
|
|
|
|
"Alias failed to run",
|
|
|
|
"alias failed to run",
|
|
|
|
tag.clone(),
|
2020-04-27 08:10:34 +02:00
|
|
|
x.to_string(),
|
2020-04-21 06:11:34 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2020-04-27 08:10:34 +02:00
|
|
|
Err(e) => {
|
|
|
|
yield Err(ShellError::labeled_error_with_secondary(
|
|
|
|
"Alias failed to run",
|
|
|
|
"alias failed to run",
|
|
|
|
tag.clone(),
|
|
|
|
e.to_string(),
|
|
|
|
tag
|
|
|
|
));
|
|
|
|
}
|
2020-04-15 07:43:23 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(stream.to_output_stream())
|
|
|
|
}
|
|
|
|
}
|