From d2f75170a6c1548b85335c7e84a000ed67a0158b Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Sat, 7 Sep 2013 21:25:17 +0000 Subject: implement environment variable expansion 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. --- lib/dtas/process.rb | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) (limited to 'lib/dtas/process.rb') 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? -- cgit v1.2.3-24-ge0c7