Skip to content

Commit

Permalink
allow multiple environment files to be specified
Browse files Browse the repository at this point in the history
  • Loading branch information
ddollar committed Sep 16, 2011
1 parent 91edac3 commit 5719f4f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
27 changes: 17 additions & 10 deletions lib/foreman/engine.rb
Expand Up @@ -22,7 +22,7 @@ def initialize(procfile, options={})
@procfile = read_procfile(procfile) @procfile = read_procfile(procfile)
@directory = File.expand_path(File.dirname(procfile)) @directory = File.expand_path(File.dirname(procfile))
@options = options @options = options
@environment = read_environment(options[:env]) @environment = read_environment_files(options[:env])
end end


def processes def processes
Expand Down Expand Up @@ -202,22 +202,29 @@ def warn_deprecated_procfile!
puts "!!! e.g. web: thin start" puts "!!! e.g. web: thin start"
end end


def read_environment(filename) def read_environment_files(filenames)
error "No such file: #{filename}" if filename && !File.exists?(filename)
filename ||= ".env"
environment = {} environment = {}


if File.exists?(filename) (filenames || "").split(",").map(&:strip).each do |filename|
File.read(filename).split("\n").each do |line| error "No such file: #{filename}" unless File.exists?(filename)
if line =~ /\A([A-Za-z_]+)=(.*)\z/ environment.merge!(read_environment(filename))
environment[$1] = $2
end
end
end end


environment.merge!(read_environment(".env")) unless filenames
environment environment
end 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 def runner
File.expand_path("../../../bin/foreman-runner", __FILE__) File.expand_path("../../../bin/foreman-runner", __FILE__)
end end
Expand Down
10 changes: 10 additions & 0 deletions spec/foreman/engine_spec.rb
Expand Up @@ -79,6 +79,16 @@
engine.execute("alpha") engine.execute("alpha")
end 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 it "should fail if specified and doesnt exist" do
mock.instance_of(Foreman::Engine).error("No such file: /tmp/env") mock.instance_of(Foreman::Engine).error("No such file: /tmp/env")
engine = Foreman::Engine.new("Procfile", :env => "/tmp/env") engine = Foreman::Engine.new("Procfile", :env => "/tmp/env")
Expand Down

0 comments on commit 5719f4f

Please sign in to comment.