Sha256: 95ed1700364e9a69b711f8fedc625490e255629ac38b7cc7e008d76d3eed167f

Contents?: true

Size: 1.29 KB

Versions: 1

Compression:

Stored size: 1.29 KB

Contents

# frozen_string_literal: true

module Terracop
  # Executes Terracop on a given state file.
  class Runner
    def initialize(type, file, formatter)
      @formatter = Formatters.const_get(formatter.to_s.capitalize).new

      @type = type
      @file = file
    end

    def run
      offenses = state.map do |instance|
        Terracop::Cop.run_for(
          instance[:type], instance[:name],
          instance[:index], instance[:attributes]
        )
      end

      print formatted(offenses)

      offenses.flatten.count
    end

    def state
      @state ||= begin
        if @file
          load_state_from_file(@type, @file)
        else
          load_state_from_terraform
        end
      end
    end

    private

    def load_state_from_file(type, file)
      if type == :plan
        PlanLoader.load(file)
      else
        StateLoader.load(file)
      end
    end

    # :nocov:
    def load_state_from_terraform
      StateLoader.load_from_text(`terraform state pull`)
    rescue JSON::ParserError
      puts 'Run terracop somewhere with a state file or pass it directly ' \
           'with --state FILE'
      exit
    end
    # :nocov:

    def formatted(offenses)
      by_res = offenses.flatten.group_by { |o| "#{o[:type]}.#{o[:name]}" }
      @formatter.generate(by_res)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
terracop-0.2.0 lib/terracop/runner.rb