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.
Instance Method Summary collapse
- #after_execution_finishes(execution) ⇒ Object
- #after_scenario_finishes(scenario) ⇒ Object
- #ask(message, options = {}) ⇒ Object
- #ask_decision(message, options = 'y(yes), n(no), q(quit)', ignore_assumeyes: false) ⇒ Object
-
#ask_to_select(message, steps) ⇒ 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
- #hline(line_char = @line_char) ⇒ Object
-
#initialize(stdout = STDOUT, stdin = STDIN, options = {}) ⇒ CLIReporter
constructor
A new instance of CLIReporter.
- #multiple_steps_decision(steps) ⇒ 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) ⇒ 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 |
# 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) @last_line = '' 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 |
Instance Method Details
#after_execution_finishes(execution) ⇒ Object
147 148 149 150 151 152 153 154 |
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 147 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
156 157 158 159 160 |
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 156 def after_scenario_finishes(scenario) (scenario) puts "\n" logger.info("=== Scenario '#{scenario.description || scenario.class}' finished ===") end |
#ask(message, options = {}) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 117 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 :/ = "#{message} " unless =~ /\s\Z/ answer = @hl.ask() { |q| q.echo = false if [:password] } answer.to_s.chomp if answer end |
#ask_decision(message, options = 'y(yes), n(no), q(quit)', ignore_assumeyes: false) ⇒ Object
187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 187 def ask_decision(, = 'y(yes), n(no), q(quit)', ignore_assumeyes: false) if !ignore_assumeyes && assumeyes? print("#{message} (assuming yes)\n") return :yes end until_valid_decision do filter_decision(ask("#{message}, [#{options}]")) end ensure clear_line end |
#ask_to_select(message, steps) ⇒ Object
rubocop:disable Metrics/MethodLength
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 |
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 209 def ask_to_select(, steps) if assumeyes? puts('(assuming first option)') return steps.first end until_valid_decision do answer = ask("#{message}, [n(next), q(quit)]") 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
166 167 168 |
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 166 def assumeyes? @assumeyes end |
#before_execution_starts(execution) ⇒ Object
92 93 94 95 96 |
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 92 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
86 87 88 89 90 |
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 86 def before_scenario_starts(scenario) logger.info("=== Scenario '#{scenario.description || scenario.class}' started ===") puts "Running #{scenario.description || scenario.class}" hline('=') end |
#clear_line ⇒ Object
162 163 164 |
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 162 def clear_line print "\r" + ' ' * @max_length + "\r" end |
#execution_info(execution, text) ⇒ Object
239 240 241 242 |
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 239 def execution_info(execution, text) prefix = "#{execution.name}:" "#{prefix} #{text}" end |
#filter_decision(answer) ⇒ Object
199 200 201 202 203 204 205 206 |
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 199 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
267 268 269 |
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 267 def hline(line_char = @line_char) puts line_char * @max_length end |
#multiple_steps_decision(steps) ⇒ Object
179 180 181 182 183 184 185 |
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 179 def multiple_steps_decision(steps) puts 'There are multiple steps to proceed:' steps.each_with_index do |step, index| puts "#{index + 1}) #{step.runtime_message}" end ask_to_select('Select step to continue', steps, &:runtime_message) end |
#new_line_if_needed ⇒ Object
129 130 131 132 133 134 135 |
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 129 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
98 99 100 101 102 103 |
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 98 def print(string) new_line_if_needed @stdout.print(string) @stdout.flush record_last_line(string) end |
#puts(string) ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 105 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
244 245 246 247 248 249 250 251 252 253 |
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 244 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
271 272 273 |
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 271 def record_last_line(string) @last_line = string.lines.to_a.last end |
#single_step_decision(step) ⇒ Object
170 171 172 173 174 175 176 177 |
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 170 def single_step_decision(step) answer = ask_decision("Continue with step [#{step.runtime_message}]?") if answer == :yes step else answer end end |
#status_label(status) ⇒ Object
255 256 257 258 259 260 261 262 263 264 265 |
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 255 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
233 234 235 236 237 |
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 233 def until_valid_decision decision = nil decision = yield until decision decision end |
#with_spinner(message) ⇒ Object
137 138 139 140 141 142 143 144 145 |
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 137 def with_spinner() new_line_if_needed @spinner.activate @spinner.update() yield @spinner ensure @spinner.deactivate @new_line_next_time = true end |