Sha256: 2919cd346fae13f26284d93e85ab33b292d0a2db2dbd7e93d5e720045afbc3ef

Contents?: true

Size: 1.31 KB

Versions: 4

Compression:

Stored size: 1.31 KB

Contents

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],
          '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]

      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
        raise "Unknown output format #{format.inspect}"
      end

      nil
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
onering-client-0.0.73 lib/onering/cli.rb
onering-client-0.0.72 lib/onering/cli.rb
onering-client-0.0.71 lib/onering/cli.rb
onering-client-0.0.70 lib/onering/cli.rb