Class: PuppetLint

Inherits:
Object
  • Object
show all
Defined in:
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,
lib/puppet-lint.rb

Defined Under Namespace

Classes: Bin, CheckPlugin, Checks, Configuration, Lexer, LexerError, NoCodeError, Plugins, RakeTask

Constant Summary collapse

VERSION =
'0.3.2'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePuppetLint

Returns a new instance of PuppetLint.



73
74
75
76
77
# File 'lib/puppet-lint.rb', line 73

def initialize
  @data = nil
  @statistics = {:error => 0, :warning => 0}
  @fileinfo = {:path => ''}
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



71
72
73
# File 'lib/puppet-lint.rb', line 71

def data
  @data
end

Class Method Details

.configurationObject



79
80
81
# File 'lib/puppet-lint.rb', line 79

def self.configuration
  @configuration ||= PuppetLint::Configuration.new
end

Instance Method Details

#code=(value) ⇒ Object



96
97
98
# File 'lib/puppet-lint.rb', line 96

def code=(value)
  @data = value
end

#configurationObject



83
84
85
# File 'lib/puppet-lint.rb', line 83

def configuration
  self.class.configuration
end

#errors?Boolean

Returns:

  • (Boolean)


141
142
143
# File 'lib/puppet-lint.rb', line 141

def errors?
  @statistics[:error] != 0
end

#file=(path) ⇒ Object



87
88
89
90
91
92
93
94
# File 'lib/puppet-lint.rb', line 87

def file=(path)
  if File.exist? path
    @fileinfo[:path] = path
    @fileinfo[:fullpath] = File.expand_path(path)
    @fileinfo[:filename] = File.basename(path)
    @data = File.read(path)
  end
end

#format_message(message) ⇒ Object



112
113
114
115
# File 'lib/puppet-lint.rb', line 112

def format_message(message)
  format = log_format
  puts format % message
end

#log_formatObject



100
101
102
103
104
105
106
107
108
109
110
# File 'lib/puppet-lint.rb', line 100

def log_format
  if configuration.log_format == ''
    ## recreate previous old log format as far as thats possible.
    format = '%{KIND}: %{message} on line %{linenumber}'
    if configuration.with_filename
      format.prepend '%{path} - '
    end
    configuration.log_format = format
  end
  return configuration.log_format
end


117
118
119
120
121
122
123
124
125
# File 'lib/puppet-lint.rb', line 117

def print_context(message, linter)
  # XXX: I don't really like the way this has been implemented (passing the
  # linter object down through layers of functions.  Refactor me!
  return if message[:check] == 'documentation'
  line = linter.manifest_lines[message[:linenumber] - 1]
  offset = line.index(/\S/)
  puts "\n  #{line.strip}"
  printf "%#{message[:column] + 2 - offset}s\n\n", '^'
end

#report(problems, linter) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/puppet-lint.rb', line 127

def report(problems, linter)
  problems.each do |message|
    @statistics[message[:kind]] += 1
    ## Add some default attributes.
    message.merge!(@fileinfo) {|key, v1, v2| v1 }
    message[:KIND] = message[:kind].to_s.upcase

    if configuration.error_level == message[:kind] or configuration.error_level == :all
      format_message message
      print_context(message, linter) if configuration.with_context
    end
  end
end

#runObject



149
150
151
152
153
154
155
156
157
# File 'lib/puppet-lint.rb', line 149

def run
  if @data.nil?
    raise PuppetLint::NoCodeError
  end

  linter = PuppetLint::Checks.new
  problems = linter.run(@fileinfo, @data)
  report problems, linter
end

#warnings?Boolean

Returns:

  • (Boolean)


145
146
147
# File 'lib/puppet-lint.rb', line 145

def warnings?
  @statistics[:warning] != 0
end