diff options
author | Eric Wong <normalperson@yhbt.net> | 2013-09-07 21:25:17 +0000 |
---|---|---|
committer | Eric Wong <normalperson@yhbt.net> | 2013-09-07 22:35:35 +0000 |
commit | d2f75170a6c1548b85335c7e84a000ed67a0158b (patch) | |
tree | fb8fba7c6d1f15826ad5f3118367c3ec57241524 /lib/dtas/process.rb | |
parent | 136f46adb568b964f61ecdafcd28936c44680b78 (diff) | |
download | dtas-d2f75170a6c1548b85335c7e84a000ed67a0158b.tar.gz |
This will allow users to more-easily edit configs and feel like a real shell. We no longer mistakenly expand nil env variables to "" anymore, either.
Diffstat (limited to 'lib/dtas/process.rb')
-rw-r--r-- | lib/dtas/process.rb | 28 |
1 files changed, 25 insertions, 3 deletions
diff --git a/lib/dtas/process.rb b/lib/dtas/process.rb index b90b318..a3c1c04 100644 --- a/lib/dtas/process.rb +++ b/lib/dtas/process.rb @@ -19,12 +19,33 @@ module DTAS::Process # :nodoc: end while true end + # expand common shell constructs based on environment variables + # this is order-dependent, but Ruby 1.9+ hashes are already order-dependent + def env_expand(env, opts) + env = env.dup + if false == opts.delete(:expand) + env.each do |key, val| + Numeric === val and env[key] = val.to_s + end + else + env.each do |key, val| + case val + when Numeric # stringify numeric values to simplify users' lives + env[key] = val.to_s + when /[\`\$]/ # perform variable/command expansion + tmp = env.dup + tmp.delete(key) + val = qx(tmp, "echo #{val}", expand: false) + env[key] = val.chomp + end + end + end + end + # for long-running processes (sox/play/ecasound filters) def dtas_spawn(env, cmd, opts) opts = { close_others: true, pgroup: true }.merge!(opts) - - # stringify env, integer values are easier to type unquoted as strings - env.each { |k,v| env[k] = v.to_s } + env = env_expand(env, opts) pid = begin Process.spawn(env, cmd, opts) @@ -53,6 +74,7 @@ module DTAS::Process # :nodoc: re.binmode opts[:err] = we end + env = env_expand(env, opts) pid = begin Process.spawn(env, *cmd, opts) rescue Errno::EINTR # Ruby bug? |