I am developing a wrapper for Terraform (https://www.terraform.io/), which at some point during its execution, it may request user input. So, my application must forward everything typed on its stdin to the subprocess' stdin. The following solution works on Linux, but on Windows the subprocess (Terraform) seems to never receive the input.
A similar approach works for wrappers implemented in Python or Go, so I believe this may actually be a bug in Ruby itself.
The file test.rb contains the code to reproduce the issue. First, Terraform is downloaded in the working directory. Then, popen3 is used to run Terraform.
I din't have a windows machine right now so not sure this is the solution for you. But JFYI you can reroute the IOs by specifying arguments to spawn like this:
Using Process.spawn indeed works :) Unfortunately, I cannot figure out a way to forward the standard output and standard error to the console (i.e, STDOUT and STDERR) as well as storing that output in a variable.
The example in test.rb is a minimal example that just reproduces the issue where the subprocess does not receive any data on its standard input. But, the real code is a little bit more complex as it needs to store the output generated by the subprocess too.
Is there any easy way to do so? Is fork + exec and using pipes the only way to go?
system also accepts redirections.
And child processes inherit STDIN/STDOUT/STDERR from their parent process by the default, you don't need such arguments at all usually.
If you want to capture the outputs and the errors, IO.popen(%w'terraform destroy', err: [:child, :out], &:read).
The following code snippet based on IO.popen seems to work. It executes a command, and it returns its output as an array containing the output lines. Optionally, the output is written to stdout too.