Sha256: 977a930d6a12691ee62e437084bcc0a395f50a252acffe495228c177b0c5b898

Contents?: true

Size: 1.49 KB

Versions: 18

Compression:

Stored size: 1.49 KB

Contents

# frozen_string_literal: true

require 'hako/env_provider'

module Hako
  module EnvProviders
    class File < EnvProvider
      # @param [Pathname] root_path
      # @param [Hash<String, Object>] options
      def initialize(root_path, options)
        unless options['path']
          validation_error!('path must be set')
        end
        @path = root_path.join(options['path'])
      end

      # @param [Array<String>] variables
      # @return [Hash<String, String>]
      def ask(variables)
        env = {}
        read_from_file 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_file do |key, _|
          if variables.include?(key)
            keys << key
          end
        end
        keys
      end

      private

      # @yieldparam [String] key
      # @yieldparam [String] val
      # @return [nil]
      def read_from_file(&block)
        ::File.open(@path) do |f|
          f.each_line do |line|
            line.chomp!
            line.lstrip!
            if line[0] == '#'
              # line comment
              next
            end

            key, val = line.split('=', 2)
            if val
              block.call(key, val)
            end
          end
        end
      end
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
hako-2.14.0 lib/hako/env_providers/file.rb
hako-2.13.0 lib/hako/env_providers/file.rb
hako-2.12.0 lib/hako/env_providers/file.rb
hako-2.11.1 lib/hako/env_providers/file.rb
hako-2.11.0 lib/hako/env_providers/file.rb
hako-2.10.0 lib/hako/env_providers/file.rb
hako-2.9.2 lib/hako/env_providers/file.rb
hako-2.9.1 lib/hako/env_providers/file.rb
hako-2.9.0 lib/hako/env_providers/file.rb
hako-2.8.0 lib/hako/env_providers/file.rb
hako-2.7.0 lib/hako/env_providers/file.rb
hako-2.6.2 lib/hako/env_providers/file.rb
hako-2.6.1 lib/hako/env_providers/file.rb
hako-2.6.0 lib/hako/env_providers/file.rb
hako-2.5.1 lib/hako/env_providers/file.rb
hako-2.5.0 lib/hako/env_providers/file.rb
hako-2.4.0 lib/hako/env_providers/file.rb
hako-2.3.1 lib/hako/env_providers/file.rb