Switch stdin redirect to manual. Add test (#1367)

This commit is contained in:
Jonathan Turner
2020-02-09 22:55:07 -08:00
committed by GitHub
parent a2668e3327
commit dc50e61f26
5 changed files with 113 additions and 42 deletions

View File

@ -373,7 +373,10 @@ pub fn create_default_context(
Ok(context)
}
pub async fn run_pipeline_standalone(pipeline: String) -> Result<(), Box<dyn Error>> {
pub async fn run_pipeline_standalone(
pipeline: String,
redirect_stdin: bool,
) -> Result<(), Box<dyn Error>> {
let mut syncer = crate::env::environment_syncer::EnvironmentSyncer::new();
let mut context = create_default_context(&mut syncer)?;
@ -390,7 +393,7 @@ pub async fn run_pipeline_standalone(pipeline: String) -> Result<(), Box<dyn Err
context.ctrl_c.store(false, Ordering::SeqCst);
}
let line = process_line(Ok(pipeline), &mut context, true).await;
let line = process_line(Ok(pipeline), &mut context, redirect_stdin).await;
match line {
LineResult::Success(line) => {
@ -644,7 +647,7 @@ async fn process_line(
return LineResult::Error(line.to_string(), failure.into());
}
let input_stream = if redirect_stdin && !atty::is(atty::Stream::Stdin) {
let input_stream = if redirect_stdin {
let file = futures::io::AllowStdIo::new(std::io::stdin());
let stream = FramedRead::new(file, LinesCodec).map(|line| {
if let Ok(line) = line {

View File

@ -32,6 +32,12 @@ fn main() -> Result<(), Box<dyn Error>> {
.multiple(true)
.takes_value(true),
)
.arg(
Arg::with_name("stdin")
.long("stdin")
.multiple(false)
.takes_value(false),
)
.get_matches();
let loglevel = match matches.value_of("loglevel") {
@ -74,7 +80,10 @@ fn main() -> Result<(), Box<dyn Error>> {
None => {}
Some(values) => {
for item in values {
futures::executor::block_on(nu::run_pipeline_standalone(item.into()))?;
futures::executor::block_on(nu::run_pipeline_standalone(
item.into(),
matches.is_present("stdin"),
))?;
}
return Ok(());
}