mirror of
https://github.com/nushell/nushell.git
synced 2025-01-03 21:09:52 +01:00
external call
This commit is contained in:
parent
9c98783917
commit
8f07f40f22
@ -1,3 +1,5 @@
|
||||
use std::process::Command;
|
||||
|
||||
use nu_protocol::ast::{Block, Call, Expr, Expression, Operator, Statement};
|
||||
use nu_protocol::engine::EvaluationContext;
|
||||
use nu_protocol::{Range, ShellError, Span, Value};
|
||||
@ -68,6 +70,33 @@ fn eval_call(context: &EvaluationContext, call: &Call, input: Value) -> Result<V
|
||||
}
|
||||
}
|
||||
|
||||
pub fn eval_external(
|
||||
context: &EvaluationContext,
|
||||
command_span: &Span,
|
||||
spans: &Vec<Span>,
|
||||
expr: &Expression,
|
||||
) -> Result<Value, ShellError> {
|
||||
let state = context.engine_state.borrow();
|
||||
let name = state.get_span_contents(command_span);
|
||||
let cmd = std::str::from_utf8(name).unwrap();
|
||||
|
||||
let args = spans
|
||||
.iter()
|
||||
.map(|span| {
|
||||
let val = state.get_span_contents(span);
|
||||
std::str::from_utf8(val).unwrap()
|
||||
})
|
||||
.collect::<Vec<&str>>();
|
||||
|
||||
let output = Command::new(cmd).args(args).output().unwrap();
|
||||
|
||||
println!("{:?}", output);
|
||||
let test = output.stdout;
|
||||
println!("{:?}", test);
|
||||
|
||||
Err(ShellError::ExternalNotSupported(expr.span))
|
||||
}
|
||||
|
||||
pub fn eval_expression(
|
||||
context: &EvaluationContext,
|
||||
expr: &Expression,
|
||||
@ -137,7 +166,9 @@ pub fn eval_expression(
|
||||
}
|
||||
Expr::RowCondition(_, expr) => eval_expression(context, expr),
|
||||
Expr::Call(call) => eval_call(context, call, Value::nothing()),
|
||||
Expr::ExternalCall(_, _) => Err(ShellError::ExternalNotSupported(expr.span)),
|
||||
Expr::ExternalCall(command_span, spans) => {
|
||||
eval_external(context, command_span, spans, expr)
|
||||
}
|
||||
Expr::Operator(_) => Ok(Value::Nothing { span: expr.span }),
|
||||
Expr::BinaryOp(lhs, op, rhs) => {
|
||||
let op_span = op.span;
|
||||
|
@ -137,7 +137,7 @@ impl EngineState {
|
||||
output
|
||||
}
|
||||
|
||||
pub fn get_span_contents(&self, span: Span) -> &[u8] {
|
||||
pub fn get_span_contents(&self, span: &Span) -> &[u8] {
|
||||
&self.file_contents[span.start..span.end]
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user