Sha256: 72702eefa95af54185466a5c0ca9f5dddf9ad16243ac3b94ac5e040de772138c

Contents?: true

Size: 1.86 KB

Versions: 46

Compression:

Stored size: 1.86 KB

Contents

# frozen_string_literal: true

require 'json'
require 'logging'

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

    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

    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

    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
  end
end

Version data entries

46 entries across 46 versions & 1 rubygems

Version Path
bolt-2.8.0 lib/bolt/rerun.rb
bolt-2.7.0 lib/bolt/rerun.rb
bolt-2.6.0 lib/bolt/rerun.rb
bolt-2.5.0 lib/bolt/rerun.rb
bolt-2.4.0 lib/bolt/rerun.rb
bolt-2.3.1 lib/bolt/rerun.rb
bolt-2.3.0 lib/bolt/rerun.rb
bolt-2.2.0 lib/bolt/rerun.rb
bolt-2.1.0 lib/bolt/rerun.rb
bolt-2.0.1 lib/bolt/rerun.rb
bolt-2.0.0 lib/bolt/rerun.rb
bolt-1.49.0 lib/bolt/rerun.rb
bolt-1.48.0 lib/bolt/rerun.rb
bolt-1.47.0 lib/bolt/rerun.rb
bolt-1.45.0 lib/bolt/rerun.rb
bolt-1.44.0 lib/bolt/rerun.rb
bolt-1.43.0 lib/bolt/rerun.rb
bolt-1.42.0 lib/bolt/rerun.rb
bolt-1.41.0 lib/bolt/rerun.rb
bolt-1.40.0 lib/bolt/rerun.rb