Sha256: 797cc16c6ac5247e7ebea1432c9546d9723ee603a616c5414977cff8524fb1bb

Contents?: true

Size: 1.95 KB

Versions: 4

Compression:

Stored size: 1.95 KB

Contents

# frozen_string_literal: true

require 'hako/env_provider'
require 'yaml'

module Hako
  module EnvProviders
    class Yaml < EnvProvider
      # @param [Pathname] root_path
      # @param [Hash<String, Object>] options
      def initialize(root_path, options) # rubocop:disable Lint/MissingSuper
        unless options['path']
          validation_error!('path must be set')
        end

        @yaml = YAML.load_file root_path.join(options['path'])

        unless @yaml.is_a?(Hash)
          validation_error!('Env yaml root must be Hash')
        end

        @options = options
      end

      # @param [Array<String>] variables
      # @return [Hash<String, String>]
      def ask(variables)
        env = {}
        read_from_yaml do |key, val|
          if variables.include?(key)
            env[key] = val
          end
        end
        env
      end

      # @return [Boolean]
      def can_ask_keys?
        true
      end

      # @param [Array<String>] variables
      # @return [Array<String>]
      def ask_keys(variables)
        keys = []
        read_from_yaml do |key, _|
          if variables.include?(key)
            keys << key
          end
        end
        keys
      end

      private

      # @yieldparam [String] key
      # @yieldparam [String] val
      def read_from_yaml(&block)
        flatten(@yaml).each(&block)
      end

      # @param [Object] obj
      # @param [String] root
      # @param [Hash<String,String>] acc
      # @return [Hash<String, String>]
      def flatten(obj, root = nil, acc = {})
        case obj
        when Array
          ary_sep = @options.fetch('ary_sep', ',')
          acc[root] = obj.join(ary_sep)
        when Hash
          obj.each do |key, value|
            key_sep = @options.fetch('key_sep', '.')
            new_root = [root, key].reject(&:nil?).join(key_sep)
            flatten(value, new_root, acc)
          end
        else
          acc[root] = obj.to_s
        end

        acc
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
hako-2.17.0 lib/hako/env_providers/yaml.rb
hako-2.16.0 lib/hako/env_providers/yaml.rb
hako-2.15.1 lib/hako/env_providers/yaml.rb
hako-2.15.0 lib/hako/env_providers/yaml.rb