Class: Moonshot::StackParameterPrinter

Inherits:
Object
  • Object
show all
Defined in:
lib/moonshot/stack_parameter_printer.rb

Overview

Displays information about existing stack parameters to the user, with information on what a stack update would do.

Instance Method Summary collapse

Constructor Details

#initialize(stack, table) ⇒ StackParameterPrinter



5
6
7
8
# File 'lib/moonshot/stack_parameter_printer.rb', line 5

def initialize(stack, table)
  @stack = stack
  @table = table
end

Instance Method Details

#determine_change(default, override, current) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/moonshot/stack_parameter_printer.rb', line 32

def determine_change(default, override, current)
  properties = []

  # If there is a stack override, determine if it would change the current
  # stack.
  if override
    properties << 'overridden'
    if current == '****'
      properties << 'may be updated, NoEcho set'
    elsif override != current
      properties << "would be updated to #{override}"
    end

  else
    # Otherwise, compare the template default with the current value to
    # determine outcome.
    properties << 'default'
    properties << "would be updated to #{default}" if default != current
  end

  properties
end

#format_properties(properties) ⇒ Object



55
56
57
58
59
60
61
62
63
# File 'lib/moonshot/stack_parameter_printer.rb', line 55

def format_properties(properties)
  string = " (#{properties.join(', ')})"

  if properties.any? { |p| p =~ /be updated/ }
    string.yellow
  else
    string.green
  end
end

#format_value(value) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/moonshot/stack_parameter_printer.rb', line 65

def format_value(value)
  if value.size > 40
    value[0..40] + '...'
  else
    value
  end
end


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/moonshot/stack_parameter_printer.rb', line 10

def print
  p_table = @table.add_leaf('Stack Parameters')
  overrides = @stack.overrides
  rows = @stack.parameters.sort.map do |key, value|
    t_param = @stack.template.parameters.find do |p|
      p.name == key
    end

    properties = determine_change(t_param ? t_param.default : nil,
                                  overrides[key],
                                  value)

    [
      "#{key}:",
      format_value(value),
      format_properties(properties)
    ]
  end

  p_table.add_table(rows)
end