Class: ChefCLI::Policyfile::Differ

Inherits:
Object
  • Object
show all
Defined in:
lib/chef-cli/policyfile/differ.rb

Constant Summary collapse

POLICY_SECTIONS =
%w{ revision_id run_list named_run_lists cookbook_locks default_attributes override_attributes }.freeze
LINES_OF_CONTEXT =
3
INITIAL_FILE_LENGTH_DIFFERENCE =
0
FORMAT =
:unified

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(old_name: nil, old_lock: nil, new_name: nil, new_lock: nil, ui: nil) ⇒ Differ

Returns a new instance of Differ.



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/chef-cli/policyfile/differ.rb', line 38

def initialize(old_name: nil, old_lock: nil, new_name: nil, new_lock: nil, ui: nil)
  @old_lock = old_lock
  @new_lock = new_lock
  @old_name = old_name
  @new_name = new_name
  @ui = ui

  @added_cookbooks    = nil
  @removed_cookbooks  = nil
  @modified_cookbooks = nil
end

Instance Attribute Details

#new_lockObject (readonly)

Returns the value of attribute new_lock.



34
35
36
# File 'lib/chef-cli/policyfile/differ.rb', line 34

def new_lock
  @new_lock
end

#new_nameObject (readonly)

Returns the value of attribute new_name.



35
36
37
# File 'lib/chef-cli/policyfile/differ.rb', line 35

def new_name
  @new_name
end

#old_lockObject (readonly)

Returns the value of attribute old_lock.



32
33
34
# File 'lib/chef-cli/policyfile/differ.rb', line 32

def old_lock
  @old_lock
end

#old_nameObject (readonly)

Returns the value of attribute old_name.



33
34
35
# File 'lib/chef-cli/policyfile/differ.rb', line 33

def old_name
  @old_name
end

#uiObject (readonly)

Returns the value of attribute ui.



36
37
38
# File 'lib/chef-cli/policyfile/differ.rb', line 36

def ui
  @ui
end

Instance Method Details

#added_cookbooksObject



166
167
168
169
# File 'lib/chef-cli/policyfile/differ.rb', line 166

def added_cookbooks
  detect_cookbook_changes if @added_cookbooks.nil?
  @added_cookbooks
end

#different?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/chef-cli/policyfile/differ.rb', line 68

def different?
  !updated_sections.empty?
end

#lock_nameObject



50
51
52
# File 'lib/chef-cli/policyfile/differ.rb', line 50

def lock_name
  old_lock["name"]
end

#modified_cookbooksObject



176
177
178
179
# File 'lib/chef-cli/policyfile/differ.rb', line 176

def modified_cookbooks
  detect_cookbook_changes if @modified_cookbooks.nil?
  @modified_cookbooks
end

#new_cookbook_locksObject



58
59
60
# File 'lib/chef-cli/policyfile/differ.rb', line 58

def new_cookbook_locks
  new_lock["cookbook_locks"]
end

#old_cookbook_locksObject



54
55
56
# File 'lib/chef-cli/policyfile/differ.rb', line 54

def old_cookbook_locks
  old_lock["cookbook_locks"]
end

#removed_cookbooksObject



171
172
173
174
# File 'lib/chef-cli/policyfile/differ.rb', line 171

def removed_cookbooks
  detect_cookbook_changes if @removed_cookbooks.nil?
  @removed_cookbooks
end

#report_added_cookbooksObject



120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/chef-cli/policyfile/differ.rb', line 120

def report_added_cookbooks
  return nil if added_cookbooks.empty?

  h1("ADDED COOKBOOKS")
  added_cookbooks.each do |cb_name|
    ui.print("\n")
    old_lock = []
    new_lock = pretty_json(new_cookbook_locks[cb_name])
    h2(cb_name)
    diff_lines(old_lock, new_lock)
  end
end

#report_default_attribute_changesObject



146
147
148
149
150
151
152
153
154
# File 'lib/chef-cli/policyfile/differ.rb', line 146

def report_default_attribute_changes
  return nil unless updated_sections.include?("default_attributes")

  h1("DEFAULT ATTRIBUTES CHANGED")

  old_default = pretty_json(old_lock["default_attributes"])
  new_default = pretty_json(new_lock["default_attributes"])
  diff_lines(old_default, new_default)
end

#report_modified_cookbooksObject



133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/chef-cli/policyfile/differ.rb', line 133

def report_modified_cookbooks
  return nil if modified_cookbooks.empty?

  h1("MODIFIED COOKBOOKS")
  modified_cookbooks.each do |cb_name|
    ui.print("\n")
    old_lock = pretty_json(old_cookbook_locks[cb_name])
    new_lock = pretty_json(new_cookbook_locks[cb_name])
    h2(cb_name)
    diff_lines(old_lock, new_lock)
  end
end

#report_override_attribute_changesObject



156
157
158
159
160
161
162
163
164
# File 'lib/chef-cli/policyfile/differ.rb', line 156

def report_override_attribute_changes
  return nil unless updated_sections.include?("override_attributes")

  h1("OVERRIDE ATTRIBUTES CHANGED")

  old_override = pretty_json(old_lock["override_attributes"])
  new_override = pretty_json(new_lock["override_attributes"])
  diff_lines(old_override, new_override)
end

#report_removed_cookbooksObject



107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/chef-cli/policyfile/differ.rb', line 107

def report_removed_cookbooks
  return nil if removed_cookbooks.empty?

  h1("REMOVED COOKBOOKS")
  removed_cookbooks.each do |cb_name|
    ui.print("\n")
    old_lock = pretty_json(old_cookbook_locks[cb_name])
    new_lock = []
    h2(cb_name)
    diff_lines(old_lock, new_lock)
  end
end

#report_rev_id_changesObject



89
90
91
92
93
94
# File 'lib/chef-cli/policyfile/differ.rb', line 89

def report_rev_id_changes
  h1("REVISION ID CHANGED")
  old_rev = old_lock["revision_id"]
  new_rev = new_lock["revision_id"]
  diff_lines([ old_rev ], [ new_rev ])
end

#report_run_list_changesObject



96
97
98
99
100
101
102
103
104
105
# File 'lib/chef-cli/policyfile/differ.rb', line 96

def report_run_list_changes
  return nil unless updated_sections.include?("run_list")

  h1("RUN LIST CHANGED")

  old_run_list = old_lock["run_list"]
  new_run_list = new_lock["run_list"]

  diff_lines(old_run_list, new_run_list)
end

#run_reportObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/chef-cli/policyfile/differ.rb', line 72

def run_report
  unless different?
    ui.err("No changes for policy lock '#{lock_name}' between '#{old_name}' and '#{new_name}'")
    return true
  end

  ui.print("Policy lock '#{lock_name}' differs between '#{old_name}' and '#{new_name}':\n\n")

  report_rev_id_changes
  report_run_list_changes
  report_added_cookbooks
  report_removed_cookbooks
  report_modified_cookbooks
  report_default_attribute_changes
  report_override_attribute_changes
end

#updated_sectionsObject



62
63
64
65
66
# File 'lib/chef-cli/policyfile/differ.rb', line 62

def updated_sections
  @updated_sections ||= POLICY_SECTIONS.select do |key|
    old_lock[key] != new_lock[key]
  end
end