-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add module to invoke a command with piped stdin and stdout.
- Loading branch information
Showing
4 changed files
with
56 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,3 +10,5 @@ p ARGV | |
|
||
puts "STDIN:" | ||
p STDIN.read | ||
|
||
STDERR.puts "Done" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
use std::process::{Command, Stdio}; | ||
use std::io::Write; | ||
use std::path::PathBuf; | ||
|
||
pub fn run(script: &PathBuf) { | ||
println!("Running script {}...",script.display()); | ||
|
||
let mut child = Command::new(script) | ||
.arg("-test") | ||
.arg("echo hello") | ||
.stdin(Stdio::piped()) | ||
.stdout(Stdio::piped()) | ||
.spawn() | ||
.expect("failed to start script {script}"); | ||
|
||
let mut stdin = child.stdin.take().expect("Failed to open stdin"); | ||
|
||
std::thread::spawn(move || { | ||
stdin.write_all("Hello, world!".as_bytes()).expect("Failed to write to stdin"); | ||
}); | ||
|
||
let output = child.wait_with_output().expect("Failed to read stdout"); | ||
|
||
let stdout = String::from_utf8_lossy(&output.stdout); | ||
let stderr = String::from_utf8_lossy(&output.stderr); | ||
|
||
println!("Exit code: {}", output.status); | ||
println!("STDOUT: {stdout}"); | ||
println!("STDERR: {stderr}"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters