Permalink
Browse files

Allow customization of environment variables

  • Loading branch information...
1 parent 2391d9f commit 28cadbed8307c5cd244db6667898e8de36c52dc0 @ivovh ivovh committed Apr 13, 2012
Showing with 19 additions and 8 deletions.
  1. +19 −8 lib/cocaine/command_line.rb
@@ -9,8 +9,9 @@ class CommandLine
end
class << self
- attr_accessor :path, :logger
+ attr_accessor :path, :logger, :environment
end
+ @environment = {}
attr_reader :exit_status
@@ -36,7 +37,8 @@ def command
def run
output = ''
begin
- with_modified_path do
+ set_modified_path
+ with_modified_environment do
@logger.info("\e[32mCommand\e[0m :: #{command}") if @logger
output = send(:'`', command)
end
@@ -59,15 +61,24 @@ def unix?
end
private
-
- def with_modified_path
+
+ def with_modified_environment
begin
- saved_path = ENV['PATH']
- extra_path = [self.class.path].flatten
- ENV['PATH'] = [ENV['PATH'], *extra_path].join(File::PATH_SEPARATOR)
+ # Save current environment
+ saved_env = ENV.select {|k,v| self.class.environment.key?(k)}
+ # Update environment
+ ENV.update(self.class.environment)
yield
ensure
- ENV['PATH'] = saved_path
+ # 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

0 comments on commit 28cadbe

Please sign in to comment.