Module: Githuh::EnvLoader

Defined in:
lib/githuh/env_loader.rb

Class Method Summary collapse

Class Method Details

.load!Object

Load a simple KEY=VALUE .env file from the current working directory and/or HOME, without clobbering already-set ENV entries.



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

def self.load!
  [File.join(Dir.pwd, '.env'), File.join(Dir.home, '.env')].uniq.each do |path|
    next unless File.file?(path) && File.readable?(path)

    File.foreach(path) do |raw|
      line = raw.strip
      next if line.empty? || line.start_with?('#')

      key, value = line.split('=', 2)
      next if key.nil? || value.nil?

      key = key.strip
      value = strip_quotes(value.strip)
      ENV[key] ||= value
    end
  end
end

.strip_quotes(value) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/githuh/env_loader.rb', line 25

def self.strip_quotes(value)
  if (value.start_with?('"') && value.end_with?('"')) ||
     (value.start_with?("'") && value.end_with?("'"))
    value[1..-2]
  else
    value
  end
end