Class: TerraformLandscape::Printer

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

Overview

Takes output from Terraform executable and outputs it in a prettified format.

Instance Method Summary collapse

Constructor Details

#initialize(output) ⇒ Printer

Returns a new instance of Printer.



7
8
9
# File 'lib/terraform_landscape/printer.rb', line 7

def initialize(output)
  @output = output
end

Instance Method Details

#process_stream(io, options = {}) ⇒ Object

rubocop:disable Metrics/MethodLength



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/terraform_landscape/printer.rb', line 11

def process_stream(io, options = {}) # rubocop:disable Metrics/MethodLength
  apply = nil
  buffer = StringIO.new
  original_tf_output = StringIO.new
  begin
    block_size = 1024

    done = false
    until done
      readable_fds, = IO.select([io])
      next unless readable_fds

      readable_fds.each do |f|
        begin
          new_output = f.read_nonblock(block_size)
          original_tf_output << new_output
          buffer << strip_ansi(new_output)
        rescue IO::WaitReadable # rubocop:disable Lint/HandleExceptions
          # Ignore; we'll call IO.select again
        rescue EOFError
          done = true
        end
      end

      apply = apply_prompt(buffer.string.encode('UTF-8',
                                                invalid: :replace,
                                                replace: ''))
      done = true if apply
    end

    begin
      process_string(buffer.string)
      @output.print apply if apply
    rescue ParseError, TerraformPlan::ParseError => e
      raise e if options[:trace]

      @output.warning FALLBACK_MESSAGE
      @output.print original_tf_output.string
    end

    @output.write_from(io)
  ensure
    io.close
  end
end

#process_string(plan_output) ⇒ Object

rubocop:disable Metrics/MethodLength



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/terraform_landscape/printer.rb', line 57

def process_string(plan_output) # rubocop:disable Metrics/MethodLength
  scrubbed_output = strip_ansi(plan_output)

  # Our grammar assumes output with Unix line endings
  scrubbed_output.gsub!("\r\n", "\n")

  # Remove initialization messages like
  # "- Downloading plugin for provider "aws" (1.1.0)..."
  # "- module.base_network"
  # as these break the parser which thinks "-" is a resource deletion
  scrubbed_output.gsub!(/^- .*\.\.\.$/, '')
  scrubbed_output.gsub!(/^- module\..*$/, '')

  # Remove separation lines that appear after refreshing state
  scrubbed_output.gsub!(/^-+$/, '')

  if (matches = scrubbed_output.scan(/^Warning:.*$/))
    matches.each do |warning|
      @output.puts warning.colorize(:yellow)
    end
  end

  # Remove preface
  if (match = scrubbed_output.match(/^Path:[^\n]+/))
    scrubbed_output = scrubbed_output[match.end(0)..-1]
  elsif (match = scrubbed_output.match(/^Terraform.+following\sactions:/))
    scrubbed_output = scrubbed_output[match.end(0)..-1]
  elsif (match = scrubbed_output.match(/^\s*(~|\+|\-)/))
    scrubbed_output = scrubbed_output[match.begin(0)..-1]
  elsif scrubbed_output =~ /^(No changes\.|This plan does nothing)/
    @output.puts 'No changes.'
    return
  else
    raise ParseError, 'Output does not contain proper preface'
  end

  # Remove postface
  if (match = scrubbed_output.match(/^Plan:[^\n]+/))
    plan_summary = scrubbed_output[match.begin(0)..match.end(0)]
    scrubbed_output = scrubbed_output[0...match.begin(0)]
  end

  plan = TerraformPlan.from_output(scrubbed_output)
  plan.display(@output)
  @output.puts plan_summary
end