Class: ForemanMaintain::Reporter::CLIReporter

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

Defined Under Namespace

Classes: Spinner

Constant Summary

Constants inherited from ForemanMaintain::Reporter

DECISION_MAPPER

Instance Method Summary collapse

Methods inherited from ForemanMaintain::Reporter

#on_next_steps

Constructor Details

#initialize(stdout = STDOUT, stdin = STDIN, options = {}) ⇒ CLIReporter

Returns a new instance of CLIReporter.



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 70

def initialize(stdout = STDOUT, stdin = STDIN, options = {})
  @stdout = stdout
  @stdin = stdin
  options.validate_options!(:assumeyes)
  @assumeyes = options.fetch(:assumeyes, false)
  @hl = HighLine.new(@stdin, @stdout)
  @max_length = 80
  @line_char = '-'
  @cell_char = '|'
  @spinner = Spinner.new(self)
  @last_line = ''
end

Instance Method Details

#after_execution_finishes(execution) ⇒ Object



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

def after_execution_finishes(execution)
  puts_status(execution.status)
  puts(execution.output) unless execution.output.empty?
  if execution.status == :already_run
    puts(<<-MESSAGE.strip_heredoc)
      The step was skipped as it was already run and it is marked
      as run_once. Use --force to enforce the execution.
    MESSAGE
  end
  hline
  new_line_if_needed
end

#after_scenario_finishes(scenario) ⇒ Object



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

def after_scenario_finishes(scenario)
  scenario_failure_message(scenario)
  puts "\n"
end

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



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

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) ⇒ Object



184
185
186
187
188
189
190
191
192
193
194
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 184

def ask_decision(message)
  if assumeyes?
    print("#{message} (assuming yes)")
    return :yes
  end
  until_valid_decision do
    filter_decision(ask("#{message}, [y(yes), n(no), q(quit)]"))
  end
ensure
  clear_line
end

#ask_to_select(message, steps) ⇒ Object

rubocop:disable Metrics/MethodLength,Metrics/AbcSize



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 206

def ask_to_select(message, 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

Returns:

  • (Boolean)


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

def assumeyes?
  @assumeyes
end

#before_execution_starts(execution) ⇒ Object



88
89
90
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 88

def before_execution_starts(execution)
  puts(execution_info(execution, ''))
end

#before_scenario_starts(scenario) ⇒ Object



83
84
85
86
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 83

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

#clear_lineObject



159
160
161
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 159

def clear_line
  print "\r" + ' ' * @max_length + "\r"
end

#execution_info(execution, text) ⇒ Object



236
237
238
239
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 236

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

#filter_decision(answer) ⇒ Object



196
197
198
199
200
201
202
203
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 196

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



263
264
265
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 263

def hline(line_char = @line_char)
  puts line_char * @max_length
end

#multiple_steps_decision(steps) ⇒ Object



176
177
178
179
180
181
182
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 176

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_neededObject



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

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


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

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

#puts(string) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 99

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



241
242
243
244
245
246
247
248
249
250
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 241

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



267
268
269
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 267

def record_last_line(string)
  @last_line = string.lines.to_a.last
end

#single_step_decision(step) ⇒ Object



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

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



252
253
254
255
256
257
258
259
260
261
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 252

def status_label(status)
  mapping = { :success => { :label => '[OK]', :color => :green },
              :fail => { :label => '[FAIL]', :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_decisionObject

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



230
231
232
233
234
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 230

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

#with_spinner(message) ⇒ Object



131
132
133
134
135
136
137
138
139
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 131

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