Class: Bolt::Outputter

Inherits:
Object
  • Object
show all
Defined in:
lib/bolt/outputter.rb,
lib/bolt/outputter/json.rb,
lib/bolt/outputter/human.rb,
lib/bolt/outputter/logger.rb,
lib/bolt/outputter/rainbow.rb

Direct Known Subclasses

Human, JSON, Logger

Defined Under Namespace

Classes: Human, JSON, Logger, Rainbow

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(color, verbose, trace, stream = $stdout) ⇒ Outputter

Returns a new instance of Outputter.



18
19
20
21
22
23
# File 'lib/bolt/outputter.rb', line 18

def initialize(color, verbose, trace, stream = $stdout)
  @color = color
  @verbose = verbose
  @trace = trace
  @stream = stream
end

Class Method Details

.for_format(format, color, verbose, trace) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/bolt/outputter.rb', line 5

def self.for_format(format, color, verbose, trace)
  case format
  when 'human'
    Bolt::Outputter::Human.new(color, verbose, trace)
  when 'json'
    Bolt::Outputter::JSON.new(color, verbose, trace)
  when 'rainbow'
    Bolt::Outputter::Rainbow.new(color, verbose, trace)
  when nil
    raise "Cannot use outputter before parsing."
  end
end

Instance Method Details

#format_apply_result(result) ⇒ Object



70
71
72
73
74
75
76
77
78
79
# File 'lib/bolt/outputter.rb', line 70

def format_apply_result(result)
  logs = result.resource_logs&.map do |log|
    # Omit low-level info/debug messages
    next if %w[info debug].include?(log['level'])
    indent(2, format_log(log))
  end
  hash = result.to_data
  hash['logs'] = logs unless logs.empty?
  hash
end

#format_message(message) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/bolt/outputter.rb', line 47

def format_message(message)
  case message
  when Array
    message.map { |item| format_message(item) }
  when Bolt::ApplyResult
    format_apply_result(message)
  when Bolt::Result, Bolt::ResultSet
    # This is equivalent to to_s, but formattable
    message.to_data
  when Bolt::RunFailure
    formatted_resultset = message.result_set.to_data
    message.to_h.merge('result_set' => formatted_resultset)
  when Hash
    message.each_with_object({}) do |(k, v), h|
      h[format_message(k)] = format_message(v)
    end
  when Integer, Float, NilClass
    message
  else
    message.to_s
  end
end

#indent(indent, string) ⇒ Object



25
26
27
28
# File 'lib/bolt/outputter.rb', line 25

def indent(indent, string)
  indent = ' ' * indent
  string.gsub(/^/, indent.to_s)
end

Raises:

  • (NotImplementedError)


34
35
36
# File 'lib/bolt/outputter.rb', line 34

def print_message
  raise NotImplementedError, "print_message() must be implemented by the outputter class"
end


30
31
32
# File 'lib/bolt/outputter.rb', line 30

def print_message_event(event)
  print_message(stringify(event[:message]))
end

#stringify(message) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/bolt/outputter.rb', line 38

def stringify(message)
  formatted = format_message(message)
  if formatted.is_a?(Hash) || formatted.is_a?(Array)
    ::JSON.pretty_generate(formatted)
  else
    formatted
  end
end