Class: Vayacondios::Client::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/vayacondios/client/cli.rb

Overview

Implements a program ‘vcd` which makes it easy to interact with Vayacondios from the command-line or from scripts.

It makes it easy to use files, pipes, or the command-line itself to pass data to Vayacondios.

The ‘document’ and the ‘query’ are String inputs passed on the command line. These String inputs are combined with String inputs read from files named via command-line options or from STDIN to create an array of “inputs” for each command to operate on.

Constant Summary collapse

Error =
Class.new(StandardError)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#clientVayacondios::HttpClient

the client used to communicate with the server

Returns:

  • (Vayacondios::HttpClient)

    the current value of client



21
22
23
# File 'lib/vayacondios/client/cli.rb', line 21

def client
  @client
end

#documentString

a document read from a file or passed from the command-line

Returns:

  • (String)

    the current value of document



21
22
23
# File 'lib/vayacondios/client/cli.rb', line 21

def document
  @document
end

#idString

the ID of the current request

Returns:

  • (String)

    the current value of id



21
22
23
# File 'lib/vayacondios/client/cli.rb', line 21

def id
  @id
end

#queryString

a query read from a file or passed from the command-line

Returns:

  • (String)

    the current value of query



21
22
23
# File 'lib/vayacondios/client/cli.rb', line 21

def query
  @query
end

#topicString

the topic of the current request

Returns:

  • (String)

    the current value of topic



21
22
23
# File 'lib/vayacondios/client/cli.rb', line 21

def topic
  @topic
end

Class Method Details

.runObject

Run a new instance of the ‘vcd` program.

Instantiates a new instance of CLI and takes it through its lifecycle.

Raises:

  • (SystemExit)

    when execution stops either because requests are complete or a fatal error has occured



129
130
131
132
133
134
135
136
137
138
# File 'lib/vayacondios/client/cli.rb', line 129

def self.run
  cli = new
  begin
    cli.boot
    cli.run
  rescue Error => e
    $stderr.puts e.message
    exit(1)
  end
end

Instance Method Details

#announceObject

Announce one or many events.

Will read the topic as the first argument, the document as the second, and the ID as the third.

Will iterate over each input and announce it as an event. It will attempt to find a topic and ID within each event and will fall back to using its own as a default.

Raises:

  • (Error)

    if no topic was given



177
178
179
180
181
182
183
184
185
# File 'lib/vayacondios/client/cli.rb', line 177

def announce
  self.topic    = cmdline.rest.shift or raise Error.new('Must provide a topic when announcing an event')
  self.document = cmdline.rest.shift
  self.id       = cmdline.rest.shift
  raise Error.new("Must provide an event to announce via the second command-line argument, the --file argument, or STDIN.") unless input?
  inputs do |event|
    handle_response client.announce(topic_for(event), event, id_for(event))
  end
end

#available_commandsObject

Commands ==



163
164
165
# File 'lib/vayacondios/client/cli.rb', line 163

def available_commands
  %w[ announce events clear_events get get_many set set! unset unset_many ]
end

#bootObject



140
141
142
143
144
# File 'lib/vayacondios/client/cli.rb', line 140

def boot
  cmdline.resolve!
  self.client = HttpClient.new(cmdline)
  log.debug "Created client with settings: #{cmdline}"
end

#clear_eventsObject



207
208
209
210
211
212
213
# File 'lib/vayacondios/client/cli.rb', line 207

def clear_events
  self.topic    = cmdline.rest.shift or raise Error.new("Must provide a topic when deleting events")
  self.query    = cmdline.rest.shift || '{}'
  inputs do |query|
    handle_response client.clear_events(topic_for(query), query)
  end
end

#cmdlineConfigliere::Param

Returns the cmdline used for the ‘vcd` program.

Returns:

  • (Configliere::Param)


103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/vayacondios/client/cli.rb', line 103

def cmdline
  return @cmdline if @cmdline
  c = Configliere::Param.new.use :commandline
  usg = self.usage
  c.define_singleton_method(:usage){ usg }
  c.description = self.description
  
  c.define :host,         flag: 'h',                                    description: "Host for Vayacondios server"
  c.define :port,         flag: 'p', type: Integer,                     description: "Port for Vayacondios server"
  c.define :organization, flag: 'o',                 default: 'vcd',    description: "Organization to write data for"
  c.define :topic_field,                             default: '_topic', description: "Field used to dynamically determine the topic of a record"
  c.define :id_field,                                default: '_id',    description: "Field used to dynamically determine the ID of a record"
  c.define :file,         flag: 'f',                                    description: "Read input from file" 
  c.define :log_file,                                                   description: "Path to log file (defaut: STDOUT)"
  c.define :log_level,                               default: 'INFO',   description: "Log level"
  c.define :pretty,       flag: 'p', type: :boolean, default: false,    description: "Pretty-print output"
  c.define :dry_run,                 type: :boolean, default: false,    description: "Don't perform any actual requests"
  @cmdline = c
end

#descriptionObject



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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/vayacondios/client/cli.rb', line 39

def description
  <<-DESCRIPTION.gsub(/^ {8}/, '').chomp
    A command-line Vayacondios client which reads/writes events and
    configuration to/from a Vayacondios server over HTTP.
    
    Announce an event like a successful build of your `foo` project on the
    `foo.build` topic.
    
      $ vcd announce foo.build '{"version": "1.2.3", "status": "success", "time": 27.56}'
    
    You can also announce multiple events at once, perhaps of all your
    projects on the `builds` topic.
    
      $ cat build_events.json
      {"project": "foo", "version": "1.2.3", "status": "success", "time": 27.56}
      {"project": "bar", "version": "0.1.8", "status": "success", "time": 5.22}
      ...
      $ vcd announce builds --file=build_events.json
    
    This works in pipelines too
    
      $ cat build_events.json | vcd announce builds
    
    Events are assigned their own unique ID unless a third ID argument is
    provided.
    
    Events can set their own topics if the contain a `_topic` key and
    their own IDs if they contain an `_id` key.  These fields can
    themselves be customized, see the --topic_field and --id_field
    options.
    
    Stash a configuration value (try the `set!` if you want to
    completely overwrite the old value with the new instead of merging,
    which is the default behavior):
    
      $ vcd set foo admin '{"name": "Bob Jones", "email": "[email protected]"}'
    
    Retrieve it again
    
      $ vcd get foo admin
      {"name": "Bob Jones", "email": "[email protected]"}
      
    Or delete it
    
      $ vcd delete foo admin
    
    Getting, setting, or deleting multiple values works the same way as
    for announcing events: either from a file or over STDIN.  Individual
    records can also set their topic and ID just like events.
    
    You can change the Vayacondios host, port, or organization at runtime
    
      $ vcd announce --host=vcd.example.com --organization=myorg foo.build '{"version": "1.2.3", "status": "success", "time": 27.56}'
    
    As well as through YAML configuration files at
    
      /etc/vayacondios/vayacondios.yml
      $PWD/config/vayacondios.yml        
  DESCRIPTION
end

#eventsObject

Search for events.

Will read the topic as the first argument and the query as the second.

For each input, will send it as a query. Will attempt to find a topic in the query itself and will fall back to using its own as a default.

Results are printed out one per-line.

Raises:

  • (Error)

    if no topic was given



199
200
201
202
203
204
205
# File 'lib/vayacondios/client/cli.rb', line 199

def events
  self.topic    = cmdline.rest.shift or raise Error.new("Must provide a topic when searching for events")
  self.query    = cmdline.rest.shift || '{}'
  inputs do |query|
    handle_response client.events(topic_for(query), query)
  end
end

#getObject

Get a single stashed value.

Will read the topic as the first argument as the ID as the second.

For each input, will use it to get a stash. Will attempt to find a topic and ID in the input and will fall back to using its own as a default.

Raises:

  • (Error)

    when no topic was given



224
225
226
227
228
229
230
231
232
233
234
# File 'lib/vayacondios/client/cli.rb', line 224

def get
  self.topic = cmdline.rest.shift or raise Error.new('Must provide a topic when getting a stash')
  self.id    = cmdline.rest.shift
  if input?
    inputs do |req|
      handle_response client.get(topic_for(req), id_for(req))
    end
  else
    handle_response client.get(topic, id)
  end
end

#get_manyObject

Search for multiple stashed values.

Will read the query as the first argument.

For each input will use it as a query.



241
242
243
244
245
246
# File 'lib/vayacondios/client/cli.rb', line 241

def get_many
  self.query = (cmdline.rest.shift || '{}')
  inputs do |query|
    handle_response client.get_many(query)
  end
end

#input?true, false

Were there any inputs?

Input is either a document passed on the command-line, a file requested to be read from the command-line, or is imminent because data is queuing up on STDIN.

Returns:

  • (true, false)


333
334
335
# File 'lib/vayacondios/client/cli.rb', line 333

def input?
  document || cmdline.file || input_on_stdin?
end

#inputs {|input| ... } ⇒ Object

For each input, parse each line as a JSON record and yield it to the given block.

First the document and/or query will be processed, then any file named on the command-line, then data over STDIN.

Yields:

  • (input)

    yields each parsed line from each input

Yield Parameters:

  • input (Hash, Array, String, Numeric, nil)

    the input



345
346
347
348
349
350
351
352
353
354
# File 'lib/vayacondios/client/cli.rb', line 345

def inputs(&block)
  raw_inputs do |line|
    begin
      yield MultiJson.load(line)
    rescue => e
      log.error("#{e.class} -- #{e.message}")
      $stderr.puts(e.backtrace)
    end
  end
end

#logLogger

The log that will be used by the ‘vcd` program as it runs.

Returns:

  • (Logger)


447
448
449
450
451
452
453
# File 'lib/vayacondios/client/cli.rb', line 447

def log
  return @log if @log
  require 'logger'
  @log = Logger.new(log_output)
  @log.level = Logger.const_get(cmdline.log_level.upcase)
  @log
end

#runObject

Run the desired command.

Raises:

  • (Error)

    if the desired command is unknown



149
150
151
152
153
154
155
156
157
# File 'lib/vayacondios/client/cli.rb', line 149

def run
  command = cmdline.rest.shift
  return cmdline.dump_help if command.nil?
  if available_commands.include? command
    send command
  else
    raise Error.new "Unknown command: <#{command}>.  Must be one of #{available_commands.inspect}"
  end
end

#setObject

Set a value by merging it into a (potentially) existing value.

Will read the topic as the first argument, ID as the second, and document as the third.

For each input, will merge that input. Will attempt to read a topic and ID for each input and will fall back to its own as a default.

Raises:

  • (Error)

    if no topic was given



258
259
260
261
262
263
264
265
266
# File 'lib/vayacondios/client/cli.rb', line 258

def set
  self.topic    = cmdline.rest.shift or raise Error.new('Must provide a topic when setting a stash')
  self.document = cmdline.rest.shift
  self.id       = cmdline.rest.shift
  raise Error.new("Must provide a document to stash via the third command-line argument, the --file argument, or STDIN.") unless input?
  inputs do |doc|
    handle_response client.set(topic_for(doc), id_for(doc), doc)
  end
end

#set!Object

Set a value by overwriting a (potentially) existing value.

Will read the topic as the first argument, ID as the second, and document as the third.

For each input, will write that input. Will attempt to read a topic and ID for each input and will fall back to its own as a default.

Raises:

  • (Error)

    if no topic was given



278
279
280
281
282
283
284
285
286
# File 'lib/vayacondios/client/cli.rb', line 278

def set!
  self.topic    = cmdline.rest.shift or raise Error.new('Must provide a topic when setting a stash')
  self.document = cmdline.rest.shift
  self.id       = cmdline.rest.shift
  raise Error.new("Must provide a document to stash via the third command-line argument, the --file argument, or STDIN.") unless input?
  inputs do |doc|
    handle_response client.set!(topic_for(doc), id_for(doc), doc)
  end
end

#unsetObject

Delete a stashed value.

Will read the topic as the first argument and ID as the second.

For each input, will delete that input. Will attempt to read a topic and ID for each input and will fall back to its own as a default.

Raises:

  • (Error)

    if no topic was given



297
298
299
300
301
302
303
304
305
306
307
# File 'lib/vayacondios/client/cli.rb', line 297

def unset
  self.topic = cmdline.rest.shift or raise Error.new('Must provide a topic when deleting a stash')
  self.id    = cmdline.rest.shift
  if input?
    inputs do |req|
      handle_response client.unset(topic_for(req), id_for(req))
    end
  else
    handle_response client.unset(topic, id)
  end
end

#unset_manyObject

Delete many stashes that match some criteria.

Each input should be a query Hash.

Raises:

  • (Error)

    if no input was given



314
315
316
317
318
319
320
# File 'lib/vayacondios/client/cli.rb', line 314

def unset_many
  self.document = cmdline.rest.shift
  raise Error.new("Must provide a query via the second command-line argument, the --file argument, or STDIN.") unless input?
  inputs do |query|
    handle_response client.unset_many(query)
  end
end

#usageObject



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/vayacondios/client/cli.rb', line 27

def usage
  <<-USAGE.gsub(/^ {8}/, '').chomp
    usage:
      vcd [ --param=val|--param|-p val|-p ] announce TOPIC [DOCUMENT] [ID]
      vcd [ --param=val|--param|-p val|-p ] get|set|set!|delete TOPIC ID [DOCUMENT]
      vcd [ --param=val|--param|-p val|-p ] events TOPIC QUERY
      vcd [ --param=val|--param|-p val|-p ] stashes QUERY
      vcd [ --param=val|--param|-p val|-p ] set_many|set_many! QUERY_AND_UPDATE
      vcd [ --param=val|--param|-p val|-p ] delete_many QUERY
  USAGE
end