Permalink
Browse files

Added the support for optional environments in the config file

  • Loading branch information...
oleriesenberg committed Jun 6, 2012
1 parent ba17280 commit 517bac7a2479eb785ea1cfef44c4df9809982198
View
@@ -40,6 +40,10 @@ def self.parse(source)
options[:config_file] = file
end
+ opts.on '-E', '--environment ENV', 'Config environment to use' do |config_env|
+ options[:config_environment] = config_env
+ end
+
opts.on('-h', '--help', 'Display all options') do
puts opts
exit
@@ -48,7 +52,7 @@ def self.parse(source)
parser.parse(source)
if config_file = options[:config_file]
- options = from_file(config_file)
+ options = from_file(config_file, options[:config_environment])
end
if required_options_missing?(options)
@@ -69,12 +73,16 @@ def self.required_options_missing?(options)
# Parses options from a YAML file.
#
# @param [String] file the filename
+ # @params [String] _env the environment
# @return [Hash] the parsed options
- def self.from_file(file)
+ def self.from_file(file, _env=nil)
unless File.exists?(file)
raise ArgumentError, "File #{file} can't be found"
end
options = YAML.load_file(file)
+ if _env && !(options = options[_env.intern])
+ raise ArgumentError, "Environment #{_env} can't be found in config"
+ end
options[:nodes] = options[:nodes].join(',')
options[:zkservers] = options[:zkservers].join(',')
View
@@ -35,6 +35,17 @@ module RedisFailover
'1'])
opts[:max_failures].should == 1
end
+
+ it 'properly parses the config file' do
+ opts = CLI.parse(['-C', "#{File.dirname(__FILE__)}/support/config/single_environment.yml"])
+ opts[:zkservers].should == 'zk01:2181,zk02:2181,zk03:2181'
+
+ opts = CLI.parse(['-C', "#{File.dirname(__FILE__)}/support/config/multiple_environments.yml", '-E', 'development'])
+ opts[:zkservers].should == 'localhost:2181'
+
+ opts = CLI.parse(['-C', "#{File.dirname(__FILE__)}/support/config/multiple_environments.yml", '-E', 'staging'])
+ opts[:zkservers].should == 'zk01:2181,zk02:2181,zk03:2181'
+ end
end
end
end
@@ -0,0 +1,15 @@
+:development:
+ :nodes:
+ - localhost:6379
+ - localhost:6389
+ :zkservers:
+ - localhost:2181
+
+:staging:
+ :nodes:
+ - redis01:6379
+ - redis02:6379
+ :zkservers:
+ - zk01:2181
+ - zk02:2181
+ - zk03:2181
@@ -0,0 +1,7 @@
+:nodes:
+ - redis01:6379
+ - redis02:6379
+:zkservers:
+ - zk01:2181
+ - zk02:2181
+ - zk03:2181

0 comments on commit 517bac7

Please sign in to comment.