class Terracop::Runner

Executes Terracop on a given state file.

Public Class Methods

new(type, file, formatter) click to toggle source
# File lib/terracop/runner.rb, line 6
def initialize(type, file, formatter)
  @formatter = Formatters.const_get(formatter.to_s.capitalize).new

  @type = type
  @file = file
end

Public Instance Methods

run() click to toggle source
# File lib/terracop/runner.rb, line 13
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
state() click to toggle source
# File lib/terracop/runner.rb, line 26
def state
  @state ||= begin
    if @file
      load_state_from_file(@type, @file)
    else
      load_state_from_terraform
    end
  end
end

Private Instance Methods

formatted(offenses) click to toggle source

:nocov:

# File lib/terracop/runner.rb, line 56
def formatted(offenses)
  by_res = offenses.flatten.group_by { |o| "#{o[:type]}.#{o[:name]}" }
  @formatter.generate(by_res)
end
load_state_from_file(type, file) click to toggle source
# File lib/terracop/runner.rb, line 38
def load_state_from_file(type, file)
  if type == :plan
    PlanLoader.load(file)
  else
    StateLoader.load(file)
  end
end
load_state_from_terraform() click to toggle source

:nocov:

# File lib/terracop/runner.rb, line 47
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