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
#assumeyes=, #on_next_steps, #plaintext=, #plaintext?
Constructor Details
#initialize(stdout = $stdout, stdin = $stdin, options = {}) ⇒ CLIReporter
Returns a new instance of CLIReporter.
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 75 def initialize(stdout = $stdout, stdin = $stdin, = {}) @stdout = stdout @stdin = stdin .(:assumeyes) @assumeyes = .fetch(:assumeyes, false) @plaintext = .fetch(:plaintext, 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.
73 74 75 |
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 73 def last_line @last_line end |
#max_length ⇒ Object (readonly)
Returns the value of attribute max_length.
73 74 75 |
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 73 def max_length @max_length end |
#select_option_counter ⇒ Object
Returns the value of attribute select_option_counter.
72 73 74 |
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 72 def select_option_counter @select_option_counter end |
Instance Method Details
#after_execution_finishes(execution) ⇒ Object
152 153 154 155 156 157 158 159 |
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 152 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
161 162 163 164 165 |
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 161 def after_scenario_finishes(scenario) (scenario) puts "\n" logger.info("=== Scenario '#{scenario.description || scenario.class}' finished ===") end |
#ask(message, options = {}) ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 122 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/.match?() answer = @hl.ask() { |q| q.echo = false if [:password] } answer&.to_s&.chomp end |
#ask_decision(message, actions_msg: 'y(yes), n(no), q(quit)', ignore_assumeyes: false, run_strategy: :fail_fast) ⇒ Object
rubocop:disable Metrics/LineLength
198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 198 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
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 |
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 222 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
175 176 177 |
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 175 def assumeyes? @assumeyes end |
#before_execution_starts(execution) ⇒ Object
97 98 99 100 101 |
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 97 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
91 92 93 94 95 |
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 91 def before_scenario_starts(scenario) logger.info("=== Scenario '#{scenario.description || scenario.class}' started ===") puts "Running #{scenario.description || scenario.class}" hline('=') end |
#clear_line ⇒ Object
167 168 169 170 171 172 173 |
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 167 def clear_line if plaintext? print "\n" else print "\r" + ' ' * @max_length + "\r" end end |
#execution_info(execution, text) ⇒ Object
256 257 258 259 |
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 256 def execution_info(execution, text) prefix = "#{execution.name}:" "#{prefix} #{text}" end |
#filter_decision(answer) ⇒ Object
rubocop:enable Metrics/LineLength
212 213 214 215 216 217 218 219 |
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 212 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
289 290 291 |
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 289 def hline(line_char = @line_char) puts line_char * @max_length end |
#multiple_steps_decision(steps, run_strategy) ⇒ Object
189 190 191 192 193 194 195 |
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 189 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
134 135 136 137 138 139 140 |
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 134 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
103 104 105 106 107 108 |
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 103 def print(string) new_line_if_needed @stdout.print(string) @stdout.flush record_last_line(string) end |
#puts(string) ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 110 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
261 262 263 264 265 266 267 268 269 270 |
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 261 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
293 294 295 |
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 293 def record_last_line(string) @last_line = string.lines.to_a.last end |
#single_step_decision(step, run_strategy) ⇒ Object
179 180 181 182 183 184 185 186 187 |
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 179 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
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 |
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 272 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 }, :info_warning => { :label => '[WARNING]', :color => :yellow } } properties = mapping[status] if @plaintext properties[:label] else @hl.color(properties[:label], properties[:color], :bold) end end |
#until_valid_decision ⇒ Object
loop over the block until it returns some non-false value
250 251 252 253 254 |
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 250 def until_valid_decision decision = nil decision = yield until decision decision end |
#with_spinner(message) ⇒ Object
142 143 144 145 146 147 148 149 150 |
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 142 def with_spinner() new_line_if_needed @spinner.activate @spinner.update() yield @spinner ensure @spinner.deactivate @new_line_next_time = true end |