Skip to content

Commit

Permalink
Added the support for optional environments in the config file
Browse files Browse the repository at this point in the history
  • Loading branch information
oleriesenberg committed Jun 6, 2012
1 parent ba17280 commit 517bac7
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 2 deletions.
12 changes: 10 additions & 2 deletions lib/redis_failover/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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(',')

Expand Down
11 changes: 11 additions & 0 deletions spec/cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
15 changes: 15 additions & 0 deletions spec/support/config/multiple_environments.yml
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions spec/support/config/single_environment.yml
Original file line number Diff line number Diff line change
@@ -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.