Sha256: 671c9e9bb8b60084862b4a71a4edddf188780b148b7588b5db52d67cbf01d24e

Contents?: true

Size: 1.44 KB

Versions: 28

Compression:

Stored size: 1.44 KB

Contents

require 'hashdiff'
require 'opsicle/output'

module Opsicle
  class Update
    attr_reader :client, :type

    def initialize(environment, type)
      @client = Client.new(environment)
      @type = type
    end

    def execute(values, options)
      before = describe
      update(values)
      after = describe
      print(before, after)
    end

    def describe
      api_method = "describe_#{@type}s"
      api_opts = {
          :"#{@type}_ids" => [client.config.opsworks_config[:"#{@type}_id"]]
      }
      client.api_call(api_method, api_opts)[:"#{@type}s"][0]
    end

    def update(values)
      api_method = "update_#{@type}"
      api_opts = values.merge(:"#{@type}_id" => client.config.opsworks_config[:"#{@type}_id"])
      client.api_call(api_method, api_opts)
    end

    def print(before, after)
      diff = HashDiff.diff(before, after)
      Output.say("Changes: #{diff.size}")
      Output.terminal.say(Terminal::Table.new headings: %w[Change Key Before After], rows: format_diff(diff)) if diff.size > 0
    end

    def format_diff(diff)
      diff.map { |change|
        case change[0]
          when '-'
            change.insert(3, nil)
            change.map! { |i| Output.format(i, :removal) }
          when '+'
            change.insert(2, nil)
            change.map! { |i| Output.format(i, :addition) }
          when '~'
            change.map! { |i| Output.format(i, :modification) }
        end
        change
      }
    end

  end
end

Version data entries

28 entries across 28 versions & 1 rubygems

Version Path
opsicle-1.0.1 lib/opsicle/commands/update.rb
opsicle-0.18.1 lib/opsicle/commands/update.rb
opsicle-0.18.0 lib/opsicle/commands/update.rb
opsicle-0.16.0 lib/opsicle/commands/update.rb
opsicle-0.15.0 lib/opsicle/commands/update.rb
opsicle-0.14.0 lib/opsicle/commands/update.rb
opsicle-0.13.1 lib/opsicle/commands/update.rb
opsicle-0.13.0 lib/opsicle/commands/update.rb