Module: Opencontrol::Linter

Defined in:
lib/opencontrol/linter.rb

Overview

This module holds the Opencontrol Linter Command Line Interface.

Class Method Summary collapse

Class Method Details

.empty_targetsObject



115
116
117
118
119
120
121
122
# File 'lib/opencontrol/linter.rb', line 115

def self.empty_targets
  {
    components: [],
    standards: [],
    certifications: [],
    opencontrols: []
  }
end

.files_not_found_issueObject



69
70
71
72
73
74
75
76
77
78
# File 'lib/opencontrol/linter.rb', line 69

def self.files_not_found_issue
  Kwalify::BaseError.new(
    'No validation files found for the pattern supplied. \
   Adding an issue to avoid failing silently.',
    nil,
    nil,
    nil,
    :linter_files_not_found_issue
  )
end

.find_schema(type, version) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/opencontrol/linter.rb', line 13

def self.find_schema(type, version)
  dir = __dir__
  case type
  when :components
    "#{dir}/../../vendor/schemas/kwalify/component/v#{version}.yaml"
  when :standards
    "#{dir}/../../vendor/schemas/kwalify/standard/v#{version}.yaml"
  when :certifications
    "#{dir}/../../vendor/schemas/kwalify/certification/v#{version}.yaml"
  when :opencontrols
    "#{dir}/../../vendor/schemas/kwalify/opencontrol/v#{version}.yaml"
  else
    throw "Unknown type of schema specified #{type} " \
"tried to get schema version #{version}"
  end
end

.load_schema(schema_file) ⇒ Object



30
31
32
33
34
35
# File 'lib/opencontrol/linter.rb', line 30

def self.load_schema(schema_file)
  ## load schema data
  # Kwalify::Yaml.load_file(schema_file)
  ## or
  YAML.load_file(schema_file)
end

.run(specification) ⇒ Object



124
125
126
127
128
129
130
131
132
133
# File 'lib/opencontrol/linter.rb', line 124

def self.run(specification)
  specification[:targets] = empty_targets.merge(specification[:targets])
  issues = validate_all(specification)
  if !issues.empty?
    puts "Complete. #{issues.length} issues found.".red
  else
    puts 'Complete. No problems found.'.green
  end
  issues.length
end

.schema_file_for_document(type, document) ⇒ Object



48
49
50
51
# File 'lib/opencontrol/linter.rb', line 48

def self.schema_file_for_document(type, document)
  version = document['schema_version'] || '1.0.0'
  find_schema(type, version)
end

.schema_not_found_issue(filename, schema_filename) ⇒ Object



80
81
82
83
84
85
86
87
88
# File 'lib/opencontrol/linter.rb', line 80

def self.schema_not_found_issue(filename, schema_filename)
  Kwalify::BaseError.new(
    'No schema files found for the pattern supplied.',
    filename,
    schema_filename,
    nil,
    :schema_files_not_found_issue
  )
end

.show_issues(issues, filename) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/opencontrol/linter.rb', line 37

def self.show_issues(issues, filename)
  if issues && !issues.empty?
    puts "#{filename}".red
    issues.each do |issue|
      puts Opencontrol::Messages.detail(issue).yellow
    end
  else
    puts "#{filename}".green
  end
end

.validate(type, filename) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/opencontrol/linter.rb', line 53

def self.validate(type, filename)
  ## load document
  # document = Kwalify::Yaml.load_file(filename)
  ## or
  document = YAML.load_file(filename)
  schema_filename = schema_file_for_document(type, document)
  unless File.exist?(schema_filename)
    return [schema_not_found_issue(filename, schema_filename)]
  end

  schema = load_schema(schema_filename)

  validator = Kwalify::Validator.new(schema)
  validator.validate(document)
end

.validate_all(specification) ⇒ Object



105
106
107
108
109
110
111
112
113
# File 'lib/opencontrol/linter.rb', line 105

def self.validate_all(specification)
  issues = []
  specification[:targets].each do |type, patterns|
    patterns.each do |pattern|
      issues += validate_pattern(type: type, pattern: pattern)
    end
  end
  issues
end

.validate_pattern(target) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/opencontrol/linter.rb', line 90

def self.validate_pattern(target)
  filenames = Dir[target[:pattern]]
  issues = []
  if filenames.empty?
    issues = [files_not_found_issue]
    show_issues(issues, target[:pattern])
    return issues
  end
  filenames.each do |filename|
    issues += validate(target[:type], filename)
    show_issues(issues, filename)
  end
  issues
end