Class: Terracop::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/terracop/runner.rb

Overview

Executes Terracop on a given state file.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, file, formatter) ⇒ Runner

Returns a new instance of Runner.



8
9
10
11
12
13
14
15
16
# File 'lib/terracop/runner.rb', line 8

def initialize(type, file, formatter)
  @formatter = Formatters.const_get(formatter.to_s.capitalize).new

  if file
    load_state_from_file(type, file)
  else
    load_state_from_terraform
  end
end

Instance Attribute Details

#stateObject

Returns the value of attribute state.



6
7
8
# File 'lib/terracop/runner.rb', line 6

def state
  @state
end

Instance Method Details

#runObject



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/terracop/runner.rb', line 18

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

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