More touchups. Fix crash

This commit is contained in:
Jonathan Turner
2019-06-15 16:20:58 +12:00
parent 8cdd567b0e
commit d7c49fa3be
3 changed files with 56 additions and 14 deletions

View File

@ -15,6 +15,7 @@ use crate::evaluate::Scope;
use crate::git::current_branch;
use crate::object::Value;
use crate::parser::ast::{Expression, Leaf, RawExpression};
use crate::parser::lexer::Spanned;
use crate::parser::{Args, Pipeline};
use log::debug;
@ -416,13 +417,17 @@ fn classify_command(
}))
}
false => {
let arg_list_strings: Vec<String> = match args {
Some(args) => args.iter().map(|i| i.as_external_arg()).collect(),
let arg_list_strings: Vec<Spanned<String>> = match args {
Some(args) => args
.iter()
.map(|i| Spanned::from_item(i.as_external_arg(), i.span))
.collect(),
None => vec![],
};
Ok(ClassifiedCommand::External(ExternalCommand {
name: name.to_string(),
name_span: Some(span.clone()),
args: arg_list_strings,
}))
}

View File

@ -1,6 +1,6 @@
use crate::commands::command::Sink;
use crate::parser::ast::Expression;
use crate::parser::lexer::Span;
use crate::parser::lexer::{Span, Spanned};
use crate::parser::registry::Args;
use crate::prelude::*;
use bytes::{BufMut, BytesMut};
@ -153,7 +153,8 @@ impl InternalCommand {
crate struct ExternalCommand {
crate name: String,
crate args: Vec<String>,
crate name_span: Option<Span>,
crate args: Vec<Spanned<String>>,
}
crate enum StreamNext {
@ -174,7 +175,7 @@ impl ExternalCommand {
let mut arg_string = format!("{}", self.name);
for arg in &self.args {
arg_string.push_str(" ");
arg_string.push_str(&arg);
arg_string.push_str(&arg.item);
}
let mut process;
@ -185,6 +186,23 @@ impl ExternalCommand {
if arg_string.contains("$it") {
let mut first = true;
for i in &inputs {
if i.as_string().is_err() {
let mut span = None;
for arg in &self.args {
if arg.item.contains("$it") {
span = Some(arg.span);
}
}
if let Some(span) = span {
return Err(ShellError::labeled_error(
"External $it needs string data",
"given object instead of string data",
span,
));
} else {
return Err(ShellError::string("Error: $it needs string data"));
}
}
if !first {
process = process.arg("&&");
process = process.arg(&self.name);
@ -209,6 +227,23 @@ impl ExternalCommand {
if arg_string.contains("$it") {
let mut first = true;
for i in &inputs {
if i.as_string().is_err() {
let mut span = None;
for arg in &self.args {
if arg.item.contains("$it") {
span = Some(arg.span);
}
}
if let Some(span) = span {
return Err(ShellError::labeled_error(
"External $it needs string data",
"given object instead of string data",
span,
));
} else {
return Err(ShellError::string("Error: $it needs string data"));
}
}
if !first {
new_arg_string.push_str("&&");
new_arg_string.push_str(&self.name);