Skip to content
Browse files

All commands are now launched from one executable.

  • Loading branch information...
1 parent 0fd8906 commit 29e4572051d9cac5c56476eb8359f1c22855fd8f @alehmann alehmann committed
View
8 bin/rrscan.rb
@@ -1,8 +0,0 @@
-#!/usr/bin/ruby
-
-$LOAD_PATH.unshift File.dirname(__FILE__) + '/../lib'
-
-require 'rubyrep'
-
-exit RR::ScanRunner.run(ARGV)
-
View
8 bin/rrsync.rb
@@ -1,8 +0,0 @@
-#!/usr/bin/ruby
-
-$LOAD_PATH.unshift File.dirname(__FILE__) + '/../lib'
-
-require 'rubyrep'
-
-exit RR::SyncRunner.run(ARGV)
-
View
2 bin/rrproxy.rb → bin/rubyrep.rb
@@ -4,5 +4,5 @@
require 'rubyrep'
-exit RR::ProxyRunner.run(ARGV)
+exit RR::CommandRunner.run(ARGV)
View
2 lib/rubyrep.rb
@@ -7,6 +7,7 @@
gem 'activerecord', '>= 2.0.1'
require 'active_record'
+require 'version'
require 'configuration'
require 'initializer'
require 'session'
@@ -20,6 +21,7 @@
require 'direct_table_scan'
require 'proxied_table_scan'
require 'database_proxy'
+require 'command_runner'
require 'proxy_runner'
require 'proxy_connection'
require 'table_spec_resolver'
View
2 lib/rubyrep/base_runner.rb
@@ -53,7 +53,7 @@ def process_options(args)
parser = OptionParser.new do |opts|
opts.banner = <<EOS
-Usage: #{$0} [options] [table_spec] [table_spec] ...
+Usage: #{$0} #{self.class.name.sub(/^.*::(.*)Runner$/, '\\1').downcase} [options] [table_spec] [table_spec] ...
#{summary_description}
View
89 lib/rubyrep/command_runner.rb
@@ -0,0 +1,89 @@
+$LOAD_PATH.unshift File.dirname(__FILE__) + '/../lib'
+
+require 'optparse'
+
+module RR
+
+ # This class implements the functionality to dispatch rubyrep commands.
+ class CommandRunner
+
+ # Returns a hash of all commands registered with #register.
+ def self.commands
+ @commands ||= {}
+ end
+
+ # Registers one or multiple commands.
+ # +commands+ is a hash with
+ # * key: name of the command
+ # * value: a command hash defining the command
+ #
+ # A command hash consists of
+ # * :+description+: short description of the command
+ # * :+command+: an object / class implementing the hash.
+ # Must have a method
+ #
+ # # runs a command
+ # # * +args+: array of command line parameters
+ # # note: will not contain the command name itself.
+ # def run(args)
+ def self.register(commands)
+ self.commands.merge!(commands)
+ end
+
+ # Prints the version to stderr
+ def self.show_version
+ $stdout.puts "rubyrep version #{RR::VERSION::STRING}"
+ end
+
+ # Prints a summary help text to stderr
+ def self.show_help
+ $stderr.puts <<EOS
+Usage: #{$0} [--help] [--version] command [parameters, ...]
+
+Runs the specified rubyrep command.
+
+Available commands:
+EOS
+ commands.sort.each do |command_name, command_hash|
+ $stderr.puts "#{command_name.center(15)} #{command_hash[:description]}"
+ end
+ end
+
+ # dispatch commands as per given command line parameters.
+ # * +args+: array of command line parameters
+ def self.run(args)
+ if args.empty?
+ show_help
+ 1
+ elsif args[0] == '--help' or (args[0] == 'help' and args.size == 1)
+ show_help
+ 0
+ elsif args[0] == '--version'
+ show_version
+ 0
+ elsif commands.include? args[0]
+ commands[args[0]][:command].run(args.slice(1, 1_000_000))
+ else
+ show_help
+ 1
+ end
+ end
+
+ end
+
+ # Command runner to show help for other commands
+ class HelpRunner
+ CommandRunner.register 'help' => {
+ :command => self,
+ :description => "Shows detailed help for the specified command"
+ }
+
+ # Runs the help command
+ # * +args+: array of command line parameters
+ def self.run(args)
+ CommandRunner.run([args[0], '--help'])
+ end
+ end
+end
+
+
View
7 lib/rubyrep/proxy_runner.rb
@@ -6,6 +6,11 @@
module RR
# This class implements the functionality of the rrproxy.rb command.
class ProxyRunner
+
+ CommandRunner.register 'proxy' => {
+ :command => self,
+ :description => 'Proxies connections from rubyrep commands to the database'
+ }
# Default options to start a DatabaseProxy server
DEFAULT_OPTIONS = {
@@ -22,7 +27,7 @@ def get_options(args)
status = 0
parser = OptionParser.new do |opts|
- opts.banner = "Usage: #{$0} [options]"
+ opts.banner = "Usage: #{$0} proxy [options]"
opts.separator ""
opts.separator "Specific options:"
View
6 lib/rubyrep/scan_runner.rb
@@ -3,6 +3,12 @@
module RR
# This class implements the functionality of the rrscan.rb command.
class ScanRunner < BaseRunner
+
+ CommandRunner.register 'scan' => {
+ :command => self,
+ :description => 'Scans for differing records between databases'
+ }
+
# Returns summary description string for the scan command.
def summary_description
"Scans for differences of the specified tables between both databases."
View
5 lib/rubyrep/sync_runner.rb
@@ -4,6 +4,11 @@ module RR
# This class implements the functionality of the rrsync.rb command.
class SyncRunner < BaseRunner
+ CommandRunner.register 'sync' => {
+ :command => self,
+ :description => 'Syncs records between databases'
+ }
+
# Returns summary description string for the scan command.
def summary_description
"Syncs the differences of the specified tables between both databases."
View
89 spec/command_runner_spec.rb
@@ -0,0 +1,89 @@
+require File.dirname(__FILE__) + '/spec_helper.rb'
+
+include RR
+
+describe CommandRunner do
+ before(:each) do
+ @org_commands = CommandRunner.commands
+ CommandRunner.instance_variable_set :@commands, nil
+ end
+
+ after(:each) do
+ CommandRunner.instance_variable_set :@commands, @org_commands
+ end
+
+ it "show_version should print the version string" do
+ $stdout.should_receive(:puts).with(/rubyrep version ([0-9]+\.){2}[0-9]+/)
+ CommandRunner.show_version
+ end
+
+ it "register should register commands, commands should return it" do
+ CommandRunner.register :bla => :bla_command
+ CommandRunner.register :blub => :blub_command
+ CommandRunner.commands.should == {
+ :bla => :bla_command,
+ :blub => :blub_command
+ }
+ end
+
+ it "show_help should print a short help" do
+ org_stderr = $stderr
+ $stderr = StringIO.new
+ begin
+ CommandRunner.register 'c1' => {:description => 'desc 1'}, 'c2' => {:description => 'desc 2'}
+ CommandRunner.show_help
+ $stderr.string.should =~ /Usage/
+ $stderr.string.should =~ /c1.*desc 1\n/
+ $stderr.string.should =~ /c2.*desc 2\n/
+ ensure
+ $stderr = org_stderr
+ end
+ end
+
+ it "run should print help if no command line parameters are given" do
+ CommandRunner.should_receive(:show_help)
+ CommandRunner.run([]).should == 1
+ end
+
+ it "run should print help if --help or help without further params is given" do
+ CommandRunner.should_receive(:show_help).twice
+ CommandRunner.run(['--help']).should == 0
+ CommandRunner.run(['help']).should == 0
+ end
+
+ it "run should print version if --version is given" do
+ CommandRunner.should_receive(:show_version)
+ CommandRunner.run(['--version'])
+ end
+
+ it "run should call the specified command with the specified params" do
+ c = mock('dummy_command')
+ c.should_receive(:run).with(['param1', 'param2'])
+ CommandRunner.register 'dummy_command' => {:command => c}
+ CommandRunner.run(['dummy_command', 'param1', 'param2'])
+ end
+
+ it "run should print help if unknown command is given" do
+ CommandRunner.should_receive(:show_help)
+ CommandRunner.run('non-existing-command').should == 1
+ end
+
+ it "rubyrep.rb should call CommandRunner#run" do
+ CommandRunner.should_receive(:run).with(ARGV).and_return(0)
+ Kernel.any_instance_should_receive(:exit) {
+ load File.dirname(__FILE__) + '/../bin/rubyrep.rb'
+ }
+ end
+end
+
+describe HelpRunner do
+ it "should register itself" do
+ CommandRunner.commands['help'][:command].should == HelpRunner
+ CommandRunner.commands['help'][:description].should be_an_instance_of(String)
+ end
+
+ it "run should call help for the specified command" do
+ CommandRunner.should_receive(:run).with(['dummy_command', '--help'])
+ HelpRunner.run(['dummy_command'])
+ end
+end
View
8 spec/proxy_runner_spec.rb
@@ -63,10 +63,8 @@
ProxyRunner.run(["--port=1234"])
end
- it "rrproxy.rb should call ProxyRunner#run" do
- ProxyRunner.should_receive(:run).with(ARGV).and_return(0)
- Kernel.any_instance_should_receive(:exit) {
- load File.dirname(__FILE__) + '/../bin/rrproxy.rb'
- }
+ it "should register itself with CommandRunner" do
+ CommandRunner.commands['proxy'][:command].should == ProxyRunner
+ CommandRunner.commands['proxy'][:description].should be_an_instance_of(String)
end
end
View
10 spec/scan_runner_spec.rb
@@ -6,13 +6,11 @@
before(:each) do
end
- it "rrscan.rb should call ScanRunner#run" do
- ScanRunner.should_receive(:run).with(ARGV).and_return(0)
- Kernel.any_instance_should_receive(:exit) {
- load File.dirname(__FILE__) + '/../bin/rrscan.rb'
- }
+ it "should register itself with CommandRunner" do
+ CommandRunner.commands['scan'][:command].should == ScanRunner
+ CommandRunner.commands['scan'][:description].should be_an_instance_of(String)
end
-
+
it "execute should scan the specified tables" do
org_stdout = $stdout
$stdout = StringIO.new
View
10 spec/sync_runner_spec.rb
@@ -6,13 +6,11 @@
before(:each) do
end
- it "rrsync.rb should call ScanRunner#run" do
- SyncRunner.should_receive(:run).with(ARGV).and_return(0)
- Kernel.any_instance_should_receive(:exit) {
- load File.dirname(__FILE__) + '/../bin/rrsync.rb'
- }
+ it "should register itself with CommandRunner" do
+ CommandRunner.commands['sync'][:command].should == SyncRunner
+ CommandRunner.commands['sync'][:description].should be_an_instance_of(String)
end
-
+
it "prepare_table_pairs should sort the tables" do
session = Session.new standard_config
session.should_receive(:sort_table_pairs).

0 comments on commit 29e4572

Please sign in to comment.
Something went wrong with that request. Please try again.