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



138
139
140
141
142
143
144
145
146
147
148
# File 'lib/bolt/outputter/json.rb', line 138

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
# 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])
  end
end


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

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


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

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


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

def print_groups(groups, _inventory_source, _default_inventory)
  count = groups.count
  @stream.puts({ groups: groups,
                 count: count }.to_json)
end


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

def print_guide(guide, topic)
  @stream.puts({
    'topic' => topic,
    'guide' => guide
  }.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


150
151
152
# File 'lib/bolt/outputter/json.rb', line 150

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


63
64
65
66
67
68
69
70
71
# File 'lib/bolt/outputter/json.rb', line 63

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


81
82
83
84
# File 'lib/bolt/outputter/json.rb', line 81

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


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

def print_plans(plans, modulepath)
  print_table('plans' => plans, 'modulepath' => modulepath)
end


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

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


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

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


86
87
88
# File 'lib/bolt/outputter/json.rb', line 86

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


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

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


44
45
46
# File 'lib/bolt/outputter/json.rb', line 44

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


123
124
125
126
127
128
129
130
# File 'lib/bolt/outputter/json.rb', line 123

def print_target_info(target_list, _inventory_source, _default_inventory, _target_flag)
  targets = target_list.values.flatten

  @stream.puts ::JSON.pretty_generate(
    targets: targets.map(&:detail),
    count: targets.count
  )
end


107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/bolt/outputter/json.rb', line 107

def print_targets(target_list, inventory_source, default_inventory, _target_flag)
  @stream.puts ::JSON.pretty_generate(
    inventory: {
      targets: target_list[:inventory].map(&:name),
      count: target_list[:inventory].count,
      file: (inventory_source || default_inventory).to_s
    },
    adhoc: {
      targets: target_list[:adhoc].map(&:name),
      count: target_list[:adhoc].count
    },
    targets: target_list.values.flatten.map(&:name),
    count: target_list.values.flatten.count
  )
end


49
50
51
52
53
54
55
56
57
# File 'lib/bolt/outputter/json.rb', line 49

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


59
60
61
# File 'lib/bolt/outputter/json.rb', line 59

def print_tasks(tasks, modulepath)
  print_table('tasks' => tasks, 'modulepath' => modulepath)
end


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

def print_topics(topics)
  print_table('topics' => topics)
end