Class: PuppetSyntax::Templates

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet-syntax/templates.rb

Instance Method Summary collapse

Instance Method Details

#check(filelist) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/puppet-syntax/templates.rb', line 7

def check(filelist)
  raise "Expected an array of files" unless filelist.is_a?(Array)

  # We now have to redirect STDERR in order to capture warnings.
  $stderr = warnings = StringIO.new()
  errors = []

  filelist.each do |file|
    if File.extname(file) == '.epp' or PuppetSyntax.epp_only
      errors.concat validate_epp(file)
    elsif File.extname(file) == '.erb'
      errors.concat validate_erb(file)
    end
  end

  $stderr = STDERR
  errors << warnings.string unless warnings.string.empty?
  errors.map! { |e| e.to_s }

  errors
end

#validate_epp(filename) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/puppet-syntax/templates.rb', line 29

def validate_epp(filename)
  if Puppet.version.to_f < 3.7
    raise "Cannot validate EPP without Puppet 4 or future parser (3.7+)"
  end

  require 'puppet/pops'
  errors = []
  begin
    parser = Puppet::Pops::Parser::EvaluatingParser::EvaluatingEppParser.new()
    parser.parse_file(filename)
  rescue => detail
    errors << detail
  end

  errors
end

#validate_erb(filename) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/puppet-syntax/templates.rb', line 46

def validate_erb(filename)
  errors = []

  begin
    erb = ERB.new(File.read(filename), nil, '-')
    erb.filename = filename
    erb.result
  rescue NameError => error
    # This is normal because we don't have the variables that would
    # ordinarily be bound by the parent Puppet manifest.
  rescue TypeError
    # This is normal because we don't have the variables that would
    # ordinarily be bound by the parent Puppet manifest.
  rescue SyntaxError => error
    errors << error
  end

  errors
end