Class: PDK::Validate::Tasks::MetadataLint

Inherits:
BaseValidator show all
Defined in:
lib/pdk/validate/tasks/metadata_lint.rb

Constant Summary collapse

FORGE_SCHEMA_URL =
'https://forgeapi.puppet.com/schemas/task.json'.freeze

Constants inherited from BaseValidator

BaseValidator::ALLOW_EMPTY_TARGETS, BaseValidator::IGNORE_DOTFILES, 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



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/pdk/validate/tasks/metadata_lint.rb', line 28

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



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
120
121
122
# File 'lib/pdk/validate/tasks/metadata_lint.rb', line 59

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|
    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
      # Need to set the JSON Parser and State Generator to the Native one to be
      # compatible with the multi_json adapter.
      JSON.parser = JSON::Ext::Parser if defined?(JSON::Ext::Parser)
      JSON.generator = JSON::Ext::Generator

      begin
        errors = JSON::Validator.fully_validate(schema_file, File.read(target)) || []
      rescue JSON::Schema::SchemaError => e
        raise PDK::CLI::FatalError, _('Unable to validate Task Metadata. %{error}.') % { error: e.message }
      end

      if errors.empty?
        report.add_event(
          file:     target,
          source:   name,
          state:    :passed,
          severity: 'ok',
        )
      else
        errors.each do |error|
          # strip off the trailing parts that aren't relevant
          error = error.split('in schema').first if error.include? 'in schema'

          report.add_event(
            file:     target,
            source:   name,
            state:    :failure,
            severity: 'error',
            message:  error,
          )
        end
        return_val = 1
      end
    end
  end

  stop_spinner(return_val)
  return_val
end

.nameObject



14
15
16
# File 'lib/pdk/validate/tasks/metadata_lint.rb', line 14

def self.name
  'task-metadata-lint'
end

.patternObject



18
19
20
# File 'lib/pdk/validate/tasks/metadata_lint.rb', line 18

def self.pattern
  'tasks/*.json'
end

.schema_fileObject



49
50
51
52
53
54
55
56
57
# File 'lib/pdk/validate/tasks/metadata_lint.rb', line 49

def self.schema_file
  schema = PDK::Util::VendoredFile.new('task.json', FORGE_SCHEMA_URL).read

  JSON.parse(schema)
rescue PDK::Util::VendoredFile::DownloadError => e
  raise PDK::CLI::FatalError, e.message
rescue JSON::ParserError
  raise PDK::CLI::FatalError, _('Failed to parse Task Metadata Schema file.')
end

.spinner_text(_targets = []) ⇒ Object



22
23
24
25
26
# File 'lib/pdk/validate/tasks/metadata_lint.rb', line 22

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

.stop_spinner(exit_code) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/pdk/validate/tasks/metadata_lint.rb', line 41

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