Sha256: c69f8c430080dad12c5ec5910af2f933eff62294cfefbc1bfee8d39745481f75

Contents?: true

Size: 1.75 KB

Versions: 7

Compression:

Stored size: 1.75 KB

Contents

require 'mustache'
require 'json'

module Vcloud
  module Core
    class ConfigLoader

      # Loads the configuration from +config_file+, optionally rendering
      # +config_file+ as a Mustache template using vars in +vars_file+ and
      # optionally validating config against +schema+ supplied.
      #
      # @param config_file [String]      Location of the YAML config file
      # @param schema      [String, nil] Location of the validation schema
      #                                  if nil, no validation takes place.
      # @param vars_file   [String, nil] Location of the vars_file (YAML),
      #                                  if nil, config_file is not rendered
      #                                  by Mustache
      # @return [Hash]
      def load_config(config_file, schema = nil, vars_file = nil)
        if vars_file
          rendered_config = Mustache.render(
            File.read(config_file),
            YAML::load_file(vars_file)
          )
          input_config = YAML::load(rendered_config)
        else
          input_config = YAML::load_file(config_file)
        end

        # There is no way in YAML or Ruby to symbolize keys in a hash
        json_string = JSON.generate(input_config)
        config = JSON.parse(json_string, :symbolize_names => true)

        if schema
          validation = Core::ConfigValidator.validate(:base, config, schema)
          validation.warnings.each do |warning|
            Vcloud::Core.logger.warn(warning)
          end
          unless validation.valid?
            validation.errors.each do |error|
              Vcloud::Core.logger.fatal(error)
            end
            raise("Supplied configuration does not match supplied schema")
          end
        end

        config
      end

    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
vcloud-core-2.2.0 lib/vcloud/core/config_loader.rb
vcloud-core-2.1.1 lib/vcloud/core/config_loader.rb
vcloud-core-2.1.0 lib/vcloud/core/config_loader.rb
vcloud-core-2.0.1 lib/vcloud/core/config_loader.rb
vcloud-core-2.0.0 lib/vcloud/core/config_loader.rb
vcloud-core-1.2.0 lib/vcloud/core/config_loader.rb
vcloud-core-1.1.0 lib/vcloud/core/config_loader.rb