Permalink
Browse files

allow multiple environment files to be specified

  • Loading branch information...
1 parent 91edac3 commit 5719f4fc7233accaa528d25af8afc7837799f77a @ddollar ddollar committed Sep 16, 2011
Showing with 27 additions and 10 deletions.
  1. +17 −10 lib/foreman/engine.rb
  2. +10 −0 spec/foreman/engine_spec.rb
View
@@ -22,7 +22,7 @@ def initialize(procfile, options={})
@procfile = read_procfile(procfile)
@directory = File.expand_path(File.dirname(procfile))
@options = options
- @environment = read_environment(options[:env])
+ @environment = read_environment_files(options[:env])
end
def processes
@@ -202,22 +202,29 @@ def warn_deprecated_procfile!
puts "!!! e.g. web: thin start"
end
- def read_environment(filename)
- error "No such file: #{filename}" if filename && !File.exists?(filename)
- filename ||= ".env"
+ def read_environment_files(filenames)
environment = {}
- if File.exists?(filename)
- File.read(filename).split("\n").each do |line|
- if line =~ /\A([A-Za-z_]+)=(.*)\z/
- environment[$1] = $2
- end
- end
+ (filenames || "").split(",").map(&:strip).each do |filename|
+ error "No such file: #{filename}" unless File.exists?(filename)
+ environment.merge!(read_environment(filename))
end
+ environment.merge!(read_environment(".env")) unless filenames
environment
end
+ def read_environment(filename)
+ return {} unless File.exists?(filename)
+
+ File.read(filename).split("\n").inject({}) do |hash, line|
+ if line =~ /\A([A-Za-z_]+)=(.*)\z/
+ hash[$1] = $2
+ end
+ hash
+ end
+ end
+
def runner
File.expand_path("../../../bin/foreman-runner", __FILE__)
end
@@ -79,6 +79,16 @@
engine.execute("alpha")
end
+ it "should read more than one if specified" do
+ File.open("/tmp/env1", "w") { |f| f.puts("FOO=bar") }
+ File.open("/tmp/env2", "w") { |f| f.puts("BAZ=qux") }
+ engine = Foreman::Engine.new("Procfile", :env => "/tmp/env1,/tmp/env2")
+ stub(engine).info
+ mock(engine).watch_for_termination
+ engine.environment.should == { "FOO"=>"bar", "BAZ"=>"qux" }
+ engine.execute("alpha")
+ end
+
it "should fail if specified and doesnt exist" do
mock.instance_of(Foreman::Engine).error("No such file: /tmp/env")
engine = Foreman::Engine.new("Procfile", :env => "/tmp/env")

0 comments on commit 5719f4f

Please sign in to comment.