Skip to content

Commit 351389a

Browse files
Tim Hinderliterdanielsdeleo
authored andcommitted
fixed CHEF-1745: 'knife (node|role|client|data bag) edit' never thinks you edited the object, so doesn't save it
1 parent 69bcade commit 351389a

1 file changed

Lines changed: 14 additions & 1 deletion

File tree

‎chef/lib/chef/knife.rb‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,20 @@ def edit_object(klass, name)
418418
object = klass.load(name)
419419

420420
output = edit_data(object)
421-
if output.to_s != object.to_s
421+
422+
# Only make the save if the user changed the object.
423+
#
424+
# Output JSON for the original (object) and edited (output), then parse
425+
# them without reconstituting the objects into real classes
426+
# (create_additions=false). Then, compare the resulting simple objects,
427+
# which will be Array/Hash/String/etc.
428+
#
429+
# We wouldn't have to do these shenanigans if all the editable objects
430+
# implemented to_hash, or if to_json against a hash returned a string
431+
# with stable key order.
432+
object_parsed_again = JSON.parse(object.to_json, :create_additions => false)
433+
output_parsed_again = JSON.parse(output.to_json, :create_additions => false)
434+
if object_parsed_again != output_parsed_again
422435
output.save
423436
self.msg("Saved #{output}")
424437
else

0 commit comments

Comments
 (0)