Sha256: 3eadb2a1980cd1e9b1efe8cd93e6df3f8bda85ce9650775e6f25a3e3f0d773e3

Contents?: true

Size: 1.83 KB

Versions: 16

Compression:

Stored size: 1.83 KB

Contents

require 'set'
require 'hashlib'

module Onering
  class Config
    class<<self
      DEFAULT_CONFIG={}
      DEFAULT_OPTIONS_FILE=["~/.onering/cli.yml", "~/.onering/key.yml", "/etc/onering/cli.yml", "/etc/onering/key.yml"]

      def load(configfile=nil, config={})
        if configfile.nil?
          configfile = []
        else
        # recursively grab all .yml files if directory is specified
          configfile = (File.directory?(configfile) ? Dir.glob(File.join(configfile, "**", "*.yml")).sort : [configfile])
        end

      # list all existing config files from least specific to most
        @_configfiles = (configfile + DEFAULT_OPTIONS_FILE).compact.select{|i|
          (File.exists?(File.expand_path(i)) rescue false)
        }.reverse

      # merge all config files with more-specific values overriding less-specific ones
        @_config ||= DEFAULT_CONFIG
        @_configfiles_seen ||= Set.new()

      # only load files we haven't seen before
        (@_configfiles - @_configfiles_seen.to_a).each do |i|
          c = YAML.load(File.read(File.expand_path(i))) rescue {}
            
          if c and not c.empty?
            Onering::Logger.info("Loading config file at #{File.expand_path(i) rescue i}", "Onering::Config")
            @_config.deep_merge!(c)
          end

          @_configfiles_seen << i
        end


      # settings specified in the library override everything
        unless config.empty?
          Onering::Logger.debug("Merging settings specified in library instantiation", "Onering::Config")
          @_config.deep_merge!(config.compact)
        end

        return @_config
      end

      def get(key, default=nil)
        @_config.get(key, default)
      end

      def set(key, value)
        @_config.set(key, value)
      end

      def unset(key)
        @_config.unset(key)
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 2 rubygems

Version Path
onering-agent-0.4.4 lib/onering/config.rb
onering-agent-0.4.3 lib/onering/config.rb
onering-client-0.4.3 lib/onering/config.rb
onering-client-0.4.2 lib/onering/config.rb
onering-client-0.4.1 lib/onering/config.rb
onering-client-0.4.0 lib/onering/config.rb
onering-client-0.3.4 lib/onering/config.rb
onering-client-0.3.3 lib/onering/config.rb
onering-client-0.3.2 lib/onering/config.rb
onering-client-0.3.1 lib/onering/config.rb
onering-client-0.3.0 lib/onering/config.rb
onering-client-0.2.4 lib/onering/config.rb
onering-client-0.2.3 lib/onering/config.rb
onering-client-0.2.1 lib/onering/config.rb
onering-client-0.2.0 lib/onering/config.rb
onering-client-0.1.7 lib/onering/config.rb