Class: CouchReplicate::CommandLine

Inherits:
Object
  • Object
show all
Defined in:
lib/couch_replicate/command_line.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ CommandLine

Returns a new instance of CommandLine.



12
13
14
# File 'lib/couch_replicate/command_line.rb', line 12

def initialize(args)
  parse_options(args)
end

Instance Attribute Details

#databaseObject (readonly)

Returns the value of attribute database.



10
11
12
# File 'lib/couch_replicate/command_line.rb', line 10

def database
  @database
end

#hostsObject (readonly)

Returns the value of attribute hosts.



10
11
12
# File 'lib/couch_replicate/command_line.rb', line 10

def hosts
  @hosts
end

#optionsObject (readonly)

Returns the value of attribute options.



10
11
12
# File 'lib/couch_replicate/command_line.rb', line 10

def options
  @options
end

Class Method Details

.run(*args) ⇒ Object



6
7
8
# File 'lib/couch_replicate/command_line.rb', line 6

def self.run(*args)
  CommandLine.new(*args).run
end

Instance Method Details

#parse_options(args) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/couch_replicate/command_line.rb', line 31

def parse_options(args)
 @options = { }

 options_parser = OptionParser.new do |opts|
   opts.banner = "Usage: couch-replicate db [OPTIONS] couchdb_host_01, couchdb_host_02[, couchdb_host_03...]"

   opts.separator ""

   opts.on("-l", "--link", "link the hosts in a circular list (this is the default if no other options are specified)") do
     @options[:link] = true
   end

   opts.on("-r", "--reverse", "link the hosts in a cicular list, reversing the order supplied") do
     @options[:reverse] = true
   end

   opts.on("-n", "-number [LINK_EVERY=3]", "link every n hosts") do |n|
     @options[:n] = n.to_i > 0 ? n.to_i : 3
   end

   opts.separator ""

   opts.on_tail("-v", "--version", "Show version") do
     puts File.basename($0) + " "  + CouchReplicate.version
     exit
   end

   # No argument, shows at tail.  This will print an options summary.
   # Try it and see!
   opts.on_tail("-h", "--help", "Show this message") do
     puts opts
     exit
   end
 end

 begin
   options_parser.parse!(args)
   @database = args.shift
   @hosts = args

   if @options[:n] && (@options[:n] == @hosts.length + 1)
     raise OptionParser::InvalidOption.new("The number of hosts cannot equal the replication shift.")
   end
 rescue OptionParser::InvalidOption => e
   raise e
 end
end

#runObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/couch_replicate/command_line.rb', line 16

def run
  if @options[:link] || @options.empty?
    $stderr.puts "Linking replication hosts..."
    CouchReplicate.link(@database, @hosts)
  end
  if @options[:reverse]
    $stderr.puts "Reverse linking replication hosts..."
    CouchReplicate.reverse_link(@database, @hosts)
  end
  if @options[:n]
    $stderr.puts "Linking every #{@options[:n]}th replication hosts..."
    CouchReplicate.nth(@options[:n], @database, @hosts)
  end
end