Class: TerraformLandscape::TerraformPlan

Inherits:
Object
  • Object
show all
Defined in:
lib/terraform_landscape/terraform_plan.rb

Overview

Represents the parsed output of ‘terraform plan`.

This allows us to easily inspect the plan and present a more readable explanation of the plan to the user.

Defined Under Namespace

Classes: ParseError

Constant Summary collapse

GRAMMAR_FILE =

rubocop:disable Metrics/ClassLength

File.expand_path(File.join(File.dirname(__FILE__),
'..', '..', 'grammar',
'terraform_plan.treetop'))
CHANGE_SYMBOL_TO_COLOR =
{
  :~ => :yellow,
  :- => :red,
  :+ => :green,
  :'-/+' => :yellow,
  :'<=' => :cyan
}.freeze
DEFAULT_DIFF_CONTEXT_LINES =
5
UNICODE_ESCAPER =
proc { |s|
  format('\u%04X', s.codepoints[0])
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(plan_ast, options = {}) ⇒ TerraformPlan

Create a plan from an abstract syntax tree (AST).



56
57
58
59
# File 'lib/terraform_landscape/terraform_plan.rb', line 56

def initialize(plan_ast, options = {})
  @ast = plan_ast
  @diff_context_lines = options.fetch(:diff_context_lines, DEFAULT_DIFF_CONTEXT_LINES)
end

Class Method Details

.from_output(string) ⇒ Object

Raises:



34
35
36
37
38
39
40
41
42
# File 'lib/terraform_landscape/terraform_plan.rb', line 34

def from_output(string)
  # Our grammar assumes output with Unix line endings
  string = string.gsub("\r\n", "\n")

  return new([]) if string.strip.empty?
  tree = parser.parse(string)
  raise ParseError, parser.failure_reason unless tree
  new(tree.to_ast)
end

Instance Method Details

#display(output) ⇒ Object



61
62
63
64
65
66
67
# File 'lib/terraform_landscape/terraform_plan.rb', line 61

def display(output)
  @out = output
  @ast.each do |resource|
    display_resource(resource)
    @out.newline
  end
end