Class: Lambchop::Diff

Inherits:
Object
  • Object
show all
Defined in:
lib/lambchop/diff.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(function_name, file, options = {}) ⇒ Diff

Returns a new instance of Diff.



6
7
8
9
10
11
12
# File 'lib/lambchop/diff.rb', line 6

def initialize(function_name, file, options = {})
  @function_name = function_name
  @file          = file
  @client        = options[:client] || Aws::Lambda::Client.new
  @out           = options[:out] || $stdout
  @options       = options
end

Class Method Details

.diff(function_name, file, options = {}) ⇒ Object



2
3
4
# File 'lib/lambchop/diff.rb', line 2

def self.diff(function_name, file, options = {})
  self.new(function_name, file, options).diff
end

Instance Method Details

#diffObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/lambchop/diff.rb', line 14

def diff
  file = @file

  if file.kind_of?(IO)
    file = file.read
  end

  file = Lambchop::Utils.remove_shebang(file)
  config, file = Lambchop::Utils.parse_magic_comment(file)

  page = @client.get_function(:function_name => @function_name)

  Lambchop::Utils.open_source(page.code.location) do |name, func|
    diff = Diffy::Diff.new(func, file, :include_diff_info => true).to_s

    unless diff.empty?
      @out.puts("--- #{@function_name}:#{name}")
      @out.puts("+++ #{@file.path}")
      @out.puts(diff.each_line.to_a.slice(2..-1).join)
    end
  end
end