11# encoding: utf-8
2+
23require 'set'
34require 'json'
45require 'puppet-lint/version'
@@ -48,7 +49,7 @@ class PuppetLint
4849 # Public: Initialise a new PuppetLint object.
4950 def initialize
5051 @code = nil
51- @statistics = { :error => 0 , :warning => 0 , :fixed => 0 , :ignored => 0 }
52+ @statistics = { :error => 0 , :warning => 0 , :fixed => 0 , :ignored => 0 }
5253 @manifest = ''
5354 end
5455
@@ -71,14 +72,16 @@ def configuration
7172 #
7273 # Returns nothing.
7374 def file = ( path )
74- if File . exist? path
75- @path = path
76- @code = File . open ( path , 'r:UTF-8' ) . read
75+ return unless File . exist? ( path )
7776
78- # Check if the input is an SE Linux policy package file (which also use
79- # the .pp extension), which all have the first 4 bytes 0xf97cff8f.
80- @code = '' if @code [ 0 .. 3 ] . unpack ( 'V' ) . first == 0xf97cff8f
77+ @path = path
78+ File . open ( path , 'r:UTF-8' ) do | f |
79+ @code = f . read
8180 end
81+
82+ # Check if the input is an SE Linux policy package file (which also use
83+ # the .pp extension), which all have the first 4 bytes 0xf97cff8f.
84+ @code = '' if @code [ 0 ..3 ] . unpack ( 'V' ) . first == 0xf97cff8f
8285 end
8386
8487 # Internal: Retrieve the format string to be used when writing problems to
@@ -90,12 +93,11 @@ def log_format
9093 if configuration . log_format == ''
9194 ## recreate previous old log format as far as thats possible.
9295 format = '%{KIND}: %{message} on line %{line}'
93- if configuration . with_filename
94- format . prepend '%{path} - '
95- end
96+ format . prepend ( '%{path} - ' ) if configuration . with_filename
9697 configuration . log_format = format
9798 end
98- return configuration . log_format
99+
100+ configuration . log_format
99101 end
100102
101103 # Internal: Format a problem message and print it to STDOUT.
@@ -106,9 +108,8 @@ def log_format
106108 def format_message ( message )
107109 format = log_format
108110 puts format % message
109- if message [ :kind ] == :ignored && !message [ :reason ] . nil?
110- puts " #{ message [ :reason ] } "
111- end
111+
112+ puts " #{ message [ :reason ] } " if message [ :kind ] == :ignored && !message [ :reason ] . nil?
112113 end
113114
114115 # Internal: Get the line of the manifest on which the problem was found
@@ -117,8 +118,7 @@ def format_message(message)
117118 #
118119 # Returns the problematic line as a string.
119120 def get_context ( message )
120- line = PuppetLint ::Data . manifest_lines [ message [ :line ] - 1 ]
121- return line . strip
121+ PuppetLint ::Data . manifest_lines [ message [ :line ] - 1 ] . strip
122122 end
123123
124124 # Internal: Print out the line of the manifest on which the problem was found
@@ -131,9 +131,9 @@ def print_context(message)
131131 return if message [ :check ] == 'documentation'
132132 return if message [ :kind ] == :fixed
133133 line = get_context ( message )
134- offset = line . index ( / \S / ) || 1
134+ offset = line . index ( %r{ \S } ) || 1
135135 puts "\n #{ line . strip } "
136- printf "%#{ message [ :column ] + 2 - offset } s\n \n " , '^'
136+ printf ( "%#{ message [ :column ] + 2 - offset } s\n \n " , '^' )
137137 end
138138
139139 # Internal: Print the reported problems with a manifest to stdout.
@@ -154,16 +154,14 @@ def report(problems)
154154 message [ 'context' ] = get_context ( message ) if configuration . with_context
155155 json << message
156156 else
157- format_message message
157+ format_message ( message )
158158 print_context ( message ) if configuration . with_context
159159 end
160160 end
161161 end
162162 puts JSON . pretty_generate ( json ) if configuration . json
163163
164- if problems . any? { |p | p [ :check ] == :syntax }
165- $stderr. puts "Try running `puppet parser validate <file>`"
166- end
164+ $stderr. puts 'Try running `puppet parser validate <file>`' if problems . any? { |p | p [ :check ] == :syntax }
167165 end
168166
169167 # Public: Determine if PuppetLint found any errors in the manifest.
@@ -186,9 +184,7 @@ def warnings?
186184 # Returns nothing.
187185 # Raises PuppetLint::NoCodeError if no manifest code has been loaded.
188186 def run
189- if @code . nil?
190- raise PuppetLint ::NoCodeError
191- end
187+ raise PuppetLint ::NoCodeError if @code . nil?
192188
193189 if @code . empty?
194190 @problems = [ ]
@@ -207,7 +203,7 @@ def run
207203 #
208204 # Returns nothing.
209205 def print_problems
210- report @problems
206+ report ( @problems )
211207 end
212208
213209 # Public: Define a new check.
0 commit comments