lib/berkshelf/formatters/json.rb in berkshelf-0.4.0 vs lib/berkshelf/formatters/json.rb in berkshelf-0.5.0.rc1

- old
+ new

@@ -1,53 +1,81 @@ module Berkshelf module Formatters + # @author Michael Ivey <ivey@gweezlebur.com> class JSON include AbstractFormatter - Berkshelf.formatters["json"] = self + register_formatter :json def initialize - @output = {cookbooks: [], errors: [], messages: []} - @cookbooks = {} + @output = { + cookbooks: Array.new, + errors: Array.new, + messages: Array.new + } + @cookbooks = Hash.new super end def cleanup_hook - @cookbooks.each do |name, details| + cookbooks.each do |name, details| details[:name] = name - @output[:cookbooks] << details + output[:cookbooks] << details end - print @output.to_json + + print MultiJson.dump(output) end + # Add a Cookbook installation entry to delayed output + # + # @param [String] cookbook + # @param [String] version + # @param [~Location] location def install(cookbook, version, location) - @cookbooks[cookbook] ||= {} - @cookbooks[cookbook][:version] = version - @cookbooks[cookbook][:location] = location + cookbooks[cookbook] ||= {} + cookbooks[cookbook][:version] = version + cookbooks[cookbook][:location] = location.to_s end - def use(cookbook, version, path=nil) - @cookbooks[cookbook] ||= {} - @cookbooks[cookbook][:version] = version - @cookbooks[cookbook][:location] = path if path + # Add a Cookbook use entry to delayed output + # + # @param [String] cookbook + # @param [String] version + # @param [String] path + def use(cookbook, version, path = nil) + cookbooks[cookbook] ||= {} + cookbooks[cookbook][:version] = version + cookbooks[cookbook][:location] = path if path end - def upload(cookbook, version, chef_server_url) - @cookbooks[cookbook] ||= {} - @cookbooks[cookbook][:version] = version - @cookbooks[cookbook][:uploaded_to] = chef_server_url + # Add a Cookbook upload entry to delayed output + # + # @param [String] cookbook + # @param [String] version + # @param [String] chef_api_url + def upload(cookbook, version, chef_api_url) + cookbooks[cookbook] ||= {} + cookbooks[cookbook][:version] = version + cookbooks[cookbook][:uploaded_to] = chef_api_url end - def shims_written(directory) - @output[:shims_dir] = directory - end - + # Add a generic message entry to delayed output + # + # @param [String] message def msg(message) - @output[:messages] << message + output[:messages] << message end + # Add an error message entry to delayed output + # + # @param [String] message def error(message) - @output[:errors] << message + output[:errors] << message end + + private + + attr_reader :output + attr_reader :cookbooks end end end