Module: Collins::CLI::Formatter

Included in:
Find, IPAM
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]

Instance Method Summary collapse

Instance Method Details



69
70
71
72
73
# File 'lib/collins/cli/formatter.rb', line 69

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



50
51
52
# File 'lib/collins/cli/formatter.rb', line 50

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



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/collins/cli/formatter.rb', line 53

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



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

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



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

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