lib/fusuma/config.rb in fusuma-2.4.1 vs lib/fusuma/config.rb in fusuma-2.5.0

- old
+ new

@@ -1,14 +1,14 @@ # frozen_string_literal: true -require_relative './multi_logger' -require_relative './config/index' -require_relative './config/searcher' -require_relative './config/yaml_duplication_checker' -require_relative './hash_support' -require 'singleton' -require 'yaml' +require_relative "./multi_logger" +require_relative "./config/index" +require_relative "./config/searcher" +require_relative "./config/yaml_duplication_checker" +require_relative "./hash_support" +require "singleton" +require "yaml" # module as namespace module Fusuma # read keymap from yaml file class Config @@ -49,10 +49,13 @@ @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 # @return [Hash] If check passes # @raise [InvalidFileError] If check does not pass def validate(path) @@ -62,14 +65,15 @@ duplicates << duplicate.value end raise InvalidFileError, "Detect duplicate keys #{duplicates}" unless duplicates.empty? yamls = YAML.load_stream(File.read(path)).compact - yamls.map(&:deep_symbolize_keys) - rescue StandardError => e - MultiLogger.error e.message - raise InvalidFileError, e.message + yamls.map do |yaml| + raise InvalidFileError, "invalid config.yml: #{path}" unless yaml.is_a? Hash + + yaml.deep_symbolize_keys + end end # @param index [Index] # @param context [Hash] def search(index) @@ -84,16 +88,16 @@ end.flatten execute_params = search(index) return if execute_params.nil? || !execute_params.is_a?(Hash) - @execute_keys.find { |k| execute_params.keys.include?(k) } + @execute_keys.find { |k| execute_params.key?(k) } end private def find_filepath - filename = 'fusuma/config.yml' + 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" elsif File.exist?(expand_config_path(filename))