puppet-lint 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/bin/puppet-lint CHANGED
@@ -41,7 +41,13 @@ if ARGV[0].nil?
41
41
  exit
42
42
  end
43
43
 
44
- l = PuppetLint.new
45
- l.file = ARGV[0]
46
- l.run
44
+ begin
45
+ l = PuppetLint.new
46
+ l.file = ARGV[0]
47
+ l.run
48
+ rescue PuppetLint::NoCodeError
49
+ puts "puppet-lint: no file specified or specified file does not exist"
50
+ puts "puppet-lint: try 'puppet-lint --help' for more information"
51
+ exit
52
+ end
47
53
 
data/lib/puppet-lint.rb CHANGED
@@ -1,8 +1,11 @@
1
1
  require 'puppet-lint/plugin'
2
2
  require 'puppet-lint/plugins'
3
+ require 'puppet'
4
+
5
+ class PuppetLint::NoCodeError < StandardError; end
3
6
 
4
7
  class PuppetLint
5
- VERSION = '0.0.3'
8
+ VERSION = '0.0.4'
6
9
 
7
10
  attr_reader :code, :file
8
11
 
@@ -41,6 +44,10 @@ class PuppetLint
41
44
  end
42
45
 
43
46
  def run
47
+ if @data.nil?
48
+ raise PuppetLint::NoCodeError
49
+ end
50
+
44
51
  PuppetLint::CheckPlugin.repository.each do |plugin|
45
52
  problems = plugin.new.run(@data)
46
53
  problems[:errors].each { |error| report :errors, error }
@@ -48,3 +55,4 @@ class PuppetLint
48
55
  end
49
56
  end
50
57
  end
58
+
@@ -18,6 +18,7 @@ end
18
18
 
19
19
  class PuppetLint::CheckPlugin
20
20
  include PuppetLint::Plugin
21
+ attr_reader :warnings, :errors
21
22
 
22
23
  def initialize
23
24
  @warnings = []
@@ -3,50 +3,58 @@
3
3
 
4
4
  class PuppetLint::Plugins::CheckResources < PuppetLint::CheckPlugin
5
5
  def test(data)
6
- line_no = 0
7
- in_resource = true
8
- first_attribute = false
9
- data.split("\n").each do |line|
10
- line_no += 1
6
+ lexer = Puppet::Parser::Lexer.new
7
+ lexer.string = data
8
+ tokens = lexer.fullscan
11
9
 
12
- if line.include? "{"
13
- in_resource = true
14
- first_attribute = true
15
- line = line.slice(line.index('{')..-1)
10
+ title_tokens = []
11
+ resource_indexes = []
12
+ tokens.each_index do |token_idx|
13
+ if tokens[token_idx].first == :COLON
14
+ # gather a list of tokens that are resource titles
15
+ if tokens[token_idx-1].first == :RBRACK
16
+ title_array_tokens = tokens[tokens.rindex { |r| r.first == :LBRACK }+1..token_idx-2]
17
+ title_tokens += title_array_tokens.select { |token| [:STRING, :NAME].include? token.first }
18
+ else
19
+ title_tokens << tokens[token_idx-1]
20
+ end
21
+
22
+ # gather a list of start and end indexes for resource attribute blocks
23
+ resource_indexes << {:start => token_idx+1, :end => tokens[token_idx+1..-1].index { |r| [:SEMIC, :RBRACE].include? r.first }+token_idx}
16
24
  end
25
+ end
17
26
 
18
- if in_resource
19
- # Resource titles SHOULD be quoted
20
- line.scan(/[^'"]\s*:/) do |match|
21
- unless line =~ /\$[\w:]+\s*:/
22
- warn "unquoted resource title on line #{line_no}"
23
- end
27
+ title_tokens.each do |token|
28
+ if token.first == :NAME
29
+ warn "unquoted resource title on line #{token.last[:line]}"
30
+ end
31
+ end
32
+
33
+ resource_indexes.each do |resource|
34
+ resource_tokens = tokens[resource[:start]..resource[:end]]
35
+ ensure_attr_index = resource_tokens.index { |token| token.first == :NAME and token.last[:value] == 'ensure' }
36
+ unless ensure_attr_index.nil?
37
+ if ensure_attr_index > 1
38
+ ensure_attr_line_no = resource_tokens[ensure_attr_index].last[:line]
39
+ warn "ensure found on line #{ensure_attr_line_no} but it's not the first attribute"
24
40
  end
41
+ end
25
42
 
26
- line.scan(/(\w+)\s*=>\s*([^\n,;]+)/) do |attr, value|
27
- # Ensure SHOULD be the first attribute listed
28
- if attr == 'ensure'
29
- unless first_attribute
30
- warn "ensure found on line #{line_no} but it's not the first attribute"
43
+ resource_type_token = tokens[tokens[0..resource[:start]].rindex { |r| r.first == :LBRACE } - 1]
44
+ if resource_type_token.last[:value] == "file"
45
+ resource_tokens.each_index do |resource_token_idx|
46
+ attr_token = resource_tokens[resource_token_idx]
47
+ if attr_token.first == :NAME and attr_token.last[:value] == 'mode'
48
+ value_token = resource_tokens[resource_token_idx + 2]
49
+ if value_token.first == :NAME
50
+ warn "unquoted file mode on line #{value_token.last[:line]}"
31
51
  end
32
- end
33
-
34
- # File modes SHOULD be represented as a 4 digits instead of 3, to
35
- # explicitly show that they are octal values.
36
- if attr == 'mode'
37
- unless value =~ /'\d{4}'/
38
- warn "mode should be represented as a 4 digit octal value on line #{line_no}"
52
+ if value_token.last[:value] !~ /\d{4}/
53
+ warn "mode should be represented as a 4 digit octal value on line #{value_token.last[:line]}"
39
54
  end
40
55
  end
41
-
42
- first_attribute = false
43
56
  end
44
57
  end
45
-
46
- if line.include? "}"
47
- in_resource = false
48
- first_attribute = false
49
- end
50
58
  end
51
59
  end
52
60
  end
data/puppet-lint.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'puppet-lint'
3
- s.version = '0.0.3'
3
+ s.version = '0.0.4'
4
4
  s.homepage = 'https://github.com/rodjek/puppet-lint/'
5
5
  s.summary = 'Ensure your Puppet manifests conform with the Puppetlabs style guide'
6
6
  s.description = 'Checks your Puppet manifests against the Puppetlabs
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: puppet-lint
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 23
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 3
10
- version: 0.0.3
9
+ - 4
10
+ version: 0.0.4
11
11
  platform: ruby
12
12
  authors:
13
13
  - Tim Sharpe
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-08-17 00:00:00 Z
18
+ date: 2011-08-18 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: rspec