lib/puppet-check/data_parser.rb in puppet-check-2.2.0 vs lib/puppet-check/data_parser.rb in puppet-check-2.2.1

- old
+ new

@@ -1,42 +1,42 @@ require_relative '../puppet_check' # executes diagnostics on data files class DataParser + require 'yaml' + # checks yaml (.yaml/.yml) def self.yaml(files) - require 'yaml' - files.each do |file| # check yaml syntax parsed = YAML.load_file(file) rescue StandardError => err - PuppetCheck.settings[:error_files].push("#{file}:\n#{err.to_s.gsub("(#{file}): ", '')}") + PuppetCheck.files[:errors][file] = err.to_s.gsub("(#{file}): ", '').split("\n") else warnings = [] # perform some rudimentary hiera checks if data exists and is hieradata warnings = hiera(parsed, file) if parsed && (File.basename(file) != 'hiera.yaml') - next PuppetCheck.settings[:warning_files].push("#{file}:\n#{warnings.join("\n")}") unless warnings.empty? - PuppetCheck.settings[:clean_files].push(file.to_s) + next PuppetCheck.files[:warnings][file] = warnings unless warnings.empty? + PuppetCheck.files[:clean].push(file.to_s) end end # checks eyaml (.eyaml/.eyml) def self.eyaml(files, public, private) require 'openssl' # keys specified? if public.nil? || private.nil? - PuppetCheck.settings[:ignored_files].concat(files) + PuppetCheck.files[:ignored].concat(files) return warn 'Public X509 and/or Private RSA PKCS7 certs were not specified. EYAML checks will not be executed.' end # keys exist? unless File.file?(public) && File.file?(private) - PuppetCheck.settings[:ignored_files].concat(files) + PuppetCheck.files[:ignored].concat(files) return warn 'Specified Public X509 and/or Private RSA PKCS7 certs do not exist. EYAML checks will not be executed.' end # setup decryption rsa = OpenSSL::PKey::RSA.new(File.read(private)) @@ -52,33 +52,39 @@ # check yaml syntax begin parsed = YAML.load_file(decrypted) rescue StandardError => err - PuppetCheck.settings[:error_files].push("#{file}:\n#{err.to_s.gsub("(#{file}): ", '')}") + PuppetCheck.files[:errors][file] = err.to_s.gsub("(#{file}): ", '').split("\n") else warnings = [] # perform some rudimentary hiera checks if data exists and is hieradata warnings = hiera(parsed, file) if parsed - next PuppetCheck.settings[:warning_files].push("#{file}:\n#{warnings.join("\n")}") unless warnings.empty? - PuppetCheck.settings[:clean_files].push(file.to_s) + next PuppetCheck.files[:warnings][file] = warnings unless warnings.empty? + PuppetCheck.files[:clean].push(file.to_s) end end end + # metadata consts + REQUIRED_KEYS = %w[name version author license summary source dependencies].freeze + REQ_DEP_KEYS = %w[requirements dependencies].freeze + DEPRECATED_KEYS = %w[types checksum].freeze + TASK_INPUTS = %w[environment stdin powershell].freeze + # checks json (.json) def self.json(files) require 'json' files.each do |file| # check json syntax begin parsed = JSON.parse(File.read(file)) rescue JSON::ParserError => err - PuppetCheck.settings[:error_files].push("#{file}:\n#{err.to_s.lines.first.strip}") + PuppetCheck.files[:errors][file] = err.to_s.lines.first.strip.split("\n") else warnings = [] # check metadata.json if File.basename(file) == 'metadata.json' @@ -87,16 +93,16 @@ # check for errors errors = [] # check for required keys - %w[name version author license summary source dependencies].each do |key| + REQUIRED_KEYS.each do |key| errors.push("Required field '#{key}' not found.") unless parsed.key?(key) end # check requirements and dependencies keys - %w[requirements dependencies].each do |key| + REQ_DEP_KEYS.each do |key| # skip if key is missing or value is an empty string, array, or hash next if !parsed.key?(key) || parsed[key].empty? # check that dependencies and requirements are an array of hashes next errors.push("Field '#{key}' is not an array of hashes.") unless (parsed[key].is_a? Array) && (parsed[key][0].is_a? Hash) @@ -121,18 +127,18 @@ end end end # check for deprecated fields - %w[types checksum].each do |key| + DEPRECATED_KEYS.each do |key| errors.push("Deprecated field '#{key}' found.") if parsed.key?(key) end # check for summary under 144 character errors.push('Summary exceeds 144 characters.') if parsed.key?('summary') && parsed['summary'].size > 144 - next PuppetCheck.settings[:error_files].push("#{file}:\n#{errors.join("\n")}") unless errors.empty? + next PuppetCheck.files[:errors][file] = errors unless errors.empty? # check for warnings # check for operatingsystem_support hash array if parsed.key?('operatingsystem_support') # check if operatingsystem_support array is actually empty @@ -167,11 +173,11 @@ # check that description is a string warnings.push('description value is not a String') unless parsed['description'].is_a?(String) # check that input_method is one of three possible values if parsed.key?('input_method') if parsed['input_method'].is_a?(String) - warnings.push('input_method value is not one of environment, stdin, or powershell') unless %w[environment stdin powershell].include?(parsed['input_method']) + warnings.push('input_method value is not one of environment, stdin, or powershell') unless TASK_INPUTS.include?(parsed['input_method']) else warnings.push('input_method value is not a String') end end # check that parameters is a hash @@ -189,11 +195,11 @@ # assume this is hieradata and ensure it is non-empty elsif parsed # perform some rudimentary hiera checks if data exists warnings = hiera(parsed, file) end - next PuppetCheck.settings[:warning_files].push("#{file}:\n#{warnings.join("\n")}") unless warnings.empty? - PuppetCheck.settings[:clean_files].push(file.to_s) + next PuppetCheck.files[:warnings][file] = warnings unless warnings.empty? + PuppetCheck.files[:clean].push(file.to_s) end end end # checks hieradata