-
Notifications
You must be signed in to change notification settings - Fork 500
/
dotenv.rb
86 lines (74 loc) · 2.07 KB
/
dotenv.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
require "dotenv/parser"
require "dotenv/environment"
require "dotenv/missing_keys"
# The top level Dotenv module. The entrypoint for the application logic.
module Dotenv
class << self
attr_accessor :instrumenter
end
module_function
def load(*filenames)
with(*filenames) do |f|
ignoring_nonexistent_files do
env = Environment.new(f, true)
instrument("dotenv.load", env: env) { env.apply }
end
end
end
# same as `load`, but raises Errno::ENOENT if any files don't exist
def load!(*filenames)
with(*filenames) do |f|
env = Environment.new(f, true)
instrument("dotenv.load", env: env) { env.apply }
end
end
# same as `load`, but will override existing values in `ENV`
def overload(*filenames)
with(*filenames.reverse) do |f|
ignoring_nonexistent_files do
env = Environment.new(f, false)
instrument("dotenv.overload", env: env) { env.apply! }
end
end
end
# same as `overload`, but raises Errno::ENOENT if any files don't exist
def overload!(*filenames)
with(*filenames.reverse) do |f|
env = Environment.new(f, false)
instrument("dotenv.overload", env: env) { env.apply! }
end
end
# returns a hash of parsed key/value pairs but does not modify ENV
def parse(*filenames)
with(*filenames) do |f|
ignoring_nonexistent_files do
Environment.new(f, false)
end
end
end
# Internal: Helper to expand list of filenames.
#
# Returns a hash of all the loaded environment variables.
def with(*filenames)
filenames << ".env" if filenames.empty?
filenames.reduce({}) do |hash, filename|
hash.merge!(yield(File.expand_path(filename)) || {})
end
end
def instrument(name, payload = {}, &block)
if instrumenter
instrumenter.instrument(name, payload, &block)
else
yield
end
end
def require_keys(*keys)
missing_keys = keys.flatten - ::ENV.keys
return if missing_keys.empty?
raise MissingKeys, missing_keys
end
def ignoring_nonexistent_files
yield
rescue Errno::ENOENT
end
end