lib/puppet-check/output_results.rb in puppet-check-2.2.0 vs lib/puppet-check/output_results.rb in puppet-check-2.2.1
- old
+ new
@@ -1,45 +1,51 @@
require_relative '../puppet_check'
# class to handle outputting diagnostic results in desired format
class OutputResults
- # output the results as text
- def self.text
- unless PuppetCheck.settings[:error_files].empty?
- print "\033[31mThe following files have errors:\033[0m\n-- "
- puts PuppetCheck.settings[:error_files].join("\n\n-- ")
- end
- unless PuppetCheck.settings[:warning_files].empty?
- print "\n\033[33mThe following files have warnings:\033[0m\n-- "
- puts PuppetCheck.settings[:warning_files].join("\n\n-- ")
- end
- unless PuppetCheck.settings[:clean_files].empty?
- print "\n\033[32mThe following files have no errors or warnings:\033[0m\n-- "
- puts PuppetCheck.settings[:clean_files].join("\n-- ")
- end
- return if PuppetCheck.settings[:ignored_files].empty?
- print "\n\033[36mThe following files have unrecognized formats and therefore were not processed:\033[0m\n-- "
- puts PuppetCheck.settings[:ignored_files].join("\n-- ")
- end
+ # output the results in various formats
+ def self.run(files, format)
+ # remove empty entries
+ files.delete_if { |_, sorted_files| sorted_files.empty? }
- # output the results as yaml or json
- def self.markup(format)
- # generate output hash
- hash = {}
- hash['errors'] = PuppetCheck.settings[:error_files] unless PuppetCheck.settings[:error_files].empty?
- hash['warnings'] = PuppetCheck.settings[:warning_files] unless PuppetCheck.settings[:warning_files].empty?
- hash['clean'] = PuppetCheck.settings[:clean_files] unless PuppetCheck.settings[:clean_files].empty?
- hash['ignored'] = PuppetCheck.settings[:ignored_files] unless PuppetCheck.settings[:ignored_files].empty?
-
- # convert hash to markup language
+ # output hash according to specified format
case format
+ when 'text'
+ text(files)
when 'yaml'
require 'yaml'
- puts Psych.dump(hash, indentation: 2)
+ # maintain filename format consistency among output formats
+ files.transform_keys!(&:to_s)
+ puts Psych.dump(files, indentation: 2)
when 'json'
require 'json'
- puts JSON.pretty_generate(hash)
+ puts JSON.pretty_generate(files)
else
raise "puppet-check: Unsupported output format '#{format}' was specified."
end
+ end
+
+ # output the results as text
+ def self.text(files)
+ private_class_method :method
+
+ # errors
+ if files.key?(:errors)
+ puts "\033[31mThe following files have errors:\033[0m"
+ files[:errors].each { |file, errors| puts "-- #{file}:\n#{errors.join("\n")}" }
+ end
+ # warnings
+ if files.key?(:warnings)
+ puts "\n\033[33mThe following files have warnings:\033[0m"
+ files[:warnings].each { |file, warnings| puts "-- #{file}:\n#{warnings.join("\n")}" }
+ end
+ # cleans
+ if files.key?(:clean)
+ print "\n\033[32mThe following files have no errors or warnings:\033[0m\n-- "
+ puts files[:clean].join("\n-- ")
+ end
+ # ignores
+ return unless files.key?(:ignored)
+ print "\n\033[36mThe following files have unrecognized formats and therefore were not processed:\033[0m\n-- "
+ puts files[:ignored].join("\n-- ")
end
end