Skip to content
Browse files

Handle missing/bad JSON, write out trailing newline

  • Loading branch information...
1 parent 806a591 commit ca55895a42480d3155d28d8f3275401bc68ce461 @decklin committed May 3, 2012
Showing with 10 additions and 2 deletions.
  1. +10 −2 libraries/json_file.rb
View
12 libraries/json_file.rb
@@ -1,12 +1,20 @@
class Chef::Provider::JsonFile < Chef::Provider::File
+ def load_json(path)
+ JSON.load(::File.read(path)) rescue nil
+ end
+
+ def dump_json(obj)
+ JSON.pretty_generate(obj) + "\n"
+ end
+
def compare_content
- JSON.load(::File.read(@current_resource.path)) == @new_resource.content
+ load_json(@current_resource.path) == @new_resource.content
end
def set_content
unless compare_content
backup @new_resource.path if ::File.exists?(@new_resource.path)
- ::File.open(@new_resource.path, "w") {|f| f.write(JSON.pretty_generate(@new_resource.content)) }
+ ::File.open(@new_resource.path, "w") {|f| f.write dump_json(@new_resource.content) }
Chef::Log.info("#{@new_resource} contents updated")
@new_resource.updated_by_last_action(true)
end

0 comments on commit ca55895

Please sign in to comment.
Something went wrong with that request. Please try again.