Class: Pangea::CLI::TofuEvents::Collector

Inherits:
Object
  • Object
show all
Defined in:
lib/pangea/cli/tofu_events.rb

Overview

Accumulates events and exposes summary information.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCollector

Returns a new instance of Collector.



95
96
97
98
99
100
101
# File 'lib/pangea/cli/tofu_events.rb', line 95

def initialize
  @events = []
  @transient_errors = []
  @dropped_warnings = []
  @plan_summary = nil
  @apply_summary = nil
end

Instance Attribute Details

#apply_summaryObject (readonly)

Returns the value of attribute apply_summary.



91
92
93
# File 'lib/pangea/cli/tofu_events.rb', line 91

def apply_summary
  @apply_summary
end

#dropped_warningsObject (readonly)

Returns the value of attribute dropped_warnings.



91
92
93
# File 'lib/pangea/cli/tofu_events.rb', line 91

def dropped_warnings
  @dropped_warnings
end

#eventsObject (readonly)

Returns the value of attribute events.



91
92
93
# File 'lib/pangea/cli/tofu_events.rb', line 91

def events
  @events
end

#plan_summaryObject (readonly)

Returns the value of attribute plan_summary.



91
92
93
# File 'lib/pangea/cli/tofu_events.rb', line 91

def plan_summary
  @plan_summary
end

#transient_errorsObject (readonly)

Returns the value of attribute transient_errors.



91
92
93
# File 'lib/pangea/cli/tofu_events.rb', line 91

def transient_errors
  @transient_errors
end

Instance Method Details

#any_transient_errors?Boolean

Returns:

  • (Boolean)


121
# File 'lib/pangea/cli/tofu_events.rb', line 121

def any_transient_errors? = !@transient_errors.empty?

#consume(event) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/pangea/cli/tofu_events.rb', line 103

def consume(event)
  @events << event
  @transient_errors << event if event.transient_error?
  @dropped_warnings << event if event.dropped_warning?

  if event.type == 'change_summary'
    changes = event.changes || {}
    # OpenTofu emits `operation` inside the `changes` hash, not at
    # event root. Fall back to top-level for forward compat.
    op = changes['operation'] || event.operation
    if op == 'plan'
      @plan_summary = changes
    elsif op == 'apply'
      @apply_summary = changes
    end
  end
end

#summary_lineObject

Nord-themed one-line summary. Zero counts are muted, non-zero counts use semantic colors (create=green, change=cyan, destroy=red).



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/pangea/cli/tofu_events.rb', line 125

def summary_line
  t = Theme
  if @apply_summary
    a, c, r = @apply_summary.values_at('add', 'change', 'remove').map(&:to_i)
    "#{t.bold(t.color(:heading, 'Apply:'))} " \
      "#{t.color(:create, a)} added, " \
      "#{t.color(:update, c)} changed, " \
      "#{t.color(:delete, r)} destroyed"
  elsif @plan_summary
    a, c, r = @plan_summary.values_at('add', 'change', 'remove').map(&:to_i)
    if (a + c + r).zero?
      t.bold(t.color(:heading, 'Plan: No changes.'))
    else
      "#{t.bold(t.color(:heading, 'Plan:'))} " \
        "#{t.color(:create, a)} to add, " \
        "#{t.color(:update, c)} to change, " \
        "#{t.color(:delete, r)} to destroy"
    end
  end
end