input from ValueStream

This commit is contained in:
Fernando Herrera 2021-09-23 18:01:20 +01:00
parent 660e8b5b73
commit 36c32e9832

View file

@ -2,7 +2,7 @@ use std::borrow::Cow;
use std::cell::RefCell; use std::cell::RefCell;
use std::env; use std::env;
use std::io::{BufRead, BufReader, Write}; use std::io::{BufRead, BufReader, Write};
use std::process::{Command as CommandSys, Stdio}; use std::process::{ChildStdin, Command as CommandSys, Stdio};
use std::rc::Rc; use std::rc::Rc;
use std::sync::mpsc; use std::sync::mpsc;
@ -116,52 +116,33 @@ impl<'call, 'contex> ExternalCommand<'call, 'contex> {
)), )),
Ok(mut child) => { Ok(mut child) => {
// if there is a string or a stream, that is sent to the pipe std // if there is a string or a stream, that is sent to the pipe std
let mut stdin_write = child
.stdin
.take()
.expect("Internal error: could not get stdin pipe for external command");
match input { match input {
Value::Nothing { span: _ } => (), Value::Nothing { span: _ } => (),
Value::String { val, span: _ } => { Value::String { val, span: _ } => {
if stdin_write.write(val.as_bytes()).is_err() { if let Some(mut stdin_write) = child.stdin.take() {
return Err(ShellError::ExternalCommand( self.write_to_stdin(&mut stdin_write, val.as_bytes())?
"Error writing input to stdin".to_string(),
self.name.span,
));
} }
} }
Value::Binary { val, span: _ } => { Value::Binary { val, span: _ } => {
if stdin_write.write(&val).is_err() { if let Some(mut stdin_write) = child.stdin.take() {
return Err(ShellError::ExternalCommand( self.write_to_stdin(&mut stdin_write, &val)?
"Error writing input to stdin".to_string(),
self.name.span,
));
} }
} }
Value::Stream { stream, span: _ } => { Value::Stream { stream, span: _ } => {
if let Some(mut stdin_write) = child.stdin.take() {
for value in stream { for value in stream {
match value { match value {
Value::String { val, span: _ } => { Value::String { val, span: _ } => {
if stdin_write.write(val.as_bytes()).is_err() { self.write_to_stdin(&mut stdin_write, val.as_bytes())?
return Err(ShellError::ExternalCommand(
"Error writing input to stdin".to_string(),
self.name.span,
));
}
} }
Value::Binary { val, span: _ } => { Value::Binary { val, span: _ } => {
if stdin_write.write(&val).is_err() { self.write_to_stdin(&mut stdin_write, &val)?
return Err(ShellError::ExternalCommand(
"Error writing input to stdin".to_string(),
self.name.span,
));
}
} }
_ => continue, _ => continue,
} }
} }
} }
}
_ => { _ => {
return Err(ShellError::ExternalCommand( return Err(ShellError::ExternalCommand(
"Input is not string or binary".to_string(), "Input is not string or binary".to_string(),
@ -253,6 +234,17 @@ impl<'call, 'contex> ExternalCommand<'call, 'contex> {
process process
} }
} }
fn write_to_stdin(&self, stdin_write: &mut ChildStdin, val: &[u8]) -> Result<(), ShellError> {
if stdin_write.write(val).is_err() {
Err(ShellError::ExternalCommand(
"Error writing input to stdin".to_string(),
self.name.span,
))
} else {
Ok(())
}
}
} }
// The piped data from stdout from the external command can be either String // The piped data from stdout from the external command can be either String