Module: Evilution::Config::FileLoader

Defined in:
lib/evilution/config/file_loader.rb

Constant Summary collapse

KNOWN_KEYS =

Keys recognised in YAML config files. ‘target_files` is intentionally excluded because it is CLI-positional (the file paths after `evilution run`).

(Evilution::Config::DEFAULTS.keys + %i[hooks]).uniq.freeze

Class Method Summary collapse

Class Method Details

.loadObject



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

def load
  Evilution::Config::CONFIG_FILES.each do |path|
    next unless File.exist?(path)

    data = YAML.safe_load_file(path, symbolize_names: true)
    return {} unless data.is_a?(Hash)

    validate_schema!(data, path: path) if data.key?(:schema_version)
    return data
  rescue Psych::SyntaxError, Psych::DisallowedClass => e
    raise Evilution::ConfigError.new("failed to parse config file #{path}: #{e.message}", file: path)
  rescue SystemCallError => e
    raise Evilution::ConfigError.new("cannot read config file #{path}: #{e.message}", file: path)
  end

  {}
end

.validate_known_keys!(keys, path:) ⇒ Object



52
53
54
55
56
57
58
59
60
# File 'lib/evilution/config/file_loader.rb', line 52

def validate_known_keys!(keys, path:)
  unknown = keys - KNOWN_KEYS
  return if unknown.empty?

  raise Evilution::ConfigError.new(
    "unknown key(s) #{unknown.inspect} in #{path}. Known keys: #{KNOWN_KEYS.sort.inspect}",
    file: path
  )
end

.validate_schema!(data, path:) ⇒ Object



30
31
32
33
# File 'lib/evilution/config/file_loader.rb', line 30

def validate_schema!(data, path:)
  validate_schema_version_value!(data[:schema_version], path: path)
  validate_known_keys!(data.keys, path: path)
end

.validate_schema_version_value!(version, path:) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/evilution/config/file_loader.rb', line 35

def validate_schema_version_value!(version, path:)
  unless version.is_a?(Integer) && version.positive?
    raise Evilution::ConfigError.new(
      "invalid schema_version #{version.inspect} in #{path}: must be a positive Integer",
      file: path
    )
  end

  return if version <= Evilution::Config::CURRENT_SCHEMA_VERSION

  raise Evilution::ConfigError.new(
    "schema_version #{version} in #{path} is newer than this evilution gem supports " \
    "(current: #{Evilution::Config::CURRENT_SCHEMA_VERSION}). Upgrade the gem.",
    file: path
  )
end