refactor engine to expose env methods

This commit is contained in:
Chris Continanza
2011-12-03 15:43:07 -08:00
parent 39ace26ae1
commit 79cf541ebe
+33 -19
View File
@@ -12,7 +12,6 @@ class Foreman::Engine
attr_reader :procfile attr_reader :procfile
attr_reader :directory attr_reader :directory
attr_reader :environment
attr_reader :options attr_reader :options
extend Term::ANSIColor extend Term::ANSIColor
@@ -26,6 +25,11 @@ class Foreman::Engine
@environment = read_environment_files(options[:env]) @environment = read_environment_files(options[:env])
end end
def self.load_env(env_file)
@environment = read_environment_files(env_file)
load_env!
end
def start def start
proctitle "ruby: foreman master" proctitle "ruby: foreman master"
termtitle "#{File.basename(@directory)} - foreman (#{processes.size} processes)" termtitle "#{File.basename(@directory)} - foreman (#{processes.size} processes)"
@@ -73,7 +77,7 @@ private ######################################################################
end end
def fork_individual(process, num, port) def fork_individual(process, num, port)
@environment.each { |k,v| ENV[k] = v } load_env!
ENV["PORT"] = port.to_s ENV["PORT"] = port.to_s
ENV["PS"] = "#{process.name}.#{num}" ENV["PS"] = "#{process.name}.#{num}"
@@ -175,27 +179,37 @@ private ######################################################################
@current_color >= COLORS.length ? "" : COLORS[@current_color] @current_color >= COLORS.length ? "" : COLORS[@current_color]
end end
def read_environment_files(filenames) module Env
environment = {} attr_reader :environment
(filenames || "").split(",").map(&:strip).each do |filename| def read_environment_files(filenames)
error "No such file: #{filename}" unless File.exists?(filename) environment = {}
environment.merge!(read_environment(filename))
end
environment.merge!(read_environment(".env")) unless filenames (filenames || "").split(",").map(&:strip).each do |filename|
environment error "No such file: #{filename}" unless File.exists?(filename)
end environment.merge!(read_environment(filename))
def read_environment(filename)
return {} unless File.exists?(filename)
File.read(filename).split("\n").inject({}) do |hash, line|
if line =~ /\A([A-Za-z_0-9]+)=(.*)\z/
hash[$1] = $2
end end
hash
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_0-9]+)=(.*)\z/
hash[$1] = $2
end
hash
end
end
def load_env!
@environment.each { |k,v| ENV[k] = v }
end end
end end
include Env
extend Env
end end