module Onering module CLI require 'yaml' require 'multi_json' @_args = {} 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] Onering::Logger.debug("Outputting data as #{format}:", "Onering::CLI") case format when 'text' if data.is_a?(Hash) data.coalesce.each do |k,v| puts k.to_s+': '+v.to_s end 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) when 'yaml' puts YAML.dump(data) else Onering::Logger.error("Unknown output format #{format.inspect}", "Onering::CLI") end nil end end end