Class: PDK::Validate::YAML::Syntax

Inherits:
BaseValidator show all
Defined in:
lib/pdk/validate/yaml/syntax.rb

Constant Summary collapse

IGNORE_DOTFILES =
false
YAML_WHITELISTED_CLASSES =
[Symbol].freeze

Constants inherited from BaseValidator

BaseValidator::ALLOW_EMPTY_TARGETS, BaseValidator::INVOKE_STYLE

Class Method Summary collapse

Methods inherited from BaseValidator

allow_empty_targets?, cmd_path, ignore_dotfiles?, ignore_pathspec, parse_options, parse_targets, process_invalid, process_skipped

Class Method Details

.create_spinner(targets = [], options = {}) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/pdk/validate/yaml/syntax.rb', line 33

def self.create_spinner(targets = [], options = {})
  return unless PDK::CLI::Util.interactive?
  options = options.merge(PDK::CLI::Util.spinner_opts_for_platform)

  exec_group = options[:exec_group]
  @spinner = if exec_group
               exec_group.add_spinner(spinner_text(targets), options)
             else
               TTY::Spinner.new("[:spinner] #{spinner_text(targets)}", options)
             end
  @spinner.auto_spin
end

.invoke(report, options = {}) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/pdk/validate/yaml/syntax.rb', line 54

def self.invoke(report, options = {})
  targets, skipped, invalid = parse_targets(options)

  process_skipped(report, skipped)
  process_invalid(report, invalid)

  return 0 if targets.empty?

  return_val = 0
  create_spinner(targets, options)

  targets.each do |target|
    next unless File.file?(target)

    unless File.readable?(target)
      report.add_event(
        file:     target,
        source:   name,
        state:    :failure,
        severity: 'error',
        message:  _('Could not be read.'),
      )
      return_val = 1
      next
    end

    begin
      ::YAML.safe_load(File.read(target), YAML_WHITELISTED_CLASSES, [], true)

      report.add_event(
        file:     target,
        source:   name,
        state:    :passed,
        severity: 'ok',
      )
    rescue Psych::SyntaxError => e
      report.add_event(
        file:     target,
        source:   name,
        state:    :failure,
        severity: 'error',
        line:     e.line,
        column:   e.column,
        message:  _('%{problem} %{context}') % {
          problem: e.problem,
          context: e.context,
        },
      )
      return_val = 1
    rescue Psych::DisallowedClass => e
      report.add_event(
        file:     target,
        source:   name,
        state:    :failure,
        severity: 'error',
        message:  _('Unsupported class: %{message}') % {
          message: e.message,
        },
      )
      return_val = 1
    end
  end

  stop_spinner(return_val)
  return_val
end

.nameObject



14
15
16
# File 'lib/pdk/validate/yaml/syntax.rb', line 14

def self.name
  'yaml-syntax'
end

.patternObject



18
19
20
21
22
23
24
25
# File 'lib/pdk/validate/yaml/syntax.rb', line 18

def self.pattern
  [
    '**/*.yaml',
    '*.yaml',
    '**/*.yml',
    '*.yml',
  ]
end

.spinner_text(_targets = []) ⇒ Object



27
28
29
30
31
# File 'lib/pdk/validate/yaml/syntax.rb', line 27

def self.spinner_text(_targets = [])
  _('Checking YAML syntax (%{targets}).') % {
    targets: pattern,
  }
end

.stop_spinner(exit_code) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/pdk/validate/yaml/syntax.rb', line 46

def self.stop_spinner(exit_code)
  if exit_code.zero? && @spinner
    @spinner.success
  elsif @spinner
    @spinner.error
  end
end