vengeance
vengeance

Reputation: 94

using piped input in conjunction with user prompted input in ruby

I am currently working with a script that defines expected configuration values, reads a configuration file for values, and finally prompts for any values expected but not specified in the configuration file.

Example expected configuration items:

username password command directories

Example configuration file:

username:bob
command:"rm -rf / && echo bwahahahahaha"

Example prompt:

password? userinput  
directories? userinput

The crux of the issue is that not all configuration items are known prior to runtime, and can be piped in from a text file or output of another program, but may still leave a gap in configuration items (triggering a prompt).

[host]$ parametergeneratingscript.rb | paramaterreceivingscript.rb        # still needs to prompt for password  

Using IO#tty? in I am able to detect the piped input and appropriately read from STDIN, but this is walking all over the user prompts (which also come from STDIN). I'm comfortable changing around the config / prompt scheme, but before I run off and nuke it, was wondering if there was a graceful way to "toggle" the source of STDIN between tty and piped input?

Upvotes: 1

Views: 359

Answers (1)

Chris
Chris

Reputation: 21

Have you tried STDIN.reopen('/dev/tty')? I believe this will reopen STDIN to read from the keyboard instead of from the pipe.

Upvotes: 2

Related Questions