Class: Fusuma::Config

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/fusuma/config.rb,
lib/fusuma/config/index.rb,
lib/fusuma/config/searcher.rb,
lib/fusuma/config/yaml_duplication_checker.rb

Overview

read keymap from yaml file

Defined Under Namespace

Modules: YAMLDuplicationChecker Classes: Index, InvalidFileError, NotFoundError, Searcher

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



38
39
40
41
42
# File 'lib/fusuma/config.rb', line 38

def initialize
  @searcher = Searcher.new
  @custom_path = nil
  @keymap = nil
end

Instance Attribute Details

#custom_pathObject

Returns the value of attribute custom_path.



36
37
38
# File 'lib/fusuma/config.rb', line 36

def custom_path
  @custom_path
end

#searcherObject (readonly)

Returns the value of attribute searcher.



36
37
38
# File 'lib/fusuma/config.rb', line 36

def searcher
  @searcher
end

Class Method Details

.custom_path=(new_path) ⇒ Object



31
32
33
# File 'lib/fusuma/config.rb', line 31

def custom_path=(new_path)
  instance.custom_path = new_path
end

.find_execute_key(index) ⇒ Object



27
28
29
# File 'lib/fusuma/config.rb', line 27

def find_execute_key(index)
  instance.find_execute_key(index)
end

.search(index) ⇒ Object



23
24
25
# File 'lib/fusuma/config.rb', line 23

def search(index)
  instance.search(index)
end

Instance Method Details

#fetch_config_params(key, base) ⇒ Hash

Parameters:

Returns:



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/fusuma/config.rb', line 79

def fetch_config_params(key, base)
  request_context = {plugin_defaults: base.keys.last.symbol.to_s}
  fallbacks = [:no_context, :plugin_default_context]
  Config::Searcher.find_context(request_context, fallbacks) do
    ret = Config.search(base)
    if ret&.key?(key)
      return ret
    end
  end
  {}
end

#find_execute_key(index) ⇒ Object

Returns Symbol.

Parameters:

Returns:

  • Symbol



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/fusuma/config.rb', line 118

def find_execute_key(index)
  @execute_keys ||= Plugin::Executors::Executor.plugins.map do |executor|
    executor.new.execute_keys
  end.flatten

  @cache_execute_keys ||= {}

  cache_key = [index.cache_key, Searcher.context].join

  return @cache_execute_keys[cache_key] if @cache_execute_keys.has_key?(cache_key)

  @cache_execute_keys[cache_key] =
    @execute_keys.find do |execute_key|
      new_index = Config::Index.new(index.keys | [execute_key])
      search(new_index)
    end
end

#keymapObject



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

def keymap
  # FIXME: @keymap is not initialized when called from outside Fusuma::Runner like fusuma-senkey
  @keymap || reload.keymap
end

#reloadObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/fusuma/config.rb', line 54

def reload
  plugin_defaults = plugin_defaults_paths.map do |default_yml|
    {
      context: {plugin_defaults: default_yml.split("/").last.delete_suffix(".yml")},
      **validate(default_yml)[0]
    }
  end

  config_path = find_config_filepath
  @keymap = validate(config_path) | plugin_defaults
  MultiLogger.info "reload config: #{config_path}"

  # reset searcher cache
  @searcher = Searcher.new
  @cache_execute_keys = nil

  self
rescue InvalidFileError => e
  MultiLogger.error e.message
  exit 1
end

#search(index) ⇒ Object

Parameters:



112
113
114
# File 'lib/fusuma/config.rb', line 112

def search(index)
  @searcher.search_with_cache(index, location: keymap)
end

#validate(path) ⇒ Hash

Returns If check passes.

Returns:

  • (Hash)

    If check passes

Raises:



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/fusuma/config.rb', line 93

def validate(path)
  duplicates = []
  YAMLDuplicationChecker.check(File.read(path), path) do |ignored, duplicate|
    MultiLogger.error "#{path}: #{ignored.value} is duplicated"
    duplicates << duplicate.value
  end
  raise InvalidFileError, "Detect duplicate keys #{duplicates}" unless duplicates.empty?

  yamls = YAML.load_stream(File.read(path)).compact
  yamls.map do |yaml|
    raise InvalidFileError, "Invalid config.yml: #{path}" unless yaml.is_a? Hash

    yaml.deep_symbolize_keys
  end
rescue Psych::SyntaxError => e
  raise InvalidFileError, "Invalid syntax: #{path} #{e.message}"
end