bin/onering in onering-client-0.0.24 vs bin/onering in onering-client-0.0.25
- old
+ new
@@ -8,29 +8,38 @@
require 'json'
require 'rubygems'
require 'subcommander'
require 'onering'
-require 'yaml'
require 'pp'
include Subcommander
def print_format(data, format=nil)
- case format
+ format = :text unless format
+
+ case format.to_sym
when :text
if data.is_a?(Hash)
pp data
elsif data.is_a?(Array)
puts data.join("\n")
else
puts data.to_s
end
- else
+ when :yaml
+ require 'yaml'
puts YAML.dump(data)
+
+ when :json
+ require 'json'
+ puts JSON.dump(data)
+
+ else
+ raise "Unknown output format #{format}"
end
end
subcommander.version = ::Gem.loaded_specs['onering-client'].version.to_s
subcommander.desc = ::Gem.loaded_specs['onering-client'].description
@@ -79,46 +88,46 @@
# GET [FIELD]
devices.subcommand :get, "Get a named field from one or more devices" do |sc|
sc.usage = "onering devices get FIELD"
sc.opt :filter, '-f', '--filter FILTER', "A urlquery filter string"
- sc.opt :as_txt, '-t', '--as-text', "Return the results as text"
+ sc.opt :format, '-t', '--format FORMAT', "Return the results as FORMAT"
sc.opt :id, '-i', '--id ID', "A specific node ID"
sc.exec do
rv = _field(:get, sc[:args].first, nil, sc)
- print_format(rv, (sc[:as_txt] ? :text : nil))
+ print_format(rv, sc[:format])
end
end
# SET [FIELD]
devices.subcommand :set, "Set a named field for one or more devices" do |sc|
sc.usage = "onering devices set FIELD VALUE"
sc.opt :filter, '-f', '--filter FILTER', "A urlquery filter string"
- sc.opt :as_txt, '-t', '--as-text', "Return the results as text"
+ sc.opt :format, '-t', '--format FORMAT', "Return the results as FORMAT"
sc.opt :id, '-i', '--id ID', "A specific node ID"
sc.exec do
rv = _field(:set, sc[:args].first, sc[:args].last, sc)
- print_format(rv, (sc[:as_txt] ? :text : nil))
+ print_format(rv, sc[:format])
end
end
# LIST
devices.subcommand :list, "List field values" do |sc|
sc.usage = "onering devices list [-f FILTER] FIELD"
sc.opt :filter, '-f', '--filter FILTER', "A urlquery filter string"
- sc.opt :as_txt, '-t', '--as-text', "Return the results as text"
+ sc.opt :format, '-t', '--format FORMAT', "Return the results as FORMAT"
sc.exec do
field = sc[:args].first
filter = sc[:filter]
print_format(api.list(field, {
:filter => filter
- }), (sc[:as_txt] ? :text : nil))
+ }), sc[:format])
end
end
# FIND
devices.subcommand :find, "Finds all nodes that match a urlquery filter string" do |sc|
@@ -169,19 +178,19 @@
end
# LIST
users.subcommand :list, "List users" do |sc|
sc.usage = "onering users list FIELD"
- sc.opt :as_txt, '-t', '--as-text', "Return the results as text"
+ sc.opt :format, '-t', '--format FORMAT', "Return the results as FORMAT"
sc.exec do
field = sc[:args].first
filter = sc[:filter]
print_format(api.list(:users, field, {
:filter => filter
- }), (sc[:as_txt] ? :text : nil))
+ }), sc[:format])
end
end
# SAVE
users.subcommand :save, "Creates or updates a new device in Onering, reading a JSON document from standard input" do |sc|
@@ -209,21 +218,43 @@
:host => ENV['ONERING_URL'],
:pemfile => ENV['ONERING_PEM']
})
call.usage = "onering call path/to/endpoint"
- call.opt :as_txt, '-t', '--as-text', "Return the results as text"
+ call.opt :format, '-t', '--format FORMAT', "Return the results as FORMAT"
call.opt :method, '-m', '--method VERB', "The HTTP method to use for the call (default: GET)"
call.opt :opts, '-o', '--option KEY:VALUE', Array, "A comma-separated list of key:value querystrings to pass with the request"
call.exec do
rv = api.request(call[:args].first.to_sym, {
:method => (call[:method] || :get),
:data => (STDIN.tty? ? {} : ::JSON.load(STDIN.read)),
:fields => (Hash[call[:opts].collect{|i| i.split(':',2) }] rescue {})
})
- print_format(rv, (call[:as_txt] ? :text : nil)) unless rv.nil? or rv.to_s.strip.chomp.empty?
+ print_format(rv, call[:format]) unless rv.nil? or rv.to_s.strip.chomp.empty?
+ end
+end
+
+subcommand :fact, "Retrieve a system fact" do |fact|
+ fact.usage = "onering fact NAME [DEFAULT] [NAME2 [DEFAULT2] ..]"
+ fact.opt :format, '-t', '--format FORMAT', "Return the results as FORMAT"
+
+ fact.exec do
+ rv = []
+
+ fact[:args].each_index do |i|
+ if i.even?
+ name = fact[:args][i]
+ else
+ default = fact[:args][i]
+ end
+
+ rv << Onering::Util.fact(name, default)
+ end
+
+ rv = rv.first if rv.length == 1
+ print_format(rv, fact[:format]) unless not rv or rv.empty?
end
end
subcommander.go!
\ No newline at end of file