Class: PDK::Validate::PuppetSyntax

Inherits:
BaseValidator show all
Defined in:
lib/pdk/validators/puppet/puppet_syntax.rb

Constant Summary

Constants inherited from BaseValidator

BaseValidator::INVOKE_STYLE

Class Method Summary collapse

Methods inherited from BaseValidator

cmd_path, invoke, parse_targets, process_invalid, process_skipped

Class Method Details

.cmdObject



12
13
14
# File 'lib/pdk/validators/puppet/puppet_syntax.rb', line 12

def self.cmd
  'puppet'
end

.nameObject



8
9
10
# File 'lib/pdk/validators/puppet/puppet_syntax.rb', line 8

def self.name
  'puppet-syntax'
end

.parse_options(_options, targets) ⇒ Object



24
25
26
# File 'lib/pdk/validators/puppet/puppet_syntax.rb', line 24

def self.parse_options(_options, targets)
  %w[parser validate].concat(targets)
end

.parse_output(report, result, targets) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/pdk/validators/puppet/puppet_syntax.rb', line 28

def self.parse_output(report, result, targets)
  # Due to PUP-7504, we will have to programmatically construct the json
  # object from the text output for now.
  output = result[:stderr].split("\n")

  results_data = []
  output.each do |offense|
    sanitize_console_output(offense)
    message, _at, location_raw = offense.partition(' at ')

    # Parse the offense type and msg
    severity, _colon, message = message.rpartition(': ')

    # Parse the offense location info
    location = location_raw.strip.match(%r{\A(?<file>.+):(?<line>\d+):(?<column>\d+)\Z}) unless location_raw.nil?

    attributes = {
      source:  name,
      message: message.strip,
      state:  :failure,
    }
    attributes[:severity] = severity.strip unless severity.nil?

    unless location.nil?
      attributes[:file] = location[:file]
      attributes[:line] = location[:line]
      attributes[:column] = location[:column]
    end

    results_data << attributes
  end

  # puppet parser validate does not include files without problems in its
  # output, so we need to go through the list of targets and add passing
  # events to the report for any target not listed in the output.
  targets.reject { |target| results_data.any? { |j| j[:file] == target } }.each do |target|
    report.add_event(
      file:     target,
      source:   name,
      severity: :ok,
      state:    :passed,
    )
  end

  results_data.each do |offense|
    report.add_event(offense)
  end
end

.patternObject



16
17
18
# File 'lib/pdk/validators/puppet/puppet_syntax.rb', line 16

def self.pattern
  '**/**.pp'
end

.sanitize_console_output(line) ⇒ Object



77
78
79
# File 'lib/pdk/validators/puppet/puppet_syntax.rb', line 77

def self.sanitize_console_output(line)
  line.gsub!(%r{\e\[([;\d]+)?m}, '')
end

.spinner_text(_targets = nil) ⇒ Object



20
21
22
# File 'lib/pdk/validators/puppet/puppet_syntax.rb', line 20

def self.spinner_text(_targets = nil)
  _('Checking Puppet manifest syntax (%{pattern})') % { pattern: pattern }
end