Class: Vayacondios::Client::CLI
- Inherits:
-
Object
- Object
- Vayacondios::Client::CLI
- 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
-
#client ⇒ Vayacondios::HttpClient
the client used to communicate with the server.
-
#document ⇒ String
a document read from a file or passed from the command-line.
-
#id ⇒ String
the ID of the current request.
-
#query ⇒ String
a query read from a file or passed from the command-line.
-
#topic ⇒ String
the topic of the current request.
Class Method Summary collapse
-
.run ⇒ Object
Run a new instance of the ‘vcd` program.
Instance Method Summary collapse
-
#announce ⇒ Object
Announce one or many events.
-
#available_commands ⇒ Object
Commands ==.
- #boot ⇒ Object
- #clear_events ⇒ Object
-
#cmdline ⇒ Configliere::Param
Returns the cmdline used for the ‘vcd` program.
- #description ⇒ Object
-
#events ⇒ Object
Search for events.
-
#get ⇒ Object
Get a single stashed value.
-
#get_many ⇒ Object
Search for multiple stashed values.
-
#input? ⇒ true, false
Were there any inputs?.
-
#inputs {|input| ... } ⇒ Object
For each input, parse each line as a JSON record and yield it to the given block.
-
#log ⇒ Logger
The log that will be used by the ‘vcd` program as it runs.
-
#run ⇒ Object
Run the desired command.
-
#set ⇒ Object
Set a value by merging it into a (potentially) existing value.
-
#set! ⇒ Object
Set a value by overwriting a (potentially) existing value.
-
#unset ⇒ Object
Delete a stashed value.
-
#unset_many ⇒ Object
Delete many stashes that match some criteria.
- #usage ⇒ Object
Instance Attribute Details
#client ⇒ Vayacondios::HttpClient
the client used to communicate with the server
21 22 23 |
# File 'lib/vayacondios/client/cli.rb', line 21 def client @client end |
#document ⇒ String
a document read from a file or passed from the command-line
21 22 23 |
# File 'lib/vayacondios/client/cli.rb', line 21 def document @document end |
#id ⇒ String
the ID of the current request
21 22 23 |
# File 'lib/vayacondios/client/cli.rb', line 21 def id @id end |
#query ⇒ String
a query read from a file or passed from the command-line
21 22 23 |
# File 'lib/vayacondios/client/cli.rb', line 21 def query @query end |
#topic ⇒ String
the topic of the current request
21 22 23 |
# File 'lib/vayacondios/client/cli.rb', line 21 def topic @topic end |
Class Method Details
.run ⇒ Object
Run a new instance of the ‘vcd` program.
Instantiates a new instance of CLI and takes it through its lifecycle.
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. exit(1) end end |
Instance Method Details
#announce ⇒ Object
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.
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_commands ⇒ Object
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 |
#boot ⇒ Object
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_events ⇒ Object
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 |
#cmdline ⇒ Configliere::Param
Returns the cmdline used for the ‘vcd` program.
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 |
#description ⇒ Object
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 |
#events ⇒ Object
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.
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 |
#get ⇒ Object
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.
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_many ⇒ Object
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.
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.
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.}") $stderr.puts(e.backtrace) end end end |
#log ⇒ Logger
The log that will be used by the ‘vcd` program as it runs.
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 |
#run ⇒ Object
Run the desired command.
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 |
#set ⇒ Object
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.
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.
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 |
#unset ⇒ Object
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.
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_many ⇒ Object
Delete many stashes that match some criteria.
Each input should be a query Hash.
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 |
#usage ⇒ Object
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 |