ManageIQ / awesome_spawn

AwesomeSpawn is a module that provides some useful features over Ruby's Kernel.spawn.
MIT License
9 stars 16 forks source link

[RFE] Pipes and redirects #57

Open agrare opened 3 years ago

agrare commented 3 years ago

Unix pipes and redirects are obviously extremely powerful for chaining multiple commands together, being able to support this nicely in AwesomeSpawn combined with AwesomeSpawn's CLI builder to prevent command injection.

Wishlist:

  1. Easily provide stdin, stdout, and stderr for a process. Allow String, File / IO for stdin, File / IO for stdout/stderr.

Using pipes to write to a process' stdin is a great way to securely provide a password without it showing up in ENV vars, and giving the calling process' $stdout as the subcommands stdout allows for realtime output without waiting for the possibly long running command to finish

  1. Easily chain commands together (aka pipe stdout from one to stdin of the next

Some bash examples that we should be able to do easily with AwesomeSpawn:

echo "hello" | sed  's/hello/goodbye/' > output.txt
AwesomeSpawn.run("sed", :in => "hello", :params => ["s/hello/goodbye/"], :out => File.new("output.txt", "w"))
AwesomeSpawn.pipe("echo", :params => ["'hello'"]).pipe("sed", :params => ["'s/hello/goodbye/'"], :out => "output.txt").run
sudo dnf upgrade 2>/dev/null
AwesomeSpawn.run("sudo", :stdin => "hunter2", :params => ["dnf", "upgrade"], :stdout => $stdout, :stderr => nil)
Fryguy commented 3 years ago

As discussed, I like the AwesomeSpawn.pipe().pipe().run style.

Fryguy commented 3 years ago

:out and :err are the existing param names for Kernel spawn, so I'd like to match that (also :in, and we should deprecate :in_data and prefer :stdin_data

agrare commented 3 years ago

:+1: I'll update the examples