Class: Puppet_X::Binford2k::Itemize::Cli

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet_x/binford2k/itemize/cli.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Cli

Returns a new instance of Cli.



2
3
4
# File 'lib/puppet_x/binford2k/itemize/cli.rb', line 2

def initialize(options = {})
  @options = options
end

Instance Method Details

#render!Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/puppet_x/binford2k/itemize/cli.rb', line 12

def render!
  run!

  case @options[:render]
  when :human
    puts to_s
  when :json
    puts to_json
  when :yaml
    puts to_yaml
  else
    raise "Invalid render type (#{@options[:render]})."
  end
end

#run!Object



6
7
8
9
10
# File 'lib/puppet_x/binford2k/itemize/cli.rb', line 6

def run!
  runner   = Puppet_X::Binford2k::Itemize::Runner.new(@options).run!
  @results = runner.results
  self
end

#to_jsonObject



51
52
53
54
# File 'lib/puppet_x/binford2k/itemize/cli.rb', line 51

def to_json
  require 'json'
  JSON.pretty_generate(@results)
end

#to_sObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/puppet_x/binford2k/itemize/cli.rb', line 27

def to_s
  name_width  = 2
  count_width = 2
  @results.each do |kind, counts|
    counts.each do |name, count|
      name_width  = [name_width, name.size].max
      count_width = [count_width, count.to_s.size].max
    end
  end

  output  = "Resource usage analysis:\n"
  output << '=' * (name_width + count_width + 8)
  output << "\n"
  @results.each do |kind, counts|
    output << ">> #{kind}:\n"

    counts.each do |name, count|
      output << sprintf("    %#{name_width}s | %#{count_width}s\n", name, count)
    end
    output << "\n"
  end
  output
end

#to_yamlObject



56
57
58
59
# File 'lib/puppet_x/binford2k/itemize/cli.rb', line 56

def to_yaml
  require 'yaml'
  @results.to_yaml
end