#![allow(clippy::wildcard_imports)]
use std::time::Duration;
use tokio::fs::File;
use tokio::process::Command;
use tokio_process_tools::visitors::write::WriteLines;
use tokio_process_tools::*;
#[tokio::main]
async fn main() {
let mut process = Process::new(Command::new("ls"))
.name(AutoName::program_only())
.stdout_and_stderr(|stream| {
stream
.broadcast()
.reliable_with_backpressure()
.no_replay()
.read_chunk_size(DEFAULT_READ_CHUNK_SIZE)
.max_buffered_chunks(DEFAULT_MAX_BUFFERED_CHUNKS)
})
.spawn()
.expect("failed to spawn command");
let log_path = std::env::temp_dir().join("tokio-process-tools-stream.log");
let log_file = File::create(&log_path)
.await
.expect("failed to open log file");
let log_collector = process
.stdout()
.consume_async(ParseLines::new(
LineParsingOptions::default(),
WriteLines::passthrough(
"stdout",
log_file,
WriteCollectionOptions::log_and_continue(),
LineWriteMode::AppendLf,
),
))
.expect("no other consumer is attached yet");
let _status = process
.wait_for_completion(Duration::from_secs(60))
.await
.unwrap()
.expect_completed("process should complete");
let _log_file = log_collector.wait().await.unwrap().unwrap();
println!("wrote stdout to {}", log_path.display());
}