Module: Smartdc::CliHelper

Instance Method Summary collapse

Instance Method Details

#describe(name, content, options) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/smartdc/cli_helper.rb', line 80

def describe(name, content, options)
  if options['all'] or options['output'] == name
    options[:exclude] ||= []
    rows = []

    content[name].each do |row|
      cols = []
      if options[:cols] == :all
        options[:exclude].each do |col|
          row.delete(col.to_s)
        end
        cols = row.values
      else
        options[:cols].each do |col|
          if col == 'key'
            cols << row[0]
          else
           cols << row[1][col]
          end
        end
      end
      rows << cols
    end

    title = options['all'] ? name : nil
    headings = options[:cols] == :all ? content[name][0].keys : options[:cols]
    puts Terminal::Table.new :title => title, :headings => headings, :rows => rows
    puts if options['all']
  end
end

#horizontal(content, options = {}) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/smartdc/cli_helper.rb', line 40

def horizontal(content, options={})
  options[:include] ||= []
  options[:exclude] ||= []
  rows = []
  headings = nil

  content.each do |row|
    options[:exclude].each do |col|
      row.delete(col.to_s)
    end

    if !options[:include].empty?
      cols = {}
      options[:include].each do |col|
        cols[col.to_s] = row[col.to_s] if row.key?(col.to_s)
      end
      row = cols
    end

    rows << row.values
    headings = row.keys if headings.nil?
  end

  Terminal::Table.new :headings => headings, :rows => rows if !content.empty?
end

#output(response, options = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/smartdc/cli_helper.rb', line 15

def output(response, options={})
  if options['raw']
    puts response.body
  else
    if response.status < 300
      if options[:table]
        case options[:table]
        when :horizontal, :h
          puts horizontal(response.content, options)
        when :vertical, :v
          puts vertical(response.content, options)
        end
      else
        if options[:message]
          puts options[:message]
        else
          puts response.content[options[:only].to_s]
        end
      end
    else
      puts vertical(response.content)
    end
  end
end

#use_machine(id) ⇒ Object



5
6
7
8
9
10
11
12
13
# File 'lib/smartdc/cli_helper.rb', line 5

def use_machine(id)
  if id.nil?
    clicfg = Smartdc::CliConfigure.new
    sdccfg = clicfg.read
    sdccfg[:use_machine]
  else
     id
  end
end

#vertical(content, options = {}) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/smartdc/cli_helper.rb', line 66

def vertical(content, options={})
  options[:exclude] ||= []
  options[:exclude].each do |col|
    content.delete(col.to_s)
  end

  rows = []
  content.to_a.each do |row|
    rows << row
  end

  Terminal::Table.new :headings => ['key','value'], :rows => rows if !content.empty?
end