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

support environment variables #30

Closed kbrock closed 9 years ago

kbrock commented 9 years ago

When we detach a process, we tend to pass a set of environment variable.

This adds the :env option to AwesomeSpawn#run so users can specify both the env and argv for a spawned task.

Fryguy commented 9 years ago

:+1:

Fryguy commented 9 years ago

Can you update documentation as well? (You can do it in a separate commit with [skip ci] to avoid rekicking travis).

kbrock commented 9 years ago

@Fryguy great question answer: {} acts the same as not passing the parameter

#!/usr/bin/env ruby
require "open3"
command = "echo ${ABC}"
output, error, status = Open3.capture3(command)
puts "1 output no env: #{output}"

output, error, status = Open3.capture3({}, command)
puts "2 output {} env: #{output}"

output, error, status = Open3.capture3({'ABC' => 'custom'}, command)
puts "3 output var env: #{output}"
$ ABC=123 ./test_spawn.rb 
ABC=123
---
1 output no env: 123
2 output {} env: 123
3 output var env: custom
kbrock commented 9 years ago

@Fryguy followup on env variables

ENV["CDE"]="cde"
AwesomeSpawn.run('echo "${ABC} ${CDE}"', :env => {"ABC" => "abcde"}).output
# => "abcde cde\n"
AwesomeSpawn.run('echo "${ABC} ${CDE}"', :env => {"ABC" => "abcde"}, :unsetenv_others=>true).output
# => "abcde \n"