lib/invoker/parsers/config.rb in invoker-1.3.2 vs lib/invoker/parsers/config.rb in invoker-1.4.0
- old
+ new
@@ -2,15 +2,26 @@
module Invoker
module Parsers
class Config
PORT_REGEX = /\$PORT/
+
attr_accessor :processes, :power_config
+ attr_reader :filename
+ # initialize takes a port form cli and decrements it by 1 and sets the
+ # instance variable @port. This port value is used as the environment
+ # variable $PORT mentioned inside invoker.ini. When method pick_port gets
+ # fired it increments the value of port by 1, subsequently when pick_port
+ # again gets fired, for another command, it will again increment port
+ # value by 1, that way generating different ports for different commands.
def initialize(filename, port)
- @filename = filename
- @port = port
+ @filename = filename || autodetect_config_file
+
+ print_message_and_abort if invalid_config_file?
+
+ @port = port - 1
@processes = load_config
if Invoker.can_run_balancer?
@power_config = Invoker::Power::Config.load_config()
end
end
@@ -35,18 +46,27 @@
processes.detect { |pconfig| pconfig.label == label }
end
private
+ def autodetect_config_file
+ Dir.glob("{invoker.ini,Procfile}").first
+ end
+
+ def invalid_config_file?
+ @filename.nil?
+ end
+
def load_config
+ @filename = to_global_file if is_global?
+
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
+ print_message_and_abort
end
end
def process_ini
ini_content = File.read(@filename)
@@ -63,10 +83,15 @@
section = { "label" => name, "command" => command }
process_command_from_section(section)
end
end
+ def print_message_and_abort
+ Invoker::Logger.puts("\n Invalid config file. Invoker requires an ini or a Procfile.".color(:red))
+ abort
+ end
+
def process_command_from_section(section)
if supports_subdomain?(section)
port = pick_port(section)
if port
command = replace_port_in_command(section['command'], port)
@@ -125,9 +150,17 @@
File.extname(@filename) == '.ini'
end
def is_procfile?
@filename =~ /Procfile/
+ end
+
+ def to_global_file
+ File.join(Invoker::Power::Config.config_dir, "#{@filename}.ini")
+ end
+
+ def is_global?
+ @filename =~ /^\w+$/ && File.exist?(to_global_file)
end
end
end
end