Class: HeadChef::CookbookDiff

Inherits:
Object
  • Object
show all
Defined in:
lib/head_chef/cookbook_diff.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCookbookDiff

Returns a new instance of CookbookDiff.



5
6
7
8
9
10
11
# File 'lib/head_chef/cookbook_diff.rb', line 5

def initialize
  @diff_hash = { add: [],
                 update: [],
                 remove: [],
                 revert: [],
                 conflict: [] }
end

Instance Attribute Details

#diff_hashObject (readonly)

Returns the value of attribute diff_hash.



3
4
5
# File 'lib/head_chef/cookbook_diff.rb', line 3

def diff_hash
  @diff_hash
end

Instance Method Details

#add(cookbook) ⇒ Object

@TODO: cleanup @TODO: switch statements



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/head_chef/cookbook_diff.rb', line 15

def add(cookbook)
  # Removal is the only operation that does not require a diff, as no
  # cookbook will be uploaded
  if cookbook.chef_version && !cookbook.berkshelf_version
    @diff_hash[:remove] << cookbook and return
  end

  unless cookbook.diff
    @diff_hash[:conflict] << cookbook and return
  end

  if cookbook.berkshelf_version && !cookbook.chef_version
    @diff_hash[:add] << cookbook and return
  end

  berkshelf_version = Semantic::Version.new(cookbook.berkshelf_version)
  chef_version = Semantic::Version.new(cookbook.chef_version)

  if berkshelf_version > chef_version
    @diff_hash[:update] << cookbook and return
  elsif berkshelf_version < chef_version
    @diff_hash[:revert] << cookbook and return
  end
end

#conflictsObject



40
41
42
# File 'lib/head_chef/cookbook_diff.rb', line 40

def conflicts
  @diff_hash[:conflict]
end

#conflicts?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/head_chef/cookbook_diff.rb', line 44

def conflicts?
  !@diff_hash[:conflict].empty?
end

#empty?Boolean

Returns:

  • (Boolean)


48
49
50
51
52
53
54
# File 'lib/head_chef/cookbook_diff.rb', line 48

def empty?
  [:add, :update, :remove, :revert, :conflict].each do |method|
    return false if !@diff_hash[method].empty?
  end

  true
end

#pretty_printObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/head_chef/cookbook_diff.rb', line 56

def pretty_print
  if self.empty?
    HeadChef.ui.say("Berksfile and Chef environment are identical", :green)
    return
  end

  colors = { add: :green,
             update: :green,
             remove: :red,
             revert: :red,
             conflict: :red }

  [:add, :update, :remove, :revert, :conflict].each do |method|
    color = colors[method]

    unless @diff_hash[method].empty?
      HeadChef.ui.say("#{method.to_s.upcase}:", color)
      diff_hash[method].each do |cookbook|
        HeadChef.ui.say("  #{cookbook.to_s}", color)
      end
    end
  end
end