class CuffSert::ProgressbarRenderer

Public Instance Methods

abort(event) click to toggle source
# File lib/cuffsert/presenters.rb, line 326
def abort(event)
  @error.write("\n" + event.message.colorize(:red) + "\n") unless @verbosity < 1
end
action(rc) click to toggle source
# File lib/cuffsert/presenters.rb, line 219
def action(rc)
  if rc[:action] == 'Modify'
    if ['True', 'Always'].include?(rc[:replacement])
      'Replace!'
    elsif ['False', 'Never'].include?(rc[:replacement])
      'Modify'
    elsif rc[:replacement] == 'Conditional'
      'Replace?'
    else
      "#{rc[:action]}/#{rc[:replacement]}"
    end
  else
    rc[:action]
  end
end
action_color(action) click to toggle source
# File lib/cuffsert/presenters.rb, line 235
def action_color(action)
  action.colorize(
    case action
    when 'Add' then :green
    when 'Modify' then :yellow
    else :red
    end
  )
end
change_set(change_set) click to toggle source
# File lib/cuffsert/presenters.rb, line 195
def change_set(change_set)
  @output.write(sprintf("Updating stack %s\n", change_set[:stack_name]))
  change_set[:changes].sort do |l, r|
    lr = l[:resource_change]
    rr = r[:resource_change]
    [
      ACTION_ORDER.index(action(lr)),
      lr[:logical_resource_id]
    ] <=> [
      ACTION_ORDER.index(action(rr)),
      rr[:logical_resource_id]
    ]
  end.map do |change|
    rc = change[:resource_change]
    sprintf("%s[%s] %-10s %s\n%s",
      rc[:logical_resource_id],
      rc[:resource_type],
      action_color(action(rc)),
      scope_desc(rc),
      change_details(rc)
    )
  end.each { |row| @output.write(row) }
end
clear() click to toggle source
# File lib/cuffsert/presenters.rb, line 291
def clear
  @output.write("\r") unless @verbosity < 1
end
done(event) click to toggle source
# File lib/cuffsert/presenters.rb, line 330
def done(event)
  @output.write(event.message.colorize(:green) + "\n") unless @verbosity < 1
end
event(event, resource) click to toggle source
# File lib/cuffsert/presenters.rb, line 262
def event(event, resource)
  return if @verbosity == 0
  return if resource[:states][-1] != :bad && @verbosity <= 1
  color, _ = interpret_states(resource)
  message = sprintf('%s %s  %s[%s] %s',
    event[:resource_status],
    event[:timestamp].strftime('%H:%M:%S%z'),
    event[:logical_resource_id],
    event[:resource_type].sub(/.*::/, ''),
    event[:resource_status_reason] || ""
  ).colorize(color)
  @output.write("\r#{message}\n")
end
report(event) click to toggle source
# File lib/cuffsert/presenters.rb, line 322
def report(event)
  @output.write(event.message.colorize(:white) + "\n") unless @verbosity < 2
end
resource(resource) click to toggle source
# File lib/cuffsert/presenters.rb, line 295
def resource(resource)
  return if @verbosity < 1
  color, symbol = interpret_states(resource)
  table = {
    :check => "+",
    :tripple_dot => ".", # "\u2026"
    :cross  => "!",
    :qmark => "?",
  }

  @output.write(table[symbol].colorize(
    :color => :white,
    :background => color
  ))
end
scope_desc(rc) click to toggle source
# File lib/cuffsert/presenters.rb, line 245
def scope_desc(rc)
  (rc[:scope] || []).map do |scope|
    case scope
    when 'Properties'
      properties = rc[:details]
        .select { |detail| detail[:target][:attribute] == 'Properties' }
        .map { |detail| detail[:target][:name] }
        .uniq
        .join(", ")
      sprintf("Properties: %s", properties)
    else
      rc[:scope]
    end
  end
  .join("; ")
end
stack(event, stack) click to toggle source
# File lib/cuffsert/presenters.rb, line 276
def stack(event, stack)
  case event
  when :create
    @output.write("Creating stack #{stack}\n")
  when :recreate
    message = sprintf(
      "Deleting and re-creating stack %s",
      stack[:stack_name]
    )
    @output.write(message.colorize(:red) + "\n")
  else
    puts event, stack
  end
end
templates(current, pending) click to toggle source
# File lib/cuffsert/presenters.rb, line 311
def templates(current, pending)
  @current_template = current
  @pending_template = pending
  @template_changes = Hashdiff.best_diff(current, pending, array_path: true)
  @template_changes.each {|c| p c} if ENV['CUFFSERT_EXPERIMENTAL']
  present_changes(extract_changes(@template_changes, 'Conditions'), 'Conditions') unless @verbosity < 1
  present_changes(extract_changes(@template_changes, 'Parameters'), 'Parameters') unless @verbosity < 1
  present_changes(extract_changes(@template_changes, 'Mappings'), 'Mappings') unless @verbosity < 1
  present_changes(extract_changes(@template_changes, 'Outputs'), 'Outputs') unless @verbosity < 1
end

Private Instance Methods

change_color(ch) click to toggle source
# File lib/cuffsert/presenters.rb, line 376
def change_color(ch)
  ch.colorize(
    case ch
    when '-' then :red
    when '+' then :green
    when '~' then :yellow
    else :white
    end
  )
end
change_details(rc) click to toggle source
# File lib/cuffsert/presenters.rb, line 336
def change_details(rc)
  (rc[:details] || []).flat_map do |detail|
    target_path = case detail[:target][:attribute]
    when 'Properties'
      [rc[:logical_resource_id], detail[:target][:attribute], detail[:target][:name]]
    when 'Tags'
      [rc[:logical_resource_id], 'Properties', detail[:target][:attribute]]
    else
      nil
    end
    extract_changes(@template_changes, 'Resources', *target_path)
  end
  .map do |(ch, path, l, r)|
    format_change(ch, path[3..-1], l, r)
  end
  .join
end
extract_changes(changes, type, *target_path) click to toggle source
# File lib/cuffsert/presenters.rb, line 354
def extract_changes(changes, type, *target_path)
  changes
    .select {|(_, path, _)| path[0..target_path.size] == [type, *target_path] }
    .map {|(ch, path, *rest)| [ch, path, *rest] }
end
format_change(ch, path, l, r = nil) click to toggle source
# File lib/cuffsert/presenters.rb, line 368
def format_change(ch, path, l, r = nil)
  sprintf("%s %s: %s\n",
    change_color(ch),
    path.join('/'),
    ch == '~' ? "#{l} -> #{r}" : l,
  )
end
interpret_states(resource) click to toggle source
# File lib/cuffsert/presenters.rb, line 387
def interpret_states(resource)
  case resource[:states]
  when [:progress]
    [:yellow, :tripple_dot]
  when [:good]
    [:green, :check]
  when [:bad]
    [:red, :cross]
  when [:good, :progress]
    [:light_white, :tripple_dot]
  when [:bad, :progress]
    [:red, :tripple_dot]
  when [:good, :good], [:bad, :good]
    [:light_white, :check]
  when [:good, :bad], [:bad, :bad]
    [:red, :qmark]
  else
    raise "Unexpected :states in #{resource.inspect}"
  end
end
present_changes(changes, type) click to toggle source
# File lib/cuffsert/presenters.rb, line 360
def present_changes(changes, type)
  return unless changes.size > 0
  @output.write("#{type}:\n")
  changes.each do |(ch, path, l, r)|
    @output.write(format_change(ch, path, l, r))
  end
end