Sha256: 6177f173276a14d20795a1d67e7d7f5f7a3cd6cd61deb9ebcca18a28bb9bfb56

Contents?: true

Size: 1.75 KB

Versions: 54

Compression:

Stored size: 1.75 KB

Contents

require 'yaml'
require 'dotenv'

module DockerSync
  module ConfigSerializer
    class << self
      # @param [String] config_path path to the yaml configuration to load
      # @return [Object] returns a Yaml hashmap, expaneded by ENV vars
      def default_deserializer_file(config_path)
        config_string = File.read(config_path)
        default_deserializer_string(config_string)
      end

      # @param [String] config_string the configuration string inf yaml format
      # @return [Object] a yaml hashmap
      def default_deserializer_string(config_string)
        deserialize_config( expand_env_variables(config_string) )
      end

      private

      # Replaces our tokens, in this case all ENV variables we defined. Find those in the string an replace
      # them with then values from our ENV, including the dotenv file
      # @param [String] config_string
      # @return [String]
      def expand_env_variables(config_string)
        load_dotenv

        env_hash = {}
        ENV.each {|k,v| env_hash[k.to_sym] = v }
        config_string.gsub!('${', '%{')
        config_string % env_hash
      end


      # deserializes the configuration string, right now as a yaml formatted string
      # @param [String] config_string
      # @return [Object]
      def deserialize_config(config_string)
        # noinspection RubyResolve
        YAML.load(config_string)
      end

      # Loads the dotenv file but also lets us overide the source not being .env but anything you put
      # into the ENV variable DOCKER_SYNC_ENV_FILE
      # @return [Object]
      def load_dotenv
        # TODO: ensure we do this once only
        env_file = ENV.fetch('DOCKER_SYNC_ENV_FILE', '.env')

        # noinspection RubyResolve
        Dotenv.load(env_file)
      end
    end
  end
end

Version data entries

54 entries across 54 versions & 1 rubygems

Version Path
docker-sync-1.0.5 lib/docker-sync/config/config_serializer.rb
docker-sync-1.0.4 lib/docker-sync/config/config_serializer.rb
docker-sync-1.0.3 lib/docker-sync/config/config_serializer.rb
docker-sync-1.0.2 lib/docker-sync/config/config_serializer.rb
docker-sync-1.0.1 lib/docker-sync/config/config_serializer.rb
docker-sync-1.0.0 lib/docker-sync/config/config_serializer.rb
docker-sync-0.7.2 lib/docker-sync/config/config_serializer.rb
docker-sync-0.7.1 lib/docker-sync/config/config_serializer.rb
docker-sync-0.7.0 lib/docker-sync/config/config_serializer.rb
docker-sync-0.6.0 lib/docker-sync/config/config_serializer.rb
docker-sync-0.5.14 lib/docker-sync/config/config_serializer.rb
docker-sync-0.5.13 lib/docker-sync/config/config_serializer.rb
docker-sync-0.5.11 lib/docker-sync/config/config_serializer.rb
docker-sync-0.5.11.pre.beta3 lib/docker-sync/config/config_serializer.rb
docker-sync-0.5.11.pre.beta2 lib/docker-sync/config/config_serializer.rb
docker-sync-0.5.11.pre.beta1 lib/docker-sync/config/config_serializer.rb
docker-sync-0.5.10 lib/docker-sync/config/config_serializer.rb
docker-sync-0.5.10.pre.beta1 lib/docker-sync/config/config_serializer.rb
docker-sync-0.5.9 lib/docker-sync/config/config_serializer.rb
docker-sync-0.5.8 lib/docker-sync/config/config_serializer.rb