Class: PlatformosCheck::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/platformos_check/config.rb

Constant Summary collapse

DOTFILE =
'.platformos-check.yml'
BUNDLED_CONFIGS_DIR =
Pathname.new("#{__dir__}/../../config").realpath
BOOLEAN =
[true, false]

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root: nil, configuration: nil, should_resolve_requires: true) ⇒ Config

Returns a new instance of Config.



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/platformos_check/config.rb', line 86

def initialize(root: nil, configuration: nil, should_resolve_requires: true)
  @configuration = if configuration
                     validate_configuration(configuration)
                   else
                     self.class.default
                   end

  extends = @configuration["extends"] || :default
  @configuration = self.class.merge_configurations!(self.class.load_config(extends), @configuration)

  @root = if root && @configuration.key?("root")
            Pathname.new(root).join(@configuration["root"])
          elsif root
            Pathname.new(root)
          end

  @auto_correct = false

  resolve_requires if @root && should_resolve_requires
end

Class Attribute Details

.last_loaded_configObject (readonly)

Returns the value of attribute last_loaded_config.



13
14
15
# File 'lib/platformos_check/config.rb', line 13

def last_loaded_config
  @last_loaded_config
end

Instance Attribute Details

#auto_correctObject

Returns the value of attribute auto_correct.



10
11
12
# File 'lib/platformos_check/config.rb', line 10

def auto_correct
  @auto_correct
end

#rootObject (readonly)

Returns the value of attribute root.



9
10
11
# File 'lib/platformos_check/config.rb', line 9

def root
  @root
end

Class Method Details

.bundled_config?(name) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/platformos_check/config.rb', line 50

def bundled_config?(name)
  name.is_a?(Symbol) || (name.is_a?(String) && name[0] == ":")
end

.bundled_config_path(name) ⇒ Object



46
47
48
# File 'lib/platformos_check/config.rb', line 46

def bundled_config_path(name)
  "#{BUNDLED_CONFIGS_DIR}/#{name.to_s.sub(/^:/, '')}.yml"
end

.defaultObject



81
82
83
# File 'lib/platformos_check/config.rb', line 81

def default
  load_config(":default")
end

.find(root, needle = DOTFILE) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/platformos_check/config.rb', line 32

def find(root, needle = DOTFILE)
  Pathname.new(root).descend.reverse_each do |path|
    pathname = path.join(needle)
    return pathname if pathname.exist?
  end
  nil
end

.from_hash(config) ⇒ Object



28
29
30
# File 'lib/platformos_check/config.rb', line 28

def from_hash(config)
  new(configuration: config, should_resolve_requires: false)
end

.from_path(path) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/platformos_check/config.rb', line 15

def from_path(path)
  if (filename = find(path))
    new(root: filename.dirname, configuration: load_config(filename))
  else
    # No configuration file
    new(root: path)
  end
end

.from_string(config) ⇒ Object



24
25
26
# File 'lib/platformos_check/config.rb', line 24

def from_string(config)
  new(configuration: YAML.load(config), should_resolve_requires: false)
end

.load_bundled_config(name) ⇒ Object



54
55
56
# File 'lib/platformos_check/config.rb', line 54

def load_bundled_config(name)
  load_file(bundled_config_path(name))
end

.load_config(name, pwd = Pathname.pwd) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/platformos_check/config.rb', line 58

def load_config(name, pwd = Pathname.pwd)
  return load_bundled_config(name) if bundled_config?(name)

  path = name.is_a?(Pathname) ? name : Pathname.new(name)
  path = pwd.join(path) if path.relative?
  return {} unless path.exist?

  config = load_file(path)
  extends = config["extends"] || :default
  merge_configurations!(load_config(extends, path.realpath.dirname), config)
end

.load_file(absolute_path) ⇒ Object



40
41
42
43
44
# File 'lib/platformos_check/config.rb', line 40

def load_file(absolute_path)
  @last_loaded_config = absolute_path
  # An empty file returns false, so we || {}.
  YAML.load_file(absolute_path) || {}
end

.merge_configurations!(config, other) ⇒ Object



70
71
72
73
74
75
76
77
78
79
# File 'lib/platformos_check/config.rb', line 70

def merge_configurations!(config, other)
  config.merge(other) do |_key, old_value, new_value|
    case old_value
    when Hash
      merge_configurations!(old_value, new_value)
    else
      new_value
    end
  end
end

Instance Method Details

#[](name) ⇒ Object



107
108
109
# File 'lib/platformos_check/config.rb', line 107

def [](name)
  @configuration[name]
end

#check_configurationsObject



115
116
117
# File 'lib/platformos_check/config.rb', line 115

def check_configurations
  @check_configurations ||= @configuration.select { |name, _| check_name?(name) }
end

#enabled_checksObject



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/platformos_check/config.rb', line 119

def enabled_checks
  @enabled_checks ||= check_configurations.map do |check_name, options|
    next unless options["enabled"]

    check_class = PlatformosCheck.const_get(check_name)

    next if check_class.categories.any? { |category| exclude_categories.include?(category) }
    next if include_categories.any? && !include_categories.all? { |category| check_class.categories.include?(category) }

    options_for_check = options.transform_keys(&:to_sym)
    options_for_check.delete(:enabled)
    severity = options_for_check.delete(:severity)
    check_ignored_patterns = options_for_check.delete(:ignore) || []
    check = if options_for_check.empty?
              check_class.new
            else
              check_class.new(**options_for_check)
            end
    check.severity = severity.to_sym if severity
    check.ignored_patterns = check_ignored_patterns + ignored_patterns
    check.options = options_for_check
    check
  end.compact
end

#exclude_categoriesObject



156
157
158
# File 'lib/platformos_check/config.rb', line 156

def exclude_categories
  self["exclude_categories"] || []
end

#exclude_categories=(categories) ⇒ Object



160
161
162
# File 'lib/platformos_check/config.rb', line 160

def exclude_categories=(categories)
  @configuration["exclude_categories"] = categories
end

#ignored_patternsObject



144
145
146
# File 'lib/platformos_check/config.rb', line 144

def ignored_patterns
  self["ignore"] || []
end

#include_categoriesObject



148
149
150
# File 'lib/platformos_check/config.rb', line 148

def include_categories
  self["include_categories"] || []
end

#include_categories=(categories) ⇒ Object



152
153
154
# File 'lib/platformos_check/config.rb', line 152

def include_categories=(categories)
  @configuration["include_categories"] = categories
end

#to_hObject



111
112
113
# File 'lib/platformos_check/config.rb', line 111

def to_h
  @configuration
end