Class: Bolt::Outputter::JSON

Inherits:
Bolt::Outputter show all
Defined in:
lib/bolt/outputter/json.rb

Instance Method Summary collapse

Methods inherited from Bolt::Outputter

for_format, #indent, #spin, #start_spin, #stop_spin

Constructor Details

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

Returns a new instance of JSON.



6
7
8
9
10
11
# File 'lib/bolt/outputter/json.rb', line 6

def initialize(color, verbose, trace, spin, stream = $stdout)
  super
  @items_open = false
  @object_open = false
  @preceding_item = false
end

Instance Method Details

#fatal_error(err) ⇒ Object



180
181
182
183
184
185
186
187
188
189
190
# File 'lib/bolt/outputter/json.rb', line 180

def fatal_error(err)
  @stream.puts "],\n" if @items_open
  @stream.puts '"_error": ' if @object_open
  err_obj = err.to_h
  if @trace && err.backtrace
    err_obj[:details] ||= {}
    err_obj[:details][:backtrace] = err.backtrace
  end
  @stream.puts err_obj.to_json
  @stream.puts '}' if @object_open
end

#handle_event(event) ⇒ Object



20
21
22
23
24
25
26
27
28
29
# File 'lib/bolt/outputter/json.rb', line 20

def handle_event(event)
  case event[:type]
  when :node_result
    print_result(event[:result])
  when :message
    print_message(event[:message])
  when :verbose
    print_message(event[:message]) if @verbose
  end
end


197
198
199
# File 'lib/bolt/outputter/json.rb', line 197

def print_action_step(step)
  $stderr.puts(step)
end


106
107
108
# File 'lib/bolt/outputter/json.rb', line 106

def print_apply_result(apply_result)
  @stream.puts apply_result.to_json
end

Print inventory group information.

Parameters:

  • count (Integer)

    Number of groups in the inventory.

  • groups (Array)

    Names of groups in the inventory.



176
177
178
# File 'lib/bolt/outputter/json.rb', line 176

def print_groups(count:, groups:, **_kwargs)
  @stream.puts({ count: count, groups: groups }.to_json)
end

Print the guide for the specified topic.

Parameters:

  • guide (String)

    The guide.

  • topic (String)

    The topic.



132
133
134
# File 'lib/bolt/outputter/json.rb', line 132

def print_guide(**kwargs)
  @stream.puts(kwargs.to_json)
end


13
14
15
16
17
18
# File 'lib/bolt/outputter/json.rb', line 13

def print_head
  @stream.puts '{ "items": ['
  @preceding_item = false
  @items_open = true
  @object_open = true
end


192
193
194
# File 'lib/bolt/outputter/json.rb', line 192

def print_message(message)
  $stderr.puts(message)
end


98
99
100
# File 'lib/bolt/outputter/json.rb', line 98

def print_new_plan(**kwargs)
  print_table(**kwargs)
end


102
103
104
# File 'lib/bolt/outputter/json.rb', line 102

def print_new_policy(**kwargs)
  print_table(**kwargs)
end


80
81
82
83
84
85
86
87
88
# File 'lib/bolt/outputter/json.rb', line 80

def print_plan_info(plan)
  path = plan.delete('module')
  plan['module_dir'] = if path.start_with?(Bolt::Config::Modulepath::MODULES_PATH)
                         "built-in module"
                       else
                         path
                       end
  @stream.puts plan.to_json
end


136
137
138
# File 'lib/bolt/outputter/json.rb', line 136

def print_plan_lookup(value)
  @stream.puts(value.to_json)
end


110
111
112
113
# File 'lib/bolt/outputter/json.rb', line 110

def print_plan_result(result)
  # Ruby JSON patches most objects to have a to_json method.
  @stream.puts result.to_json
end


94
95
96
# File 'lib/bolt/outputter/json.rb', line 94

def print_plans(**kwargs)
  print_table(**kwargs)
end


75
76
77
78
# File 'lib/bolt/outputter/json.rb', line 75

def print_plugin_list(plugins:, modulepath:)
  plugins.delete(:validate_resolve_reference)
  print_table('plugins' => plugins, 'modulepath' => modulepath)
end


90
91
92
# File 'lib/bolt/outputter/json.rb', line 90

def print_policy_list(**kwargs)
  print_table(**kwargs)
end


140
141
142
143
144
# File 'lib/bolt/outputter/json.rb', line 140

def print_puppetfile_result(success, puppetfile, moduledir)
  @stream.puts({ success: success,
                 puppetfile: puppetfile,
                 moduledir: moduledir.to_s }.to_json)
end


31
32
33
34
35
# File 'lib/bolt/outputter/json.rb', line 31

def print_result(result)
  @stream.puts ',' if @preceding_item
  @stream.puts result.to_json
  @preceding_item = true
end


115
116
117
# File 'lib/bolt/outputter/json.rb', line 115

def print_result_set(result_set)
  @stream.puts result_set.to_json
end


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

def print_summary(results, elapsed_time)
  @stream.puts "],\n"
  @preceding_item = false
  @items_open = false
  @stream.puts format('"target_count": %<size>d, "elapsed_time": %<elapsed>d }',
                      size: results.size,
                      elapsed: elapsed_time)
end


46
47
48
# File 'lib/bolt/outputter/json.rb', line 46

def print_table(results)
  @stream.puts results.to_json
end

Print target names and where they came from.

Parameters:

  • adhoc (Hash)

    Adhoc targets provided on the command line.

  • inventory (Hash)

    Targets provided from the inventory.

  • targets (Array)

    All targets.

  • count (Integer)

    Number of targets.



167
168
169
# File 'lib/bolt/outputter/json.rb', line 167

def print_target_info(adhoc:, inventory:, targets:, count:, **_kwargs)
  @stream.puts({ adhoc: adhoc, inventory: inventory, targets: targets, count: count }.to_json)
end

Print target names and where they came from.

Parameters:

  • adhoc (Hash)

    Adhoc targets provided on the command line.

  • inventory (Hash)

    Targets provided from the inventory.

  • targets (Array)

    All targets.

  • count (Integer)

    Number of targets.



153
154
155
156
157
158
# File 'lib/bolt/outputter/json.rb', line 153

def print_targets(adhoc:, inventory:, targets:, count:, **_kwargs)
  adhoc[:targets]     = adhoc[:targets].map { |t| t['name'] }
  inventory[:targets] = inventory[:targets].map { |t| t['name'] }
  targets             = targets.map { |t| t['name'] }
  @stream.puts({ adhoc: adhoc, inventory: inventory, targets: targets, count: count }.to_json)
end

Print information about a task.

Parameters:



56
57
58
59
60
61
62
63
64
# File 'lib/bolt/outputter/json.rb', line 56

def print_task_info(task:)
  path = task.files.first['path'].chomp("/tasks/#{task.files.first['name']}")
  module_dir = if path.start_with?(Bolt::Config::Modulepath::MODULES_PATH)
                 "built-in module"
               else
                 path
               end
  @stream.puts task.to_h.merge(module_dir: module_dir).to_json
end

List available tasks.

Parameters:

  • tasks (Array)

    A list of task names and descriptions.

  • modulepath (Array)

    The modulepath.



71
72
73
# File 'lib/bolt/outputter/json.rb', line 71

def print_tasks(**kwargs)
  print_table(**kwargs)
end

Print available guide topics.

Parameters:

  • topics (Array)

    The available topics.



123
124
125
# File 'lib/bolt/outputter/json.rb', line 123

def print_topics(**kwargs)
  print_table(kwargs)
end