Sha256: 007b43a0ec180fb5946aee94f57b60282fba373ad123b89a2912eb41c15dcb2c

Contents?: true

Size: 1.91 KB

Versions: 4

Compression:

Stored size: 1.91 KB

Contents

# frozen_string_literal: true

%w[
  erb
  yaml
].each(&method(:require))

module Diffend
  # Module for all the components related to setting up the config
  module Configs
    # Class responsible for fetching the config from .diffend.yml
    module Fetcher
      class << self
        # @param logger [Diffend::Logger]
        # @param plugin_path [String] path of the plugin
        # @param build_path [String] path of the current build
        #
        # @return [Hash] details from configuration file
        #
        # @example
        #   details = Fetcher.new.call('./')
        #   details.build_path #=> './'
        def call(logger, plugin_path, build_path)
          build(plugin_path, build_path)
        rescue Errors::MalformedConfigurationFile
          build_malformed_error_message(build_path)
            .tap(&logger.method(:fatal))

          raise Diffend::Errors::HandledException
        end

        private

        # @param plugin_path [String] path of the plugin
        # @param build_path [String] path of the current build
        #
        # @return [OpenStruct] open struct with config details
        def build(plugin_path, build_path)
          default_config = File.join(plugin_path, 'config', 'diffend.yml')
          project_config = File.join(build_path, '.diffend.yml')

          hash = read_file(default_config)

          if File.exist?(project_config)
            hash.merge!(read_file(project_config) || {})
          end

          hash
        end

        def read_file(path)
          YAML.safe_load(ERB.new(File.read(path)).result)
        rescue Psych::SyntaxError
          raise Errors::MalformedConfigurationFile
        end

        # @return [String] malformed configuration file message
        def build_malformed_error_message
          <<~MSG
            \nYour Diffend configuration file is malformed.\n
            Please re-setup.\n
          MSG
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
diffend-monitor-0.2.36 lib/diffend/configs/fetcher.rb
diffend-0.2.36 lib/diffend/configs/fetcher.rb
diffend-monitor-0.2.35 lib/diffend/configs/fetcher.rb
diffend-0.2.35 lib/diffend/configs/fetcher.rb