Sha256: 5a4b15f1f0630e874916aa1a2bb7e1c1d266172e00984045a80fea7597ca14ba

Contents?: true

Size: 1.24 KB

Versions: 2

Compression:

Stored size: 1.24 KB

Contents

# frozen_string_literal: true

require 'yaml'
require 'active_support/core_ext/hash/keys'
require 'active_support/core_ext/hash/deep_merge'

module Deadpull
  module Builders
    class Configuration
      include Dry::Transaction

      HOME_PATH = File.expand_path('~/.config/deadpull.yml').freeze

      step :working_directory_config
      step :local_config
      step :inline_config

      def working_directory_config(input)
        transactionally_merge_input_with_file(input, current_working_path.join('.deadpull.yml'))
      end

      def local_config(input)
        transactionally_merge_input_with_file(input, current_working_path.join('.deadpull.local.yml'))
      end

      def inline_config(input, inline_config)
        Success(input.deep_merge(inline_config))
      end

      private

      def current_working_path
        @current_working_path ||= Pathname.new(Dir.pwd).freeze
      end

      def transactionally_merge_input_with_file(input, file)
        output = if File.exist?(file)
                   YAML.load_file(file).deep_symbolize_keys
                 else
                   {}
                 end
        Success(input.deep_merge(output))
      rescue Psych::SyntaxError => error
        Failure(error)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
deadpull-0.1.1 lib/deadpull/builders/configuration.rb
deadpull-0.1.0 lib/deadpull/builders/configuration.rb