knife-essentials 0.5.2 → 0.5.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -110,7 +110,40 @@ module ChefFS
110
110
  return result
111
111
  end
112
112
 
113
+ def self.sort_keys(json_object)
114
+ if json_object.is_a?(Array)
115
+ json_object.map { |o| sort_keys(o) }
116
+ elsif json_object.is_a?(Hash)
117
+ new_hash = {}
118
+ json_object.keys.sort.each { |key| new_hash[key] = sort_keys(json_object[key]) }
119
+ new_hash
120
+ else
121
+ json_object
122
+ end
123
+ end
124
+
125
+ def self.canonicalize_json(json_text)
126
+ parsed_json = JSON.parse(json_text, :create_additions => false)
127
+ parsed_json.delete('requesting_actor_type')
128
+ parsed_json.delete('requesting_user')
129
+ sorted_json = sort_keys(parsed_json)
130
+ JSON.pretty_generate(sorted_json)
131
+ end
132
+
113
133
  def self.diff_text(old_path, new_path, old_value, new_value)
134
+ # Reformat JSON for a nicer diff.
135
+ if old_path =~ /\.json$/
136
+ begin
137
+ reformatted_old_value = canonicalize_json(old_value)
138
+ reformatted_new_value = canonicalize_json(new_value)
139
+ old_value = reformatted_old_value
140
+ new_value = reformatted_new_value
141
+ rescue
142
+ # If JSON parsing fails, we just won't change any values and fall back
143
+ # to normal diff.
144
+ end
145
+ end
146
+
114
147
  # Copy to tempfiles before diffing
115
148
  # TODO don't copy things that are already in files! Or find an in-memory diff algorithm
116
149
  begin
@@ -1,4 +1,4 @@
1
1
  module ChefFS
2
- VERSION = "0.5.2"
2
+ VERSION = "0.5.3"
3
3
  end
4
4
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: knife-essentials
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.2
4
+ version: 0.5.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-05-11 00:00:00.000000000Z
12
+ date: 2012-05-17 00:00:00.000000000Z
13
13
  dependencies: []
14
14
  description: Universal knife verbs that work with your Chef repository
15
15
  email: jkeiser@opscode.com