Class: ForemanMaintain::Reporter::CLIReporter
- Inherits:
-
ForemanMaintain::Reporter
- Object
- ForemanMaintain::Reporter
- ForemanMaintain::Reporter::CLIReporter
- Includes:
- Concerns::Logger
- Defined in:
- lib/foreman_maintain/reporter/cli_reporter.rb
Defined Under Namespace
Classes: Spinner
Constant Summary
Constants inherited from ForemanMaintain::Reporter
Instance Attribute Summary collapse
-
#last_line ⇒ Object
readonly
Returns the value of attribute last_line.
-
#max_length ⇒ Object
readonly
Returns the value of attribute max_length.
-
#select_option_counter ⇒ Object
Returns the value of attribute select_option_counter.
Instance Method Summary collapse
- #after_execution_finishes(execution) ⇒ Object
- #after_scenario_finishes(scenario) ⇒ Object
- #ask(message, options = {}) ⇒ Object
-
#ask_decision(message, actions_msg: 'y(yes), n(no), q(quit)', ignore_assumeyes: false, run_strategy: :fail_fast) ⇒ Object
rubocop:disable Metrics/LineLength.
-
#ask_to_select(message, steps, run_strategy) ⇒ Object
rubocop:disable Metrics/MethodLength.
- #assumeyes? ⇒ Boolean
- #before_execution_starts(execution) ⇒ Object
- #before_scenario_starts(scenario) ⇒ Object
- #clear_line ⇒ Object
- #execution_info(execution, text) ⇒ Object
-
#filter_decision(answer) ⇒ Object
rubocop:enable Metrics/LineLength.
- #hline(line_char = @line_char) ⇒ Object
-
#initialize(stdout = STDOUT, stdin = STDIN, options = {}) ⇒ CLIReporter
constructor
A new instance of CLIReporter.
- #multiple_steps_decision(steps, run_strategy) ⇒ Object
- #new_line_if_needed ⇒ Object
- #print(string) ⇒ Object
- #puts(string) ⇒ Object
- #puts_status(status) ⇒ Object
- #record_last_line(string) ⇒ Object
- #single_step_decision(step, run_strategy) ⇒ Object
- #status_label(status) ⇒ Object
-
#until_valid_decision ⇒ Object
loop over the block until it returns some non-false value.
- #with_spinner(message) ⇒ Object
Methods included from Concerns::Logger
Methods inherited from ForemanMaintain::Reporter
Constructor Details
#initialize(stdout = STDOUT, stdin = STDIN, options = {}) ⇒ CLIReporter
Returns a new instance of CLIReporter.
73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 73 def initialize(stdout = STDOUT, stdin = STDIN, = {}) @stdout = stdout @stdin = stdin .(:assumeyes) @assumeyes = .fetch(:assumeyes, false) @hl = HighLine.new(@stdin, @stdout) @max_length = 80 @line_char = '-' @cell_char = '|' @spinner = Spinner.new(self) @spinner.start_spinner if @stdout.tty? @last_line = '' @select_option_counter = 0 end |
Instance Attribute Details
#last_line ⇒ Object (readonly)
Returns the value of attribute last_line.
71 72 73 |
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 71 def last_line @last_line end |
#max_length ⇒ Object (readonly)
Returns the value of attribute max_length.
71 72 73 |
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 71 def max_length @max_length end |
#select_option_counter ⇒ Object
Returns the value of attribute select_option_counter.
70 71 72 |
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 70 def select_option_counter @select_option_counter end |
Instance Method Details
#after_execution_finishes(execution) ⇒ Object
149 150 151 152 153 154 155 156 |
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 149 def after_execution_finishes(execution) puts_status(execution.status) puts(execution.output) unless execution.output.empty? puts(already_run_msg) if execution.status == :already_run hline new_line_if_needed logger.info("--- Execution step '#{execution.name}' finished ---") end |
#after_scenario_finishes(scenario) ⇒ Object
158 159 160 161 162 |
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 158 def after_scenario_finishes(scenario) (scenario) puts "\n" logger.info("=== Scenario '#{scenario.description || scenario.class}' finished ===") end |
#ask(message, options = {}) ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 119 def ask(, = {}) new_line_if_needed .(:password) # the answer is confirmed by ENTER which will emit a new line @new_line_next_time = false @last_line = '' # add space at the end as otherwise highline would add new line there :/ = "#{} " unless =~ /\s\Z/ answer = @hl.ask() { |q| q.echo = false if [:password] } answer.to_s.chomp if answer end |
#ask_decision(message, actions_msg: 'y(yes), n(no), q(quit)', ignore_assumeyes: false, run_strategy: :fail_fast) ⇒ Object
rubocop:disable Metrics/LineLength
191 192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 191 def ask_decision(, actions_msg: 'y(yes), n(no), q(quit)', ignore_assumeyes: false, run_strategy: :fail_fast) actions_msg = 'y(yes), n(no)' if run_strategy == :fail_slow if !ignore_assumeyes && assumeyes? print("#{} (assuming yes)\n") return :yes end until_valid_decision do filter_decision(ask("#{}, [#{actions_msg}]")) end ensure clear_line end |
#ask_to_select(message, steps, run_strategy) ⇒ Object
rubocop:disable Metrics/MethodLength
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 |
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 215 def ask_to_select(, steps, run_strategy) if assumeyes? step = steps[@select_option_counter] @select_option_counter += 1 puts("(assuming option #{@select_option_counter})") return step end until_valid_decision do actions = run_strategy == :fail_slow ? 'n(next)' : 'n(next), q(quit)' answer = ask("#{}, [#{actions}]") if answer =~ /^\d+$/ && (answer.to_i - 1) < steps.size steps[answer.to_i - 1] else decision = filter_decision(answer) if decision == :yes steps.first else decision end end end ensure clear_line end |
#assumeyes? ⇒ Boolean
168 169 170 |
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 168 def assumeyes? @assumeyes end |
#before_execution_starts(execution) ⇒ Object
94 95 96 97 98 |
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 94 def before_execution_starts(execution) label = execution.step.label.to_s.tr('_', '-') logger.info("--- Execution step '#{execution.name}' [#{label}] started ---") puts(execution_info(execution, '')) end |
#before_scenario_starts(scenario) ⇒ Object
88 89 90 91 92 |
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 88 def before_scenario_starts(scenario) logger.info("=== Scenario '#{scenario.description || scenario.class}' started ===") puts "Running #{scenario.description || scenario.class}" hline('=') end |
#clear_line ⇒ Object
164 165 166 |
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 164 def clear_line print "\r" + ' ' * @max_length + "\r" end |
#execution_info(execution, text) ⇒ Object
249 250 251 252 |
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 249 def execution_info(execution, text) prefix = "#{execution.name}:" "#{prefix} #{text}" end |
#filter_decision(answer) ⇒ Object
rubocop:enable Metrics/LineLength
205 206 207 208 209 210 211 212 |
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 205 def filter_decision(answer) decision = nil answer = answer.downcase DECISION_MAPPER.each do |, decision_label| decision = decision_label if .include?(answer) end decision end |
#hline(line_char = @line_char) ⇒ Object
277 278 279 |
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 277 def hline(line_char = @line_char) puts line_char * @max_length end |
#multiple_steps_decision(steps, run_strategy) ⇒ Object
182 183 184 185 186 187 188 |
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 182 def multiple_steps_decision(steps, run_strategy) puts 'There are multiple steps to proceed:' steps.each_with_index do |step, index| puts "#{index + 1}) #{step.}" end ask_to_select('Select step to continue', steps, run_strategy) end |
#new_line_if_needed ⇒ Object
131 132 133 134 135 136 137 |
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 131 def new_line_if_needed if @new_line_next_time @stdout.print("\n") @stdout.flush @new_line_next_time = false end end |
#print(string) ⇒ Object
100 101 102 103 104 105 |
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 100 def print(string) new_line_if_needed @stdout.print(string) @stdout.flush record_last_line(string) end |
#puts(string) ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 107 def puts(string) # we don't print the new line right away, as we want to be able to put # the status label at the end of the last line, if possible. # Therefore, we just mark that we need to print the new line next time # we are printing something. new_line_if_needed @stdout.print(string) @stdout.flush @new_line_next_time = true record_last_line(string) end |
#puts_status(status) ⇒ Object
254 255 256 257 258 259 260 261 262 263 |
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 254 def puts_status(status) label_offset = 10 padding = @max_length - @last_line.to_s.size - label_offset if padding < 0 new_line_if_needed padding = @max_length - label_offset end @stdout.print(' ' * padding + status_label(status)) @new_line_next_time = true end |
#record_last_line(string) ⇒ Object
281 282 283 |
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 281 def record_last_line(string) @last_line = string.lines.to_a.last end |
#single_step_decision(step, run_strategy) ⇒ Object
172 173 174 175 176 177 178 179 180 |
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 172 def single_step_decision(step, run_strategy) answer = ask_decision("Continue with step [#{step.}]?", run_strategy: run_strategy) if answer == :yes step else answer end end |
#status_label(status) ⇒ Object
265 266 267 268 269 270 271 272 273 274 275 |
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 265 def status_label(status) mapping = { :success => { :label => '[OK]', :color => :green }, :fail => { :label => '[FAIL]', :color => :red }, :abort => { :label => '[ABORTED]', :color => :red }, :running => { :label => '[RUNNING]', :color => :blue }, :skipped => { :label => '[SKIPPED]', :color => :yellow }, :already_run => { :label => '[ALREADY RUN]', :color => :yellow }, :warning => { :label => '[WARNING]', :color => :yellow } } properties = mapping[status] @hl.color(properties[:label], properties[:color], :bold) end |
#until_valid_decision ⇒ Object
loop over the block until it returns some non-false value
243 244 245 246 247 |
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 243 def until_valid_decision decision = nil decision = yield until decision decision end |
#with_spinner(message) ⇒ Object
139 140 141 142 143 144 145 146 147 |
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 139 def with_spinner() new_line_if_needed @spinner.activate @spinner.update() yield @spinner ensure @spinner.deactivate @new_line_next_time = true end |