Class: ForemanMaintain::Reporter::CLIReporter

Inherits:
ForemanMaintain::Reporter show all
Includes:
Concerns::Logger
Defined in:
lib/foreman_maintain/reporter/cli_reporter.rb

Defined Under Namespace

Classes: Spinner

Constant Summary

Constants inherited from ForemanMaintain::Reporter

DECISION_MAPPER

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Concerns::Logger

#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.



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 76

def initialize(stdout = STDOUT, stdin = STDIN, options = {})
  @stdout = stdout
  @stdin = stdin
  options.validate_options!(:assumeyes)
  @assumeyes = options.fetch(:assumeyes, false)
  @plaintext = options.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_lineObject (readonly)

Returns the value of attribute last_line.



74
75
76
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 74

def last_line
  @last_line
end

#max_lengthObject (readonly)

Returns the value of attribute max_length.



74
75
76
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 74

def max_length
  @max_length
end

#select_option_counterObject

Returns the value of attribute select_option_counter.



73
74
75
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 73

def select_option_counter
  @select_option_counter
end

Instance Method Details

#after_execution_finishes(execution) ⇒ Object



153
154
155
156
157
158
159
160
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 153

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



162
163
164
165
166
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 162

def after_scenario_finishes(scenario)
  scenario_failure_message(scenario)
  puts "\n"
  logger.info("=== Scenario '#{scenario.description || scenario.class}' finished ===")
end

#ask(message, options = {}) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 123

def ask(message, options = {})
  new_line_if_needed
  options.validate_options!(: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 = "#{message} " unless message =~ /\s\Z/
  answer = @hl.ask(message) { |q| q.echo = false if options[: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



199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 199

def ask_decision(message, 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("#{message} (assuming yes)\n")
    return :yes
  end
  until_valid_decision do
    filter_decision(ask("#{message}, [#{actions_msg}]"))
  end
ensure
  clear_line
end

#ask_to_select(message, steps, run_strategy) ⇒ Object

rubocop:disable Metrics/MethodLength



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 223

def ask_to_select(message, 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("#{message}, [#{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

Returns:

  • (Boolean)


176
177
178
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 176

def assumeyes?
  @assumeyes
end

#before_execution_starts(execution) ⇒ Object



98
99
100
101
102
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 98

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



92
93
94
95
96
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 92

def before_scenario_starts(scenario)
  logger.info("=== Scenario '#{scenario.description || scenario.class}' started ===")
  puts "Running #{scenario.description || scenario.class}"
  hline('=')
end

#clear_lineObject



168
169
170
171
172
173
174
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 168

def clear_line
  if plaintext?
    print "\n"
  else
    print "\r" + ' ' * @max_length + "\r"
  end
end

#execution_info(execution, text) ⇒ Object



257
258
259
260
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 257

def execution_info(execution, text)
  prefix = "#{execution.name}:"
  "#{prefix} #{text}"
end

#filter_decision(answer) ⇒ Object

rubocop:enable Metrics/LineLength



213
214
215
216
217
218
219
220
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 213

def filter_decision(answer)
  decision = nil
  answer   = answer.downcase
  DECISION_MAPPER.each do |options, decision_label|
    decision = decision_label if options.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



190
191
192
193
194
195
196
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 190

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.runtime_message}"
  end
  ask_to_select('Select step to continue', steps, run_strategy)
end

#new_line_if_neededObject



135
136
137
138
139
140
141
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 135

def new_line_if_needed
  if @new_line_next_time
    @stdout.print("\n")
    @stdout.flush
    @new_line_next_time = false
  end
end


104
105
106
107
108
109
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 104

def print(string)
  new_line_if_needed
  @stdout.print(string)
  @stdout.flush
  record_last_line(string)
end

#puts(string) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 111

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



262
263
264
265
266
267
268
269
270
271
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 262

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



180
181
182
183
184
185
186
187
188
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 180

def single_step_decision(step, run_strategy)
  answer = ask_decision("Continue with step [#{step.runtime_message}]?",
                        run_strategy: run_strategy)
  if answer == :yes
    step
  else
    answer
  end
end

#status_label(status) ⇒ Object



273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 273

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]
  if @plaintext
    properties[:label]
  else
    @hl.color(properties[:label], properties[:color], :bold)
  end
end

#until_valid_decisionObject

loop over the block until it returns some non-false value



251
252
253
254
255
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 251

def until_valid_decision
  decision = nil
  decision = yield until decision
  decision
end

#with_spinner(message) ⇒ Object



143
144
145
146
147
148
149
150
151
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 143

def with_spinner(message)
  new_line_if_needed
  @spinner.activate
  @spinner.update(message)
  yield @spinner
ensure
  @spinner.deactivate
  @new_line_next_time = true
end