Class: Bolt::Outputter::Human
- Inherits:
-
Bolt::Outputter
- Object
- Bolt::Outputter
- Bolt::Outputter::Human
- Defined in:
- lib/bolt/outputter/human.rb
Constant Summary collapse
- COLORS =
{ red: "31", green: "32", yellow: "33" }.freeze
Instance Method Summary collapse
- #colorize(color, string) ⇒ Object
- #fatal_error(err) ⇒ Object
- #indent(indent, string) ⇒ Object
- #print_apply_result(apply_result) ⇒ Object
- #print_event(event) ⇒ Object
- #print_head ⇒ Object
- #print_plan_info(plan) ⇒ Object
- #print_plan_result(plan_result) ⇒ Object
- #print_puppetfile_result(success, puppetfile, moduledir) ⇒ Object
- #print_result(result) ⇒ Object
- #print_start(target) ⇒ Object
- #print_summary(results, elapsed_time = nil) ⇒ Object
- #print_table(results) ⇒ Object
- #print_task_info(task) ⇒ Object
- #remove_trail(string) ⇒ Object
Methods inherited from Bolt::Outputter
for_format, #initialize, #print_message
Constructor Details
This class inherits a constructor from Bolt::Outputter
Instance Method Details
#colorize(color, string) ⇒ Object
15 16 17 18 19 20 21 |
# File 'lib/bolt/outputter/human.rb', line 15 def colorize(color, string) if @color && @stream.isatty "\033[#{COLORS[color]}m#{string}\033[0m" else string end end |
#fatal_error(err) ⇒ Object
209 210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/bolt/outputter/human.rb', line 209 def fatal_error(err) @stream.puts(colorize(:red, err.)) if err.is_a? Bolt::RunFailure @stream.puts ::JSON.pretty_generate(err.result_set) end if @trace && err.backtrace err.backtrace.each do |line| @stream.puts(colorize(:red, "\t#{line}")) end end end |
#indent(indent, string) ⇒ Object
23 24 25 26 |
# File 'lib/bolt/outputter/human.rb', line 23 def indent(indent, string) indent = ' ' * indent string.gsub(/^/, indent.to_s) end |
#print_apply_result(apply_result) ⇒ Object
181 182 183 184 |
# File 'lib/bolt/outputter/human.rb', line 181 def print_apply_result(apply_result) apply_result.each { |result| print_result(result) } print_summary(apply_result) end |
#print_event(event) ⇒ Object
32 33 34 35 36 37 38 39 |
# File 'lib/bolt/outputter/human.rb', line 32 def print_event(event) case event[:type] when :node_start print_start(event[:target]) when :node_result print_result(event[:result]) end end |
#print_head ⇒ Object
13 |
# File 'lib/bolt/outputter/human.rb', line 13 def print_head; end |
#print_plan_info(plan) ⇒ Object
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/bolt/outputter/human.rb', line 154 def print_plan_info(plan) # Building lots of strings... pretty_params = +"" plan_info = +"" usage = +"bolt plan run #{plan['name']}" plan['parameters']&.each do |name, p| pretty_params << "- #{name}: #{p['type']}\n" usage << (p.include?('default_value') ? " [#{name}=<value>]" : " #{name}=<value>") end plan_info << "\n#{plan['name']}" plan_info << "\n\n" plan_info << "USAGE:\n#{usage}\n\n" plan_info << "PARAMETERS:\n#{pretty_params}\n" if plan['parameters'] plan_info << "MODULE:\n" path = plan['module'] plan_info << if path.start_with?(Bolt::PAL::MODULES_PATH) "built-in module" else path end @stream.puts(plan_info) end |
#print_plan_result(plan_result) ⇒ Object
187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/bolt/outputter/human.rb', line 187 def print_plan_result(plan_result) value = plan_result.value if value.nil? @stream.puts("Plan completed successfully with no result") elsif value.is_a? Bolt::ApplyFailure @stream.puts(colorize(:red, value.)) elsif value.is_a? Bolt::ResultSet value.each { |result| print_result(result) } print_summary(value) else @stream.puts(::JSON.pretty_generate(plan_result, quirks_mode: true)) end end |
#print_puppetfile_result(success, puppetfile, moduledir) ⇒ Object
201 202 203 204 205 206 207 |
# File 'lib/bolt/outputter/human.rb', line 201 def print_puppetfile_result(success, puppetfile, moduledir) if success @stream.puts("Successfully synced modules from #{puppetfile} to #{moduledir}") else @stream.puts(colorize(:red, "Failed to sync modules from #{puppetfile} to #{moduledir}")) end end |
#print_result(result) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/bolt/outputter/human.rb', line 45 def print_result(result) if result.success? @stream.puts(colorize(:green, "Finished on #{result.target.host}:")) else @stream.puts(colorize(:red, "Failed on #{result.target.host}:")) end if result.error_hash @stream.puts(colorize(:red, remove_trail(indent(2, result.error_hash['msg'])))) end if result. @stream.puts(remove_trail(indent(2, result.))) end # There is more information to output if result.generic_value # Use special handling if the result looks like a command or script result if result.generic_value.keys == %w[stdout stderr exit_code] unless result['stdout'].strip.empty? @stream.puts(indent(2, "STDOUT:")) @stream.puts(indent(4, result['stdout'])) end unless result['stderr'].strip.empty? @stream.puts(indent(2, "STDERR:")) @stream.puts(indent(4, result['stderr'])) end else @stream.puts(indent(2, ::JSON.pretty_generate(result.generic_value))) end end end |
#print_start(target) ⇒ Object
41 42 43 |
# File 'lib/bolt/outputter/human.rb', line 41 def print_start(target) @stream.puts(colorize(:green, "Started on #{target.host}...")) end |
#print_summary(results, elapsed_time = nil) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/bolt/outputter/human.rb', line 78 def print_summary(results, elapsed_time = nil) ok_set = results.ok_set unless ok_set.empty? @stream.puts format('Successful on %<size>d node%<plural>s: %<names>s', size: ok_set.size, plural: ok_set.size == 1 ? '' : 's', names: ok_set.names.join(',')) end error_set = results.error_set unless error_set.empty? @stream.puts colorize(:red, format('Failed on %<size>d node%<plural>s: %<names>s', size: error_set.size, plural: error_set.size == 1 ? '' : 's', names: error_set.names.join(','))) end total_msg = format('Ran on %<size>d node%<plural>s', size: results.size, plural: results.size == 1 ? '' : 's') total_msg += format(' in %<elapsed>.2f seconds', elapsed: elapsed_time) unless elapsed_time.nil? @stream.puts total_msg end |
#print_table(results) ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/bolt/outputter/human.rb', line 103 def print_table(results) @stream.puts Terminal::Table.new( rows: results, style: { border_x: '', border_y: '', border_i: '', padding_left: 0, padding_right: 3, border_top: false, border_bottom: false } ) end |
#print_task_info(task) ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/bolt/outputter/human.rb', line 119 def print_task_info(task) # Building lots of strings... pretty_params = +"" task_info = +"" usage = +"bolt task run --nodes <node-name> #{task['name']}" task['metadata']['parameters']&.each do |k, v| pretty_params << "- #{k}: #{v['type'] || 'Any'}\n" pretty_params << " #{v['description']}\n" if v['description'] usage << if v['type'].is_a?(Puppet::Pops::Types::POptionalType) " [#{k}=<value>]" else " #{k}=<value>" end end usage << " [--noop]" if task['metadata']['supports_noop'] task_info << "\n#{task['name']}" task_info << " - #{task['metadata']['description']}" if task['metadata']['description'] task_info << "\n\n" task_info << "USAGE:\n#{usage}\n\n" task_info << "PARAMETERS:\n#{pretty_params}\n" unless pretty_params.empty? task_info << "MODULE:\n" path = task['files'][0]['path'].chomp("/tasks/#{task['files'][0]['name']}") task_info << if path.start_with?(Bolt::PAL::MODULES_PATH) "built-in module" else path end @stream.puts(task_info) end |
#remove_trail(string) ⇒ Object
28 29 30 |
# File 'lib/bolt/outputter/human.rb', line 28 def remove_trail(string) string.sub(/\s\z/, '') end |