Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

daikon.yml with 1 redis works

  • Loading branch information...
commit 15bee7eb50a8ce6798d3b96608bc081755cafdf7 1 parent 59883f0
@gabebw gabebw authored
View
6 lib/daikon.rb
@@ -6,6 +6,12 @@
require 'socket'
require 'stringio'
require 'thread'
+begin
+ require 'psych'
+rescue LoadError
+ # Ignore
+end
+require 'yaml'
require 'em-hiredis'
require 'em-http-request'
View
19 lib/daikon/configuration.rb
@@ -37,14 +37,29 @@ def validate_api_key
def parse
FLAGS.each_with_index do |flag, flag_index|
argv_index = @argv.index(flag)
+ option_variable_name = OPTIONS[flag_index]
+ default_value = DEFAULTS[flag_index]
+ current_value = send(option_variable_name)
+
value = if argv_index
@argv[argv_index + 1]
+ elsif yaml_config[option_variable_name]
+ yaml_config[option_variable_name]
else
- DEFAULTS[flag_index]
+ default_value
end
- send "#{OPTIONS[flag_index]}=", value
+ send "#{option_variable_name}=", value
end
end
+
+ def yaml_config
+ @yaml_config ||= begin
+ loaded_yaml = YAML.load_file('daikon.yml')
+ loaded_yaml['daikon']
+ rescue Errno::ENOENT
+ {}
+ end
+ end
end
end
View
48 spec/configuration_spec.rb
@@ -64,3 +64,51 @@
end
end
end
+
+describe Daikon::Configuration, "with daikon.yml with 1 redis" do
+ let(:api_key){ "magic_key" }
+ let(:redis_url){ "redis://0.0.0.0:6380" }
+ let(:server_prefix){ "orange" }
+ let(:yaml) do
+ <<-YML
+---
+daikon:
+ api_key: #{api_key}
+ redis_url: #{redis_url}
+ server_prefix: #{server_prefix}
+ YML
+ end
+
+ before do
+ open('daikon.yml', 'w+'){ |f| f.write(yaml) }
+ end
+
+ after do
+ File.delete('daikon.yml')
+ end
+
+ it "reads options from daikon.yml" do
+ configuration = Daikon::Configuration.new
+ configuration.api_key.should == api_key
+ configuration.redis_url.should == redis_url
+ configuration.server_prefix.should == server_prefix
+ end
+
+ it "prefers api_key from ARGV" do
+ api_key_from_argv = 'a_different_key'
+ configuration = Daikon::Configuration.new(['-k', api_key_from_argv])
+ configuration.api_key.should == api_key_from_argv
+ end
+
+ it "prefers redis_url from ARGV" do
+ redis_url_from_argv = 'redis://0.0.0.0:5000'
+ configuration = Daikon::Configuration.new(['-u', redis_url_from_argv])
+ configuration.redis_url.should == redis_url_from_argv
+ end
+
+ it "prefers server_prefix from ARGV" do
+ server_prefix_from_argv = 'my_prefix'
+ configuration = Daikon::Configuration.new(['-s', server_prefix_from_argv])
+ configuration.server_prefix.should == server_prefix_from_argv
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.