lib/fusuma/config.rb in fusuma-3.0.0 vs lib/fusuma/config.rb in fusuma-3.1.0
- old
+ new
@@ -2,10 +2,11 @@
require_relative "./multi_logger"
require_relative "./config/index"
require_relative "./config/searcher"
require_relative "./config/yaml_duplication_checker"
+require_relative "./plugin/manager"
require_relative "./hash_support"
require "singleton"
require "yaml"
# module as namespace
@@ -30,11 +31,11 @@
def custom_path=(new_path)
instance.custom_path = new_path
end
end
- attr_reader :keymap, :custom_path, :searcher
+ attr_reader :custom_path, :searcher
def initialize
@searcher = Searcher.new
@custom_path = nil
@keymap = nil
@@ -43,21 +44,51 @@
def custom_path=(new_path)
@custom_path = new_path
reload
end
+ def keymap
+ # FIXME: @keymap is not initialized when called from outside Fusuma::Runner like fusuma-senkey
+ @keymap || reload.keymap
+ end
+
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
- path = find_filepath
- MultiLogger.info "reload config: #{path}"
- @keymap = validate(path)
+
self
rescue InvalidFileError => e
MultiLogger.error e.message
exit 1
end
+ # @param key [Symbol]
+ # @param base [Config::Index]
+ # @return [Hash]
+ 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
+
# @return [Hash] If check passes
# @raise [InvalidFileError] If check does not pass
def validate(path)
duplicates = []
YAMLDuplicationChecker.check(File.read(path), path) do |ignored, duplicate|
@@ -73,11 +104,10 @@
yaml.deep_symbolize_keys
end
end
# @param index [Index]
- # @param context [Hash]
def search(index)
@searcher.search_with_cache(index, location: keymap)
end
# @param index [Config::Index]
@@ -93,11 +123,11 @@
@execute_keys.find { |k| execute_params.key?(k) }
end
private
- def find_filepath
+ def find_config_filepath
filename = "fusuma/config.yml"
if custom_path
return expand_custom_path if File.exist?(expand_custom_path)
raise NotFoundError, "#{expand_custom_path} is NOT FOUND"
@@ -117,8 +147,15 @@
File.expand_path "~/.config/#{filename}"
end
def expand_default_path(filename)
File.expand_path "../../#{filename}", __FILE__
+ end
+
+ def plugin_defaults_paths
+ Plugin::Manager.load_paths.map do |plugin_path|
+ yml = plugin_path.gsub(/\.rb$/, ".yml")
+ yml if File.exist?(yml)
+ end.compact
end
end
end