Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

deprecate -h and -p in favor of a -u option for redis url

  • Loading branch information...
commit 2a9a952489c5bc8762ab848954a42293721ae7ce 1 parent f9bcc52
@qrush qrush authored
View
2  lib/daikon/client.rb
@@ -20,7 +20,7 @@ def setup(config, logger = nil)
end
def connect
- Redis.new(:host => config.redis_host, :port => config.redis_port)
+ Redis.connect(:url => config.redis_url)
end
def start_monitor
View
41 lib/daikon/configuration.rb
@@ -1,26 +1,49 @@
module Daikon
class Configuration
- FLAGS = %w[-h -p -k -f -s]
- OPTIONS = %w[redis_host redis_port api_key field_id server_prefix]
- DEFAULTS = %w[127.0.0.1 6379 1234567890 1 https://radish.heroku.com]
+ BLANK_KEY = "1234567890"
+ FLAGS = ["-u", "-k", "-s"]
+ OPTIONS = ["redis_url", "api_key", "server_prefix"]
+ DEFAULTS = ["redis://0.0.0.0:6379", BLANK_KEY, "https://radish.heroku.com"]
attr_accessor *OPTIONS
def initialize(argv = [])
+ @argv = argv
+
+ validate_deprecated_options
+ parse
+ validate_api_key
+ end
+
+ private
+
+ def argv_matches?(regexp)
+ @argv.any? { |arg| arg =~ regexp }
+ end
+
+ def validate_deprecated_options
+ if argv_matches?(/\-h|\-p/)
+ abort "Please use '-u redis://127.0.0.1:6379' format instead to specify redis url"
+ end
+ end
+
+ def validate_api_key
+ if api_key == BLANK_KEY && argv_matches?(/start|run/)
+ abort "Must supply an api key to start the daemon.\nExample: daikon start #{FLAGS[2]} #{DEFAULTS[2]}"
+ end
+ end
+
+ def parse
FLAGS.each_with_index do |flag, flag_index|
- argv_index = argv.index(flag)
+ argv_index = @argv.index(flag)
value = if argv_index
- argv[argv_index + 1]
+ @argv[argv_index + 1]
else
DEFAULTS[flag_index]
end
send "#{OPTIONS[flag_index]}=", value
end
-
- if api_key == DEFAULTS[2] && argv.any? { |arg| arg =~ /start|run/ }
- abort "Must supply an api key to start the daemon.\nExample: daikon start #{FLAGS[2]} #{DEFAULTS[2]}"
- end
end
end
end
View
9 spec/client_spec.rb
@@ -6,19 +6,20 @@
let(:redis) { 'redis instance' }
before do
- Redis.stubs(:new => redis)
+ Redis.stubs(:connect => redis)
subject.stubs(:redis=)
end
context "with overrides" do
- let(:config) { Daikon::Configuration.new(%w[-h 8.8.8.8 -p 1234]) }
+ let(:url) { "redis://8.8.8.8:1234" }
+ let(:config) { Daikon::Configuration.new(["-u", url]) }
before do
subject.setup(config, logger)
end
it "sets redis to listen on the given port" do
- Redis.should have_received(:new).with(:host => "8.8.8.8", :port => "1234").twice
+ Redis.should have_received(:connect).with(:url => url).twice
subject.should have_received(:redis=).with(redis)
end
end
@@ -31,7 +32,7 @@
end
it "sets redis to listen on the given port" do
- Redis.should have_received(:new).with(:host => "127.0.0.1", :port => "6379").twice
+ Redis.should have_received(:connect).with(:url => "redis://0.0.0.0:6379").twice
subject.should have_received(:redis=).with(redis)
end
end
View
30 spec/configuration_spec.rb
@@ -2,13 +2,11 @@
describe Daikon::Configuration do
subject { Daikon::Configuration.new(flags) }
- let(:flags) { %w[-h 4.2.2.2 -p 9001 -k deadbeef -s localhost:9337 -f 1337] }
+ let(:flags) { %w[-u redis://4.2.2.2:9001 -k deadbeef -s localhost:9337] }
it "parses the given flags" do
- subject.redis_host.should == "4.2.2.2"
- subject.redis_port.should == "9001"
+ subject.redis_url.should == "redis://4.2.2.2:9001"
subject.api_key.should == "deadbeef"
- subject.field_id.should == "1337"
subject.server_prefix == "localhost:9337"
end
end
@@ -17,10 +15,8 @@
subject { Daikon::Configuration.new(%w[-k 1234567890]) }
it "uses the default keys" do
- subject.redis_host.should == "127.0.0.1"
- subject.redis_port.should == "6379"
+ subject.redis_url.should == "redis://0.0.0.0:6379"
subject.api_key.should == "1234567890"
- subject.field_id.should == "1"
subject.server_prefix == "radish.heroku.com"
end
end
@@ -44,13 +40,19 @@
end
describe Daikon::Configuration do
- subject { Daikon::Configuration.new(flags) }
- let(:flags) { %w[-p 9001 -k deadbeef] }
+ it "fails if -h option given" do
+ capture do
+ lambda {
+ Daikon::Configuration.new(%w[-h 8.8.8.8])
+ }.should raise_error(SystemExit)
+ end
+ end
- it "can handle defaults and given options" do
- subject.redis_port.should == "9001"
- subject.api_key.should == "deadbeef"
- subject.field_id.should == "1"
- subject.server_prefix == "radish.heroku.com"
+ it "fails if -p option given" do
+ capture do
+ lambda {
+ Daikon::Configuration.new(%w[-p 6380])
+ }.should raise_error(SystemExit)
+ end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.