Sha256: dc0a5e998ec83e94f73b1e8a2f631691373c3e09e9387db163d0ec43f1d4461b

Contents?: true

Size: 1.39 KB

Versions: 5

Compression:

Stored size: 1.39 KB

Contents

require 'logger'
require 'psych'

# add a deep_merge function to hashes
# rubocop:disable Style/CaseEquality, Style/ClassAndModuleChildren
class ::Hash
  def deep_merge(second)
    merger = proc do |_key, v1, v2|
      if Hash === v1 && Hash === v2
        v1.merge(v2, &merger)
      else
        v2
      end
    end
    merge(second, &merger)
  end
end

module PicsolveDockerBuilder
  #  Base class for everything
  #  * logging
  #  * accessing config
  module Base
    def create_logger
      log = Logger.new(STDOUT)
      log.level = Logger::DEBUG
      log
    end

    def log
      @logger ||= create_logger
    end

    def config
      @config ||= read_config
    end

    def base_dir
      Dir.pwd
    end

    def config_file
      ENV['DOCKER_BUILDER_FILE'] || '.docker-builder.yml'
    end

    def config_path
      File.join(base_dir, config_file)
    end

    # This should replace config_path pretty soon
    def config_paths
      [config_path] + Dir.glob(
        config_path[0, config_path.length - 3] + '*.yml'
      )
    end

    def default_config
      {}
    end

    def validate_config(c)
      c
    end

    def read_config
      c = default_config
      path = config_path
      begin
        yaml = Psych.load_file path
        c = c.deep_merge(yaml)
      rescue Errno::ENOENT
        log.debug "can not find config at '#{path}'"
      end
      validate_config(c)
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
picsolve_docker_builder-0.5.5 lib/picsolve_docker_builder/base.rb
picsolve_docker_builder-0.5.4 lib/picsolve_docker_builder/base.rb
picsolve_docker_builder-0.5.3 lib/picsolve_docker_builder/base.rb
picsolve_docker_builder-0.5.2 lib/picsolve_docker_builder/base.rb
picsolve_docker_builder-0.5.1 lib/picsolve_docker_builder/base.rb