Class: Utcp::Utils::EnvLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/utcp/utils/env_loader.rb

Class Method Summary collapse

Class Method Details

.load_file(path = ".env") ⇒ Object

Loads simple KEY=VALUE pairs into ENV (no interpolation here)



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/utcp/utils/env_loader.rb', line 7

def self.load_file(path = ".env")
  return {} unless File.file?(path)
  vars = {}
  File.readlines(path, chomp: true).each do |line|
    next if line.strip.empty? || line.strip.start_with?("#")
    key, value = line.split("=", 2)
    next unless key
    value ||= ""
    value = value.strip
    # remove optional surrounding quotes
    value = value.gsub(/\A"(.*)"\z/, '\1')
    value = value.gsub(/\A'(.*)'\z/, '\1')
    key = key.strip
    ENV[key] = value
    vars[key] = value
  end
  vars
end