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-06-13 10:43:21 +02:00
|
|
|
use nu_protocol::{hir::Block, 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 call_info = args.call_info.clone();
|
2020-04-15 07:43:23 +02:00
|
|
|
let registry = registry.clone();
|
2020-06-27 23:04:57 +02:00
|
|
|
let mut block = self.block.clone();
|
|
|
|
block.set_is_last(!call_info.args.is_last);
|
|
|
|
|
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
|
|
|
|
2020-06-13 10:43:21 +02:00
|
|
|
let mut scope = call_info.scope.clone();
|
|
|
|
let evaluated = call_info.evaluate(®istry).await?;
|
|
|
|
if let Some(positional) = &evaluated.args.positional {
|
|
|
|
for (pos, arg) in positional.iter().enumerate() {
|
|
|
|
scope
|
|
|
|
.vars
|
|
|
|
.insert(alias_command.args[pos].to_string(), arg.clone());
|
2020-04-27 08:10:34 +02:00
|
|
|
}
|
2020-06-13 10:43:21 +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-06-13 10:43:21 +02:00
|
|
|
// FIXME: we need to patch up the spans to point at the top-level error
|
|
|
|
Ok(run_block(
|
|
|
|
&block,
|
|
|
|
&mut context,
|
|
|
|
input,
|
|
|
|
&scope.it,
|
|
|
|
&scope.vars,
|
|
|
|
&scope.env,
|
|
|
|
)
|
|
|
|
.await?
|
|
|
|
.to_output_stream())
|
2020-04-15 07:43:23 +02:00
|
|
|
}
|
|
|
|
}
|