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], '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] 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.empty? end end end when 'json' puts MultiJson.dump(data) when 'yaml' puts YAML.dump(data) else raise "Unknown output format #{format.inspect}" end nil end end end