Sha256: 601c0c7847669f77f51308ee25073f74573d0fa3d6fd3270798961f8c10af88c

Contents?: true

Size: 1.31 KB

Versions: 6

Compression:

Stored size: 1.31 KB

Contents

require 'pathname'
require 'active_support'
require 'active_support/core_ext/enumerable'
require 'guignol'

module Guignol::Configuration

  def configuration
    @configuration ||= load_config_file
  end

  private

  def config_file_path
    @config_file_path ||= [
      Pathname.new(ENV['GUIGNOL_YML'] || '/var/nonexistent'),
      Pathname.new('guignol.yml'),
      Pathname.new('config/guignol.yml'),
      Pathname.new(ENV['HOME']).join('.guignol.yml')
    ].find(&:exist?)
  end

  # Load the config hash for the file, converting old (v0.2.0) Yaml config files.
  def load_config_file
    return {} if config_file_path.nil?
    data = YAML.load(config_file_path.read)
    return data unless data.kind_of?(Array)

    # Convert the toplevel array to a hash. Same for arrays of volumes.
    Guignol.logger.warn "Configuration file '#{config_file_path}' uses the old array format. Trying to load it."
    raise "Instance config lacks :name" unless data.collect_key(:name).all?
    result = data.index_by { |item| item.delete(:name) }
    result.each_pair do |name, config|
      next unless config[:volumes]
      raise "Volume config lacks :name" unless config[:volumes].collect_key(:name).all?
      config[:volumes] = config[:volumes].index_by { |item| item.delete(:name) }
    end

    return result
  end

  Guignol.extend(self)
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
guignol-0.3.9 lib/guignol/configuration.rb
guignol-0.3.8 lib/guignol/configuration.rb
guignol-0.3.7 lib/guignol/configuration.rb
guignol-0.3.6.2 lib/guignol/configuration.rb
guignol-0.3.6.1 lib/guignol/configuration.rb
guignol-0.3.6 lib/guignol/configuration.rb