Module: Chef::Knife::KnifecosmicBaseList

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(includer) ⇒ Object



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/chef/knife/cosmic_baselist.rb', line 26

def self.included(includer)
  includer.class_eval do
    include Chef::Knife::KnifecosmicBase

    option :filter,
           :long => "--filter 'FIELD:NAME'",
           :description => "Specify field and part of name to list"

    option :fields,
           :long => "--fields 'NAME, NAME'",
           :description => "The fields to output, comma-separated"

    option :fieldlist,
           :long => "--fieldlist",
           :description => "The available fields to output/filter",
           :boolean => true

    option :noheader,
           :long => "--noheader",
           :description => "Removes header from output",
           :boolean => true
  end
end

Instance Method Details

#list_object(columns, object) ⇒ Object



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
# File 'lib/chef/knife/cosmic_baselist.rb', line 82

def list_object(columns, object)

  output_format(object)
  object_list = []
  if locate_config_value(:fields)
    locate_config_value(:fields).split(',').each { |n| object_list << ui.color(("#{n}").strip, :bold) }
  else
    columns.each do |column|
      n = (column.split(':').first).strip
      object_list << (ui.color("#{n}", :bold) || 'N/A')
    end
  end

  n_columns = object_list.count
  object_list = [] if locate_config_value(:noheader)

  object.each do |r|
    if locate_config_value(:fields)
      locate_config_value(:fields).downcase.split(',').each { |n| object_list << ((r[("#{n}").strip]).to_s || 'N/A') }
    else
      columns.each { |column| object_list << (r["#{column.split(':').last.strip}"].to_s || 'N/A') }
    end
  end
  puts ui.list(object_list, :uneven_columns_across, n_columns)
  list_object_fields(object) if locate_config_value(:fieldlist)
end

#list_object_fields(object) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/chef/knife/cosmic_baselist.rb', line 59

def list_object_fields(object)
  exit 1 if object.nil? || object.empty?
  object_fields = [
    ui.color('Key', :bold),
    ui.color('Type', :bold),
    ui.color('Value', :bold)
  ]

  object.first.sort.each do |k,v|
    object_fields << ui.color(k, :yellow, :bold)
    object_fields << v.class.to_s
    if v.kind_of?(Hash)
      object_fields << '<Hash>'
    elsif v.kind_of?(Array)
      object_fields << '<Array>'
    else
      object_fields << ("#{v}").strip.to_s
    end
  end
  puts "\n"
  puts ui.list(object_fields, :uneven_columns_across, 3)
end

#output_format(json) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/chef/knife/cosmic_baselist.rb', line 50

def output_format(json)
  if locate_config_value(:format) =~ /^j/i
    json_hash = {};
    json.each { |k| json_hash.merge!( k['id'] => k) }
    puts JSON.pretty_generate(json_hash)
    exit 0
  end
end