lib/chef/taste/display.rb in chef-taste-1.0.0 vs lib/chef/taste/display.rb in chef-taste-1.1.0
- old
+ new
@@ -29,53 +29,101 @@
module Taste
# Displays the status of the dependency cookbooks
#
class Display
class << self
+ # Prints the status of dependent cookbooks in specified format
+ #
+ # @param dependencies [Array<Dependency>] list of cookbook dependency objects
+ # @param format [String] the format used for display
+ #
+ def print(dependencies, format)
+ case format
+ when 'table'
+ TableDisplay.print(dependencies)
+ when'json'
+ JSONDisplay.print(dependencies)
+ else
+ raise UnsupportedDisplayFormatError, "Display format '#{format}' is not supported"
+ end
+ end
+ end
+ end
+
+ # Displays the cookbook dependency status in a table format
+ #
+ class TableDisplay
+ class << self
# Prints the status of dependent cookbooks as a table
#
# @param dependencies [Array<Dependency>] list of cookbook dependency objects
#
def print(dependencies)
- if dependencies.empty?
- puts "No dependent cookbooks"
- else
- rows = []
- headings = [
- 'Name',
- 'Requirement',
- 'Used',
- 'Latest',
- 'Status',
- 'Changelog'
+ rows = []
+ headings = %w(Name Requirement Used Latest Status Changelog)
+ dependencies.each do |dependency|
+ status_symbol, color = status_to_symbol_and_color(dependency.status)
+ rows << [
+ dependency.name,
+ dependency.requirement,
+ dependency.version_used,
+ dependency.latest,
+ { value: status_symbol.send(color), alignment: :center },
+ dependency.changelog
]
- dependencies.each do |dependency|
- color =
- if dependency.status == TICK_MARK
- 'green'
- elsif dependency.status == X_MARK
- 'red'
- else
- 'white'
- end
- rows << [
- dependency.name,
- dependency.requirement,
- dependency.version_used,
- dependency.latest,
- {:value => dependency.status.send(color), :alignment => :center},
- dependency.changelog
- ]
- end
+ end
- # If any of the cookbook is out-of-date
- out_of_date = dependencies.any? { |dep| dep.status == X_MARK }
- table = Terminal::Table.new headings: headings, rows: rows
- puts table
- if out_of_date
- puts "Status: out-of-date ( #{X_MARK} )".red
- else
- puts "Status: up-to-date ( #{TICK_MARK} )".green
+ # If any of the cookbook is out-of-date
+ table = Terminal::Table.new headings: headings, rows: rows
+ puts table
+ if dependencies.any? { |dep| dep.status == 'out-of-date' }
+ puts "Status: out-of-date ( #{X_MARK} )".red
+ else
+ puts "Status: up-to-date ( #{TICK_MARK} )".green
+ end
+ end
+
+ # Given the status of the cookbook, this method will convert it to the unicode symbol
+ # and color. The up-to-date cookbook will receive a green color TICK mark whereas
+ # the out-of-date cookbook will receive a red color 'X' mark.
+ #
+ # @param status [String] the status of the cookbook
+ #
+ # @return [String, String] status symbol and color
+ #
+ def status_to_symbol_and_color(status)
+ case status
+ when 'up-to-date'
+ return TICK_MARK, 'green'
+ when'out-of-date'
+ return X_MARK, 'red'
+ else
+ return '', 'white'
+ end
+ end
+ end
+ end
+
+ # Displays the cookbook dependency status in JSON format
+ #
+ class JSONDisplay
+ class << self
+ # Prints the status of dependent in JSON
+ #
+ # @param dependencies [Array<Dependency>] list of cookbook dependency objects
+ #
+ def print(dependencies)
+ puts JSON.pretty_generate(dependencies_hash(dependencies))
+ end
+
+ # Converts the dependency objects to JSON object
+ #
+ # @param dependencies [Array<Dependency>] list of cookbook dependency objects
+ #
+ def dependencies_hash(dependencies)
+ {}.tap do |hash|
+ dependencies.each do |dependency|
+ hash[dependency.name] = dependency.to_hash
end
end
end
end
end