Class: Bolt::Rerun

Inherits:
Object
  • Object
show all
Defined in:
lib/bolt/rerun.rb

Instance Method Summary collapse

Constructor Details

#initialize(path, save_failures) ⇒ Rerun

Returns a new instance of Rerun.



8
9
10
11
12
# File 'lib/bolt/rerun.rb', line 8

def initialize(path, save_failures)
  @path = path
  @save_failures = save_failures
  @logger = Logging.logger[self]
end

Instance Method Details

#dataObject



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/bolt/rerun.rb', line 14

def data
  @data ||= JSON.parse(File.read(@path))
  unless @data.is_a?(Array) && @data.all? { |r| r['target'] && r['status'] }
    raise Bolt::FileError.new("Missing data in rerun file: #{@path}", @path)
  end
  @data
rescue JSON::ParserError
  raise Bolt::FileError.new("Could not parse rerun file: #{@path}", @path)
rescue IOError, SystemCallError
  raise Bolt::FileError.new("Could not read rerun file: #{@path}", @path)
end

#get_targets(filter) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/bolt/rerun.rb', line 26

def get_targets(filter)
  filtered = case filter
             when 'all'
               data
             when 'failure'
               data.select { |result| result['status'] == 'failure' }
             when 'success'
               data.select { |result| result['status'] == 'success' }
             else
               raise Bolt::CLIError, "Unexpected option #{filter} for '--retry'"
             end
  filtered.map { |result| result['target'] }
end

#update(result_set) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/bolt/rerun.rb', line 40

def update(result_set)
  unless @save_failures == false
    if result_set.is_a?(Bolt::PlanResult)
      result_set = result_set.value
      result_set = result_set.result_set if result_set.is_a?(Bolt::RunFailure)
    end

    if result_set.is_a?(Bolt::ResultSet)
      data = result_set.map { |res| res.status_hash.select { |k, _| %i[target status].include? k } }
      FileUtils.mkdir_p(File.dirname(@path))
      File.write(@path, data.to_json)
    elsif File.exist?(@path)
      FileUtils.rm(@path)
    end
  end
rescue StandardError => e
  @logger.warn("Failed to save result to #{@path}: #{e.message}")
end