class PuppetLint
Public: The public interface to puppet-lint.
Public Class Methods
Public: Access PuppetLint's configuration from outside the class.
Returns a PuppetLint::Configuration object.
# File lib/puppet-lint.rb, line 56 def self.configuration @configuration ||= PuppetLint::Configuration.new end
Public: Initialise a new PuppetLint object.
# File lib/puppet-lint.rb, line 47 def initialize @code = nil @statistics = {:error => 0, :warning => 0, :fixed => 0, :ignored => 0} @manifest = '' end
Public: Define a new check.
name - A unique name for the check as a Symbol. block - The check logic. This must contain a `check` method and optionally
a `fix` method.
Returns nothing.
Examples
PuppetLint.new_check(:foo) do def check end end
# File lib/puppet-lint.rb, line 201 def self.new_check(name, &block) class_name = name.to_s.split('_').map(&:capitalize).join klass = PuppetLint.const_set("Check#{class_name}", Class.new(PuppetLint::CheckPlugin)) klass.const_set('NAME', name) klass.class_exec(&block) PuppetLint.configuration.add_check(name, klass) PuppetLint::Data.ignore_overrides[name] ||= {} end
Public Instance Methods
Public: Access PuppetLint's configuration from inside the class.
Returns a PuppetLint::Configuration object.
# File lib/puppet-lint.rb, line 63 def configuration self.class.configuration end
Public: Determine if PuppetLint found any errors in the manifest.
Returns true if errors were found, otherwise returns false.
# File lib/puppet-lint.rb, line 146 def errors? @statistics[:error] != 0 end
Public: Set the path of the manifest file to be tested and read the contents of the file.
Returns nothing.
# File lib/puppet-lint.rb, line 71 def file=(path) if File.exist? path @path = path @code = File.read(path) end end
Internal: Format a problem message and print it to STDOUT.
message - A Hash containing all the information about a problem.
Returns nothing.
# File lib/puppet-lint.rb, line 100 def format_message(message) format = log_format puts format % message if message[:kind] == :ignored && !message[:reason].nil? puts " #{message[:reason]}" end end
Internal: Retrieve the format string to be used when writing problems to STDOUT. If the user has not specified a custom log format, build one for them.
Returns a format String to be used with String#%.
# File lib/puppet-lint.rb, line 83 def log_format if configuration.log_format == '' ## recreate previous old log format as far as thats possible. format = '%{KIND}: %{message} on line %{line}' if configuration.with_filename format.prepend '%{path} - ' end configuration.log_format = format end return configuration.log_format end
Internal: Print out the line of the manifest on which the problem was found as well as a marker pointing to the location on the line.
message - A Hash containing all the information about a problem.
Returns nothing.
# File lib/puppet-lint.rb, line 114 def print_context(message) return if message[:check] == 'documentation' return if message[:kind] == :fixed line = PuppetLint::Data.manifest_lines[message[:line] - 1] offset = line.index(/\S/) || 1 puts "\n #{line.strip}" printf "%#{message[:column] + 2 - offset}s\n\n", '^' end
Public: Print any problems that were found out to stdout.
Returns nothing.
# File lib/puppet-lint.rb, line 183 def print_problems report @problems end
Internal: Print the reported problems with a manifest to stdout.
problems - An Array of problem Hashes as returned by
PuppetLint::Checks#run.
Returns nothing.
# File lib/puppet-lint.rb, line 129 def report(problems) problems.each do |message| next if message[:kind] == :ignored && !PuppetLint.configuration.show_ignored message[:KIND] = message[:kind].to_s.upcase message[:linenumber] = message[:line] if message[:kind] == :fixed || [message[:kind], :all].include?(configuration.error_level) format_message message print_context(message) if configuration.with_context end end end
Public: Run the loaded manifest code through the lint checks and print the results of the checks to stdout.
Returns nothing. Raises PuppetLint::NoCodeError if no manifest code has been loaded.
# File lib/puppet-lint.rb, line 162 def run if @code.nil? raise PuppetLint::NoCodeError end if @code.empty? @problems = [] @manifest = [] return end linter = PuppetLint::Checks.new @problems = linter.run(@path, @code) @problems.each { |problem| @statistics[problem[:kind]] += 1 } @manifest = linter.manifest if PuppetLint.configuration.fix end
Public: Determine if PuppetLint found any warnings in the manifest.
Returns true if warnings were found, otherwise returns false.
# File lib/puppet-lint.rb, line 153 def warnings? @statistics[:warning] != 0 end