Class: CouchDocs::CommandLine

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

Constant Summary collapse

COMMANDS =
%w{push dump}
DEPRECATED_COMMANDS =
%w{load}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ CommandLine

Returns a new instance of CommandLine.



16
17
18
# File 'lib/couch_docs/command_line.rb', line 16

def initialize(args)
  parse_options(args)
end

Instance Attribute Details

#commandObject (readonly)

Returns the value of attribute command.



14
15
16
# File 'lib/couch_docs/command_line.rb', line 14

def command
  @command
end

#optionsObject (readonly)

Returns the value of attribute options.



14
15
16
# File 'lib/couch_docs/command_line.rb', line 14

def options
  @options
end

Class Method Details

.run(*args) ⇒ Object



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

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

Instance Method Details

#design_doc_update?(args) ⇒ Boolean

Returns:

  • (Boolean)


154
155
156
# File 'lib/couch_docs/command_line.rb', line 154

def design_doc_update?(args)
  args.any? { |f| f.path =~ /_design/ }
end

#directory_watcher_update(args) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/couch_docs/command_line.rb', line 134

def directory_watcher_update(args)
  if initial_add? args
    CouchDocs.put_dir(@options[:couchdb_url],
                      @options[:target_dir])
  else
    if design_doc_update? args
      CouchDocs.put_design_dir(@options[:couchdb_url],
                               "#{@options[:target_dir]}/_design")
    end
    documents(args).each do |update|
      CouchDocs.put_file(@options[:couchdb_url],
                         update.path)
    end
  end
end

#documents(args) ⇒ Object



158
159
160
# File 'lib/couch_docs/command_line.rb', line 158

def documents(args)
  args.reject { |f| f.path =~ /_design/ }
end

#initial_add?(args) ⇒ Boolean

Returns:

  • (Boolean)


150
151
152
# File 'lib/couch_docs/command_line.rb', line 150

def initial_add?(args)
  args.all? { |f| f.type == :added }
end

#parse_options(args) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/couch_docs/command_line.rb', line 58

def parse_options(args)
  @options = { :target_dir => "." }

  options_parser = OptionParser.new do |opts|
    opts.banner = "Usage: couch-docs push|dump [OPTIONS] couchdb_url [target_dir]"

    opts.separator ""
    opts.separator "If a target_dir is not specified, the current working directory will be used."

    opts.separator ""
    opts.separator "Push options:"

    opts.on("-R", "--destructive",
            "Drop the couchdb_uri (if it exists) and create a new database") do
      @options[:destructive] = true
    end

    # TODO: bulk_docs in 1.2
    # opts.on("-b", "--bulk [BATCH_SIZE=1000]", Integer,
    #         "Use bulk insert when pushing new documents") do |batch_size|
    #   @options[:bulk] = true
    #   @options[:batch_size] = batch_size || 1000
    # end

    opts.on("-w", "--watch", "Watch the directory for changes, uploading when detected") do
      @options[:watch] = true
    end

    opts.separator ""
    opts.separator "Dump options:"

    opts.on("-d", "--design", "Only dump design documents") do
      @options[:dump] = :design
    end
    opts.on("-D", "--data", "Only dump data documents") do
      @options[:dump] = :doc
    end

    opts.separator ""
    opts.separator "Common options:"

    opts.on_tail("-v", "--version", "Show version") do
      puts File.basename($0) + " "  + CouchDocs::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)
    additional_help = "#{options_parser.banner}\n\nTry --help for more options."
    unless (COMMANDS+DEPRECATED_COMMANDS).include? args.first
      puts "invalid command: \"#{args.first}\".  Must be one of #{COMMANDS.join(', ')}.\n\n"
      puts additional_help
      exit
    end
    @command = args.shift
    unless args.first
      puts "Missing required couchdb_uri argument.\n\n"
      puts additional_help
      exit
    end
    @options[:couchdb_url] = args.shift
    @options[:target_dir]  = args.shift if (args.size >= 1)

  rescue OptionParser::InvalidOption => e
    raise e
  end
end

#runObject



20
21
22
23
24
25
26
27
28
29
30
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
# File 'lib/couch_docs/command_line.rb', line 20

def run
  case command
  when "dump"
    CouchDocs.dump(@options[:couchdb_url],
                   @options[:target_dir],
                   @options[:dump])
  when "push"
    if @options[:destructive]
      CouchDocs.destructive_database_create(options[:couchdb_url])
    end

    dw = DirectoryWatcher.new @options[:target_dir]
    dw.glob = '**/*'
    dw.interval = 2.0

    dw.add_observer do |*args|
      puts "Updating documents on CouchDB Server..."
      directory_watcher_update(args)
    end

    if @options[:watch]
      dw.start

      begin
        sleep 30 while active?
      rescue Interrupt
        dw.stop
        puts
      end
    else
      dw.run_once
    end
  when "load" # DEPRECATED
    CouchDocs.put_dir(@options[:target_dir],
                      @options[:couchdb_url])
  end
end