Sha256: 019527d7b5b1c588aab0e8f6fcaaa94d1111e3d5d2ac1257e183b3f967b910a4

Contents?: true

Size: 1.67 KB

Versions: 42

Compression:

Stored size: 1.67 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 = Bolt::Logger.logger(self)
    end

    def data
      @data ||= Bolt::Util.read_json_file(@path, 'rerun')
      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
    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| { target: res.target.name, status: res.status } }
          FileUtils.mkdir_p(File.dirname(@path))
          File.write(@path, data.to_json)
        elsif File.exist?(@path)
          FileUtils.rm(@path)
        end
      end
    rescue StandardError => e
      Bolt::Logger.warn_once("unwriteable_file", "Failed to save result to #{@path}: #{e.message}")
    end
  end
end

Version data entries

42 entries across 42 versions & 1 rubygems

Version Path
bolt-3.29.0 lib/bolt/rerun.rb
bolt-3.28.0 lib/bolt/rerun.rb
bolt-3.27.4 lib/bolt/rerun.rb
bolt-3.27.2 lib/bolt/rerun.rb
bolt-3.27.1 lib/bolt/rerun.rb
bolt-3.26.2 lib/bolt/rerun.rb
bolt-3.26.1 lib/bolt/rerun.rb
bolt-3.25.0 lib/bolt/rerun.rb
bolt-3.24.0 lib/bolt/rerun.rb
bolt-3.23.1 lib/bolt/rerun.rb
bolt-3.23.0 lib/bolt/rerun.rb
bolt-3.22.1 lib/bolt/rerun.rb
bolt-3.22.0 lib/bolt/rerun.rb
bolt-3.21.0 lib/bolt/rerun.rb
bolt-3.20.0 lib/bolt/rerun.rb
bolt-3.19.0 lib/bolt/rerun.rb
bolt-3.18.0 lib/bolt/rerun.rb
bolt-3.17.0 lib/bolt/rerun.rb
bolt-3.16.1 lib/bolt/rerun.rb
bolt-3.16.0 lib/bolt/rerun.rb