Module: Collins::CLI::Formatter

Included in:
DC, Find, IPAM, State
Defined in:
lib/collins/cli/formatter.rb

Constant Summary collapse

FORMATTING_DEFAULTS =
{
  :format           => :table,           # how to display the results
  :separator        => "\t",
  :columns          => [:tag, :hostname, :nodeclass, :status, :pool, :primary_role, :secondary_role],
  :column_override  => [],       # if set, these are the columns to display
  :show_header      => false,        # if the header for columns should be displayed
}
ADDRESS_POOL_COLUMNS =

if the header for columns should be displayed

[:name, :network, :start_address, :specified_gateway, :gateway, :broadcast, :possible_addresses]
STATUS_STATE_COLUMNS =
[:status_name, :state_name, :status_description, :description]

Instance Method Summary collapse

Instance Method Details



92
93
94
95
96
# File 'lib/collins/cli/formatter.rb', line 92

def display_as_link assets, client
  assets.each do |a|
    puts "#{client.host}/asset/#{a.tag}"
  end
end

#display_as_robot_talk(assets, format = :json) ⇒ Object



73
74
75
# File 'lib/collins/cli/formatter.rb', line 73

def display_as_robot_talk(assets, format = :json)
  puts assets.send("to_#{format}".to_sym)
end

#display_as_table(assets, columns, separator, show_header = false) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/collins/cli/formatter.rb', line 76

def display_as_table(assets, columns, separator, show_header = false)
  # lets figure out how wide each column is, including header
  column_width_pairs = columns.map do |column|
    # grab all attributes == column and figure out max width
    width = assets.map{|a| (column == :state) ?  a.send(column).label.to_s.length : a.send(column).to_s.length}.max
    width = [width, column.to_s.length].max if show_header
    [column,width]
  end
  column_width_map = Hash[column_width_pairs]
  if show_header
    $stderr.puts column_width_map.map{|c,w| "%-#{w}s" % c}.join(separator)
  end
  assets.each do |a|
    puts column_width_map.map {|c,w| v = (c == :state) ?  a.send(c).label : a.send(c) ; "%-#{w}s" % v }.join(separator)
  end
end

#format_assets(assets, opts = {}) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/collins/cli/formatter.rb', line 47

def format_assets(assets, opts = {})
  opts = FORMATTING_DEFAULTS.merge(opts)
  if assets.length > 0
    case opts[:format]
    when :table
      # if the user passed :column_override, respect that absolutely. otherwise, the columns to display
      # should be opts[:columns] + any extra attributes queried for. this way ```cf -c hostname -a is_vm:true```
      # wont return 2 columns; only the one you asked for
      columns = if opts[:column_override].empty?
                  opts[:columns].concat(search_attrs.keys).compact.uniq
                else
                  opts[:column_override]
                end
      display_as_table(assets,columns,opts[:separator],opts[:show_header])
    when :link
      display_as_link assets, collins
    when :json,:yaml
      display_as_robot_talk(assets,opts[:format])
    else
      raise "I don't know how to display assets in #{opts[:format]} format!"
    end
  else
    raise "No assets found"
  end
end

#format_pools(pools, opts = {}) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/collins/cli/formatter.rb', line 14

def format_pools(pools, opts = {})
  if pools.length > 0
    opts = FORMATTING_DEFAULTS.merge(opts)
    # map the hashes into openstructs that will respond to #send(:name)
    ostructs = pools.map { |p| OpenStruct.new(Hash[p.map {|k,v| [k.downcase,v]}]) }
    display_as_table(ostructs, ADDRESS_POOL_COLUMNS, opts[:separator], opts[:show_header])
  else
    raise "No pools found"
  end
end

#format_states(states, opts = {}) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/collins/cli/formatter.rb', line 25

def format_states(states, opts = {})
  if states.length > 0
    opts = FORMATTING_DEFAULTS.merge(opts)
    # map the hashes into openstructs that will respond to #send(:name)
    ostructs = states.map do |s|
      OpenStruct.new({
        :status_name => s.status.name || 'Any',
        :status_id => s.status.id || 0, # assign 0 to "Any" status
        :status_description => s.status.description || 'Any status',
        :state_name => s.name,
        :state_label => s.label,
        :state_id => s.id,
        :description => s.description
      })
    end
    ostructs.sort_by! {|x| "#{x.status_id}#{x.state_id}"}
    display_as_table(ostructs, STATUS_STATE_COLUMNS, opts[:separator], opts[:show_header])
  else
    raise "No states found"
  end
end