module Onering module CLI require 'yaml' require 'multi_json' @_args = {} class Plugin def self.default_format(output_value=nil, args=nil) if output_value.is_a?(Hash) or (output_value.is_a?(Array) and output_value.compact.first.is_a?(Hash)) then return 'yaml' end return nil rescue Exception return nil end def self.inherited(subsclass) @_subclasses ||= [] @_subclasses << subsclass unless @_subclasses.include?(subsclass) end def self.registered_plugins() @_subclasses || [] end end def self.connect(cliargs) @_args = cliargs return Onering::API.new({ 'autoconnect' => cliargs[:autoconnect], 'config' => { 'url' => cliargs[:url], 'path' => cliargs[:path], 'source' => cliargs[:source], 'nosslverify' => cliargs[:nosslverify], 'params' => Hash[(cliargs[:param] || []).collect{|i| i.split('=',2) }], 'authentication' => { 'type' => (cliargs[:apikey_given] ? :token : nil), 'keyfile' => (cliargs[:apikey] || cliargs[:sslkey]) }.compact }.compact }.compact) end def self.output(data, format) return nil if @_args[:quiet] return nil if data.nil? Onering::Logger.debug("Outputting data as #{format}:", "Onering::CLI") case format when 'text', 'txt' if data.is_a?(Hash) data.coalesce.each do |k,v| puts k.to_s+': '+v.to_s end elsif data.is_a?(Array) and data.first.is_a?(Array) puts data.collect{|i| i.map(&:to_s).join(@_args.get(:separator, "\t")) }.sort.join("\n") else [*data].each do |d| if d.is_a?(Hash) d.coalesce.each do |k,v| puts k.to_s+': '+v.to_s end else puts d unless d.nil? or d.to_s.empty? end end end when 'json' puts MultiJson.dump(data, :pretty => true) when 'yaml' puts YAML.dump(data) else Onering::Logger.error("Unknown output format #{format.inspect}", "Onering::CLI") end return nil end end end