Permalink
Browse files

Move the path manipulation into a setter

  • Loading branch information...
1 parent 8283050 commit 2cfec9cce00e58aea78e64b1e259974c937f73b9 @jyurek jyurek committed Apr 13, 2012
Showing with 17 additions and 15 deletions.
  1. +17 −15 lib/cocaine/command_line.rb
@@ -7,9 +7,22 @@ class CommandLine
rescue LoadError => e
# posix-spawn gem not available
end
-
+
class << self
- attr_accessor :path, :logger, :environment
+ attr_accessor :logger
+
+ def path
+ @supplemental_path
+ end
+ def path=(supplemental_path)
+ @supplemental_path = supplemental_path
+ @supplemental_environment ||= {}
+ @supplemental_environment['PATH'] = [ENV['PATH'], *supplemental_path].join(File::PATH_SEPARATOR)
+ end
+
+ def environment
+ @supplemental_environment ||= {}
+ end
end
@environment = {}
@@ -37,7 +50,6 @@ def command
def run
output = ''
begin
- set_modified_path
with_modified_environment do
@logger.info("\e[32mCommand\e[0m :: #{command}") if @logger
output = send(:'`', command)
@@ -61,27 +73,17 @@ def unix?
end
private
-
+
def with_modified_environment
begin
- # Save current environment
- saved_env = ENV.select {|k,v| self.class.environment.key?(k)}
- # Update environment
+ saved_env = ENV.to_hash
ENV.update(self.class.environment)
yield
ensure
- # Restore old environment
ENV.update(saved_env)
end
end
- def set_modified_path
- unless self.class.path.nil? or self.class.path == ''
- extra_path = [self.class.path].flatten
- self.class.environment['PATH'] = [ENV['PATH'], *extra_path].join(File::PATH_SEPARATOR)
- end
- end
-
def interpolate(pattern, vars)
# interpolates :variables and :{variables}
pattern.gsub(%r#:(?:\w+|\{\w+\})#) do |match|

0 comments on commit 2cfec9c

Please sign in to comment.