Module: Chef::Knife::GithubBaseList

Defined in:
lib/chef/knife/github_baselist.rb

Class Method Summary collapse

Class Method Details

.included(includer) ⇒ Object



23
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/chef/knife/github_baselist.rb', line 23

def self.included(includer)
  includer.class_eval do

    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

    def display_info(data, columns, match = [])
      object_list = []

      if config[:fields]
        config[:fields].split(',').each { |n| object_list << ui.color(("#{n}").capitalize.strip, :bold) }
      else
        columns.each { |c| r = c.split(","); object_list << ui.color(("#{r.last}").strip, :bold) }
      end

      col = object_list.count
      object_list = [] if config[:noheader]

      data.each do |k,v|
        if config[:fields]
          config[:fields].downcase.split(',').each { |n| object_list << ((v["#{n}".strip]).to_s || 'n/a') }
        else
          color = :white
          if !match.empty? && !config[:all]
            matches = []; match.each { |m| matches << v[m].to_s }
            if matches.uniq.count == 1
              next if config[:mismatch]
            else
              color = :yellow
            end
          end
          columns.each { |c|  r = c.split(","); object_list << ui.color((v["#{r.first}"]).to_s, color) || 'n/a' }
        end
      end

      puts ui.list(object_list, :uneven_columns_across, col)
      display_object_fields(data) if locate_config_value(:fieldlist)
    end

    def display_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.each do |n|
        if n.class == Hash
          n.keys.each do |k,v|
            object_fields << ui.color(k, :yellow, :bold)
            object_fields << n[k].class.to_s
            if n[k].kind_of?(Array)
              object_fields << '<Array>'
            elsif n[k].kind_of?(Hash)
              object_fields << '<Hash>'
            else
              object_fields << ("#{n[k]}").strip.to_s
            end
          end
        end
      end
      puts "\n"
      puts ui.list(object_fields, :uneven_columns_across, 3)
    end
  end
end