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.



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

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



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

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



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

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


199
200
201
# File 'lib/bolt/outputter/json.rb', line 199

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


108
109
110
# File 'lib/bolt/outputter/json.rb', line 108

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.



178
179
180
# File 'lib/bolt/outputter/json.rb', line 178

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.



134
135
136
# File 'lib/bolt/outputter/json.rb', line 134

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


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

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


194
195
196
# File 'lib/bolt/outputter/json.rb', line 194

def print_message(message)
  $stderr.puts(Bolt::Util::Format.stringify(message))
end


100
101
102
# File 'lib/bolt/outputter/json.rb', line 100

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


104
105
106
# File 'lib/bolt/outputter/json.rb', line 104

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


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

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


138
139
140
# File 'lib/bolt/outputter/json.rb', line 138

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


112
113
114
115
# File 'lib/bolt/outputter/json.rb', line 112

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


96
97
98
# File 'lib/bolt/outputter/json.rb', line 96

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


77
78
79
80
# File 'lib/bolt/outputter/json.rb', line 77

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


92
93
94
# File 'lib/bolt/outputter/json.rb', line 92

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


142
143
144
145
146
# File 'lib/bolt/outputter/json.rb', line 142

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


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

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


117
118
119
# File 'lib/bolt/outputter/json.rb', line 117

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


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

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


48
49
50
# File 'lib/bolt/outputter/json.rb', line 48

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.



169
170
171
# File 'lib/bolt/outputter/json.rb', line 169

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.



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

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:



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

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.



73
74
75
# File 'lib/bolt/outputter/json.rb', line 73

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

Print available guide topics.

Parameters:

  • topics (Array)

    The available topics.



125
126
127
# File 'lib/bolt/outputter/json.rb', line 125

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