module Berkshelf module Formatters class JSON include AbstractFormatter register_formatter :json # Output the version of Berkshelf def version @output = { version: Berkshelf::VERSION } end def initialize @output = { cookbooks: Array.new, errors: Array.new, messages: Array.new } @cookbooks = Hash.new Berkshelf.ui.mute { super } end def cleanup_hook cookbooks.each do |name, details| details[:name] = name output[:cookbooks] << details end puts ::JSON.pretty_generate(output) end # @param [Berkshelf::Dependency] dependency def fetch(dependency) cookbooks[dependency] ||= {} cookbooks[dependency][:version] = dependency.locked_version.to_s cookbooks[dependency][:location] = dependency.location end # Add a Cookbook installation entry to delayed output # # @param [Source] source # the source the dependency is being downloaded from # @param [RemoteCookbook] cookbook # the cookbook to be downloaded def install(source, cookbook) cookbooks[cookbook.name] ||= {} cookbooks[cookbook.name][:version] = cookbook.version unless source.default? cookbooks[cookbook.name][:api_source] = source.uri cookbooks[cookbook.name][:location_path] = cookbook.location_path end end # Add a Cookbook use entry to delayed output # # @param [Dependency] dependency def use(dependency) cookbooks[dependency.name] ||= {} cookbooks[dependency.name][:version] = dependency.cached_cookbook.version if dependency.location.is_a?(PathLocation) cookbooks[dependency.name][:metadata] = true if dependency.location.metadata? cookbooks[dependency.name][:location] = dependency.location.relative_path end end # Add a Cookbook upload entry to delayed output # # @param [Berkshelf::CachedCookbook] cookbook # @param [Ridley::Connection] conn def upload(cookbook, conn) name = cookbook.cookbook_name cookbooks[name] ||= {} cookbooks[name][:version] = cookbook.version cookbooks[name][:uploaded_to] = conn.server_url end # Add a Cookbook skip entry to delayed output # # @param [Berkshelf::CachedCookbook] cookbook # @param [Ridley::Connection] conn def skip(cookbook, conn) name = cookbook.cookbook_name cookbooks[name] ||= {} cookbooks[name][:version] = cookbook.version cookbooks[name][:skipped] = true end # Output a list of outdated cookbooks and the most recent version # to delayed output # # @param [Hash] hash # the list of outdated cookbooks in the format # { 'cookbook' => { 'api.berkshelf.com' => # } } def outdated(hash) hash.keys.each do |name| hash[name].each do |source, cookbook| cookbooks[name] ||= {} cookbooks[name][:version] = cookbook.version cookbooks[name][:sources] ||= {} cookbooks[name][:sources][source] = cookbook end end end # Output a list of cookbooks to delayed output # # @param [Array] dependencies def list(dependencies) dependencies.each do |dependency, cookbook| cookbooks[dependency.name] ||= {} cookbooks[dependency.name][:version] = dependency.locked_version.to_s if dependency.location cookbooks[dependency.name][:location] = dependency.location end end end # Output Cookbook info entry to delayed output # # @param [CachedCookbook] cookbook def show(cookbook) cookbooks[cookbook.cookbook_name] = cookbook.pretty_hash end # Add a vendor message to delayed output # # @param [CachedCookbook] cookbook # @param [String] destination def vendor(cookbook, destination) cookbook_destination = File.join(destination, cookbook.cookbook_name) msg("Vendoring #{cookbook.cookbook_name} (#{cookbook.version}) to #{cookbook_destination}") end # Add a generic message entry to delayed output # # @param [String] message def msg(message) output[:messages] << message end # Add an error message entry to delayed output # # @param [String] message def error(message) output[:errors] << message end # Add a warning message entry to delayed output # # @param [String] message def warn(message) output[:warnings] << message end private attr_reader :output attr_reader :cookbooks end end end