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,49 +116,30 @@ 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: _ } => {
for value in stream { if let Some(mut stdin_write) = child.stdin.take() {
match value { for value in stream {
Value::String { val, span: _ } => { match value {
if stdin_write.write(val.as_bytes()).is_err() { Value::String { val, span: _ } => {
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: _ } => { self.write_to_stdin(&mut stdin_write, &val)?
if stdin_write.write(&val).is_err() {
return Err(ShellError::ExternalCommand(
"Error writing input to stdin".to_string(),
self.name.span,
));
} }
_ => continue,
} }
_ => continue,
} }
} }
} }
@ -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