Class: Wip::DotenvLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/wip/dotenv_loader.rb

Overview

Parses a .env file the way docker compose does, so values don't have to be duplicated into wip.yml just to reach the container as -e flags.

Constant Summary collapse

LINE_PATTERN =
/\A(?:export\s+)?([A-Za-z_][A-Za-z0-9_]*)=(.*)\z/

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ DotenvLoader

Returns a new instance of DotenvLoader.



11
12
13
# File 'lib/wip/dotenv_loader.rb', line 11

def initialize(path)
  @path = Pathname(path)
end

Instance Method Details

#loadObject



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/wip/dotenv_loader.rb', line 15

def load
  return {} unless @path.file?

  @path.readlines.each_with_object({}) do |line, env|
    line = line.strip
    next if line.empty? || line.start_with?('#')

    match = LINE_PATTERN.match(line)
    next unless match

    env[match[1]] = unquote(match[2])
  end
end