Class: PuppetLint
- Inherits:
-
Object
- Object
- PuppetLint
- Defined in:
- lib/puppet-lint.rb,
lib/puppet-lint/lexer.rb,
lib/puppet-lint/plugins.rb,
lib/puppet-lint/version.rb,
lib/puppet-lint/lexer/token.rb,
lib/puppet-lint/configuration.rb,
lib/puppet-lint/tasks/puppet-lint.rb
Overview
Public: The public interface to puppet-lint.
Defined Under Namespace
Classes: Bin, CheckPlugin, Checks, Configuration, Data, Lexer, LexerError, NoCodeError, NoFix, OptParser, Plugins, RakeTask
Constant Summary collapse
- VERSION =
'2.2.1'
Instance Attribute Summary collapse
-
#code ⇒ Object
Public: Gets/Sets the String manifest code to be checked.
-
#manifest ⇒ Object
readonly
Public: Gets the String manifest with the errors fixed.
-
#path ⇒ Object
Public: Gets/Sets the String path to the manifest to be checked.
-
#problems ⇒ Object
readonly
Public: Returns an Array of Hashes describing the problems found in the manifest.
-
#statistics ⇒ Object
readonly
Public: Returns a Hash of linter statistics.
Class Method Summary collapse
-
.configuration ⇒ Object
Public: Access PuppetLint’s configuration from outside the class.
-
.new_check(name, &block) ⇒ Object
Public: Define a new check.
Instance Method Summary collapse
-
#configuration ⇒ Object
Public: Access PuppetLint’s configuration from inside the class.
-
#errors? ⇒ Boolean
Public: Determine if PuppetLint found any errors in the manifest.
-
#file=(path) ⇒ Object
Public: Set the path of the manifest file to be tested and read the contents of the file.
-
#format_message(message) ⇒ Object
Internal: Format a problem message and print it to STDOUT.
-
#get_context(message) ⇒ Object
Internal: Get the line of the manifest on which the problem was found.
-
#initialize ⇒ PuppetLint
constructor
Public: Initialise a new PuppetLint object.
-
#log_format ⇒ Object
Internal: Retrieve the format string to be used when writing problems to STDOUT.
-
#print_context(message) ⇒ Object
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.
-
#print_problems ⇒ Object
Public: Print any problems that were found out to stdout.
-
#report(problems) ⇒ Object
Internal: Print the reported problems with a manifest to stdout.
-
#run ⇒ Object
Public: Run the loaded manifest code through the lint checks and print the results of the checks to stdout.
-
#warnings? ⇒ Boolean
Public: Determine if PuppetLint found any warnings in the manifest.
Constructor Details
#initialize ⇒ PuppetLint
Public: Initialise a new PuppetLint object.
48 49 50 51 52 |
# File 'lib/puppet-lint.rb', line 48 def initialize @code = nil @statistics = {:error => 0, :warning => 0, :fixed => 0, :ignored => 0} @manifest = '' end |
Instance Attribute Details
#code ⇒ Object
Public: Gets/Sets the String manifest code to be checked.
17 18 19 |
# File 'lib/puppet-lint.rb', line 17 def code @code end |
#manifest ⇒ Object (readonly)
Public: Gets the String manifest with the errors fixed.
20 21 22 |
# File 'lib/puppet-lint.rb', line 20 def manifest @manifest end |
#path ⇒ Object
Public: Gets/Sets the String path to the manifest to be checked.
37 38 39 |
# File 'lib/puppet-lint.rb', line 37 def path @path end |
#problems ⇒ Object (readonly)
Public: Returns an Array of Hashes describing the problems found in the manifest.
Each Hash will contain *at least*:
:check - The Symbol name of the check that generated the problem.
:kind - The Symbol kind of the problem (:error, :warning, or
:fixed).
:line - The Integer line number of the location of the problem in
the manifest.
:column - The Integer column number of the location of the problem in
the manifest.
:message - The String message describing the problem that was found.
34 35 36 |
# File 'lib/puppet-lint.rb', line 34 def problems @problems end |
#statistics ⇒ Object (readonly)
Public: Returns a Hash of linter statistics
:error - An Integer count of errors found in the manifest.
:warning - An Integer count of warnings found in the manifest.
:fixed - An Integer count of problems found in the manifest that were
automatically fixed.
45 46 47 |
# File 'lib/puppet-lint.rb', line 45 def statistics @statistics end |
Class Method Details
.configuration ⇒ Object
Public: Access PuppetLint’s configuration from outside the class.
Returns a PuppetLint::Configuration object.
57 58 59 |
# File 'lib/puppet-lint.rb', line 57 def self.configuration @configuration ||= PuppetLint::Configuration.new end |
.new_check(name, &block) ⇒ Object
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
218 219 220 221 222 223 224 225 |
# File 'lib/puppet-lint.rb', line 218 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 |
Instance Method Details
#configuration ⇒ Object
Public: Access PuppetLint’s configuration from inside the class.
Returns a PuppetLint::Configuration object.
64 65 66 |
# File 'lib/puppet-lint.rb', line 64 def configuration self.class.configuration end |
#errors? ⇒ Boolean
Public: Determine if PuppetLint found any errors in the manifest.
Returns true if errors were found, otherwise returns false.
163 164 165 |
# File 'lib/puppet-lint.rb', line 163 def errors? @statistics[:error] != 0 end |
#file=(path) ⇒ Object
Public: Set the path of the manifest file to be tested and read the contents of the file.
Returns nothing.
72 73 74 75 76 77 |
# File 'lib/puppet-lint.rb', line 72 def file=(path) if File.exist? path @path = path @code = File.read(path) end end |
#format_message(message) ⇒ Object
Internal: Format a problem message and print it to STDOUT.
message - A Hash containing all the information about a problem.
Returns nothing.
101 102 103 104 105 106 107 |
# File 'lib/puppet-lint.rb', line 101 def () format = log_format puts format % if [:kind] == :ignored && ![:reason].nil? puts " #{[:reason]}" end end |
#get_context(message) ⇒ Object
Internal: Get the line of the manifest on which the problem was found
message - A Hash containing all the information about a problem.
Returns the problematic line as a string.
114 115 116 117 |
# File 'lib/puppet-lint.rb', line 114 def get_context() line = PuppetLint::Data.manifest_lines[[:line] - 1] return line.strip end |
#log_format ⇒ Object
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#%.
84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/puppet-lint.rb', line 84 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 |
#print_context(message) ⇒ Object
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.
125 126 127 128 129 130 131 132 |
# File 'lib/puppet-lint.rb', line 125 def print_context() return if [:check] == 'documentation' return if [:kind] == :fixed line = get_context() offset = line.index(/\S/) || 1 puts "\n #{line.strip}" printf "%#{[:column] + 2 - offset}s\n\n", '^' end |
#print_problems ⇒ Object
Public: Print any problems that were found out to stdout.
Returns nothing.
200 201 202 |
# File 'lib/puppet-lint.rb', line 200 def print_problems report @problems end |
#report(problems) ⇒ Object
Internal: Print the reported problems with a manifest to stdout.
problems - An Array of problem Hashes as returned by
PuppetLint::Checks#run.
Returns nothing.
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/puppet-lint.rb', line 140 def report(problems) json = [] problems.each do || next if [:kind] == :ignored && !PuppetLint.configuration.show_ignored [:KIND] = [:kind].to_s.upcase if [:kind] == :fixed || [[:kind], :all].include?(configuration.error_level) if configuration.json ['context'] = get_context() if configuration.with_context json << else print_context() if configuration.with_context end end end puts JSON.pretty_generate(json) if configuration.json end |
#run ⇒ Object
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.
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/puppet-lint.rb', line 179 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 |
#warnings? ⇒ Boolean
Public: Determine if PuppetLint found any warnings in the manifest.
Returns true if warnings were found, otherwise returns false.
170 171 172 |
# File 'lib/puppet-lint.rb', line 170 def warnings? @statistics[:warning] != 0 end |