lib/invoker/parsers/config.rb in invoker-1.0.2 vs lib/invoker/parsers/config.rb in invoker-1.0.3
- old
+ new
@@ -1,17 +1,18 @@
require 'iniparse'
+require 'dotenv'
module Invoker
module Parsers
class Config
PORT_REGEX = /\$PORT/
attr_accessor :processes, :power_config
def initialize(filename, port)
- @ini_content = File.read(filename)
+ @filename = filename
@port = port
- @processes = process_ini(@ini_content)
+ @processes = load_config
if Invoker.can_run_balancer?
@power_config = Invoker::Power::Config.load_config()
end
end
@@ -22,29 +23,56 @@
def dns_port
power_config && power_config.dns_port
end
def process(label)
- processes.detect {|pconfig|
- pconfig.label == label
- }
+ processes.detect { |pconfig| pconfig.label == label }
end
private
- def process_ini(ini_content)
+ def load_config
+ if is_ini?
+ process_ini
+ elsif is_procfile?
+ process_procfile
+ else
+ Invoker::Logger.puts("\n Invalid config file. Invoker requires an ini or a Procfile.".color(:red))
+ abort
+ end
+ end
+
+ def process_ini
+ ini_content = File.read(@filename)
document = IniParse.parse(ini_content)
document.map do |section|
check_directory(section["directory"])
- if supports_subdomain?(section)
- port = pick_port(section)
- make_option_for_subdomain(section, port)
- else
- make_option(section)
- end
+ process_command_from_section(section)
end
end
+ def process_procfile
+ load_env
+ procfile = Invoker::Parsers::Procfile.new(@filename)
+ procfile.entries.map do |name, command|
+ section = { "label" => name, "command" => command }
+ process_command_from_section(section)
+ end
+ end
+
+ def process_command_from_section(section)
+ if supports_subdomain?(section)
+ port = pick_port(section)
+ make_option_for_subdomain(section, port)
+ else
+ make_option(section)
+ end
+ end
+
+ def load_env
+ Dotenv.load
+ end
+
def pick_port(section)
if section['command'] =~ PORT_REGEX
@port += 1
elsif section['port']
section['port']
@@ -54,19 +82,19 @@
end
def make_option_for_subdomain(section, port)
OpenStruct.new(
port: port,
- label: section.key,
+ label: section["label"] || section.key,
dir: section["directory"],
cmd: replace_port_in_command(section["command"], port)
)
end
def make_option(section)
OpenStruct.new(
- label: section.key,
+ label: section["label"] || section.key,
dir: section["directory"],
cmd: section["command"]
)
end
@@ -86,8 +114,15 @@
else
command
end
end
+ def is_ini?
+ File.extname(@filename) == '.ini'
+ end
+
+ def is_procfile?
+ @filename =~ /Procfile/
+ end
end
end
end