Class: SimplyGenius::Atmos::Plugins::PlanSummary

Inherits:
OutputFilter
  • Object
show all
Defined in:
lib/simplygenius/atmos/plugins/plan_summary.rb

Instance Attribute Summary

Attributes inherited from OutputFilter

#context

Instance Method Summary collapse

Methods inherited from OutputFilter

#close

Methods included from UI

#agree, #ask, #choose, color_enabled, color_enabled=, #display, #error, #notify, #say, #warn

Constructor Details

#initialize(context) ⇒ PlanSummary

Returns a new instance of PlanSummary.



10
11
12
13
14
# File 'lib/simplygenius/atmos/plugins/plan_summary.rb', line 10

def initialize(context)
  super
  @plan_detected = false
  @summary_data = ""
end

Instance Method Details

#filter(data, flushing: false) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/simplygenius/atmos/plugins/plan_summary.rb', line 16

def filter(data, flushing: false)
  summary_saved = false
  if data =~ /^[\e\[\dm\s]*Terraform will perform the following actions:[\e\[\dm\s]*$/
    @plan_detected = true
    @summary_data = data.sub(/.*Terraform will perform the following actions:[^\n]*\n/m, "")
    summary_saved = true
  end

  if @plan_detected
    @summary_data << data unless summary_saved
    data.sub!(/^[\e\[\dm\s]*Plan:.*$/) do |m|
      summary = summarize(@summary_data)
      m + "\n\n#{summary}\n"
    end
  end

  data
end

#summarize(data) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/simplygenius/atmos/plugins/plan_summary.rb', line 35

def summarize(data)
  # Looking for +/-/~ at start within 2 spaces, could also look for lines that end with {
  lines = data.lines.select { |l|
    l = l.gsub(/\e\[\d+m/, '')
    l =~ /^\s{0,2}[~+\-<]/
  }.collect(&:chomp)
  lines = lines.reject {|l| l =~ /-----/ }
  "Plan Summary:\n#{lines.join("\n")}"
end