dotenv 3.1.4 → 3.1.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/lib/dotenv/parser.rb +54 -59
- data/lib/dotenv/substitutions/command.rb +1 -1
- data/lib/dotenv/substitutions/variable.rb +9 -15
- data/lib/dotenv/template.rb +1 -1
- data/lib/dotenv/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: '0538a4a253670835a8677a2aedfd452bf30ddd8518eef0ca6a0ba448bf7ddebf'
|
4
|
+
data.tar.gz: 6070c643cedccf246d9e24304a42da54736a573f1c15da6343a061f1f525e1be
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f5206862c11d4ee45343f1d1d9a66570f82df8fdf16fb403bd354d20de56de6958d49b1061688a18fbf2cbd5f06f5aabd7d3b073bfa7f3469523b8fff2495e33
|
7
|
+
data.tar.gz: d25a177df394c9050b9d0c10d06f929bf652ef24f2b7599841734ae92d7e1ebbe388400aec5adfb36eb14e33088a723af969792a966b8cffa48b11da63596bbd
|
data/README.md
CHANGED
@@ -213,7 +213,7 @@ DATABASE_URL="postgres://$(whoami)@localhost/my_database"
|
|
213
213
|
|
214
214
|
### Variable Substitution
|
215
215
|
|
216
|
-
You need to add the value of another variable in one of your variables? You can reference the variable with `${VAR}` or often just `$VAR` in
|
216
|
+
You need to add the value of another variable in one of your variables? You can reference the variable with `${VAR}` or often just `$VAR` in unquoted or double-quoted values.
|
217
217
|
|
218
218
|
```shell
|
219
219
|
DATABASE_URL="postgres://${USER}@localhost/my_database"
|
data/lib/dotenv/parser.rb
CHANGED
@@ -8,25 +8,29 @@ module Dotenv
|
|
8
8
|
# Parses the `.env` file format into key/value pairs.
|
9
9
|
# It allows for variable substitutions, command substitutions, and exporting of variables.
|
10
10
|
class Parser
|
11
|
-
@substitutions =
|
12
|
-
|
11
|
+
@substitutions = [
|
12
|
+
Dotenv::Substitutions::Variable,
|
13
|
+
Dotenv::Substitutions::Command
|
14
|
+
]
|
13
15
|
|
14
16
|
LINE = /
|
15
|
-
(?:^|\A)
|
16
|
-
\s*
|
17
|
-
(
|
18
|
-
([\w.]+)
|
19
|
-
(
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
17
|
+
(?:^|\A) # beginning of line
|
18
|
+
\s* # leading whitespace
|
19
|
+
(?<export>export\s+)? # optional export
|
20
|
+
(?<key>[\w.]+) # key
|
21
|
+
(?: # optional separator and value
|
22
|
+
(?:\s*=\s*?|:\s+?) # separator
|
23
|
+
(?<value> # optional value begin
|
24
|
+
\s*'(?:\\'|[^'])*' # single quoted value
|
25
|
+
| # or
|
26
|
+
\s*"(?:\\"|[^"])*" # double quoted value
|
27
|
+
| # or
|
28
|
+
[^\#\n]+ # unquoted value
|
29
|
+
)? # value end
|
30
|
+
)? # separator and value end
|
31
|
+
\s* # trailing whitespace
|
32
|
+
(?:\#.*)? # optional comment
|
33
|
+
(?:$|\z) # end of line
|
30
34
|
/x
|
31
35
|
|
32
36
|
class << self
|
@@ -38,41 +42,55 @@ module Dotenv
|
|
38
42
|
end
|
39
43
|
|
40
44
|
def initialize(string, overwrite: false)
|
41
|
-
|
45
|
+
# Convert line breaks to same format
|
46
|
+
@string = string.gsub(/[\n\r]+/, "\n")
|
42
47
|
@hash = {}
|
43
48
|
@overwrite = overwrite
|
44
49
|
end
|
45
50
|
|
46
51
|
def call
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
52
|
+
@string.scan(LINE) do
|
53
|
+
match = $LAST_MATCH_INFO
|
54
|
+
|
55
|
+
# Skip parsing values that will be ignored
|
56
|
+
next if ignore?(match[:key])
|
57
|
+
|
58
|
+
# Check for exported variable with no value
|
59
|
+
if match[:export] && !match[:value]
|
60
|
+
if !@hash.member?(match[:key])
|
61
|
+
raise FormatError, "Line #{match.to_s.inspect} has an unset variable"
|
62
|
+
end
|
63
|
+
else
|
64
|
+
@hash[match[:key]] = parse_value(match[:value] || "")
|
65
|
+
end
|
56
66
|
end
|
67
|
+
|
57
68
|
@hash
|
58
69
|
end
|
59
70
|
|
60
71
|
private
|
61
72
|
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
raise FormatError, "Line #{line.inspect} has an unset variable"
|
66
|
-
end
|
67
|
-
end
|
73
|
+
# Determine if the key can be ignored.
|
74
|
+
def ignore?(key)
|
75
|
+
!@overwrite && key != "DOTENV_LINEBREAK_MODE" && ENV.key?(key)
|
68
76
|
end
|
69
77
|
|
78
|
+
QUOTED_STRING = /\A(['"])(.*)\1\z/m
|
70
79
|
def parse_value(value)
|
71
80
|
# Remove surrounding quotes
|
72
|
-
value = value.strip.sub(
|
81
|
+
value = value.strip.sub(QUOTED_STRING, '\2')
|
73
82
|
maybe_quote = Regexp.last_match(1)
|
74
|
-
|
75
|
-
|
83
|
+
|
84
|
+
# Expand new lines in double quoted values
|
85
|
+
value = expand_newlines(value) if maybe_quote == '"'
|
86
|
+
|
87
|
+
# Unescape characters and performs substitutions unless value is single quoted
|
88
|
+
if maybe_quote != "'"
|
89
|
+
value = unescape_characters(value)
|
90
|
+
self.class.substitutions.each { |proc| value = proc.call(value, @hash) }
|
91
|
+
end
|
92
|
+
|
93
|
+
value
|
76
94
|
end
|
77
95
|
|
78
96
|
def unescape_characters(value)
|
@@ -86,28 +104,5 @@ module Dotenv
|
|
86
104
|
value.gsub('\n', "\\\\\\n").gsub('\r', "\\\\\\r")
|
87
105
|
end
|
88
106
|
end
|
89
|
-
|
90
|
-
def variable_not_set?(line)
|
91
|
-
!line.split[1..].all? { |var| @hash.member?(var) }
|
92
|
-
end
|
93
|
-
|
94
|
-
def unescape_value(value, maybe_quote)
|
95
|
-
if maybe_quote == '"'
|
96
|
-
unescape_characters(expand_newlines(value))
|
97
|
-
elsif maybe_quote.nil?
|
98
|
-
unescape_characters(value)
|
99
|
-
else
|
100
|
-
value
|
101
|
-
end
|
102
|
-
end
|
103
|
-
|
104
|
-
def perform_substitutions(value, maybe_quote)
|
105
|
-
if maybe_quote != "'"
|
106
|
-
self.class.substitutions.each do |proc|
|
107
|
-
value = proc.call(value, @hash, overwrite: @overwrite)
|
108
|
-
end
|
109
|
-
end
|
110
|
-
value
|
111
|
-
end
|
112
107
|
end
|
113
108
|
end
|
@@ -12,29 +12,23 @@ module Dotenv
|
|
12
12
|
VARIABLE = /
|
13
13
|
(\\)? # is it escaped with a backslash?
|
14
14
|
(\$) # literal $
|
15
|
-
(?!\() #
|
15
|
+
(?!\() # shouldn't be followed by parenthesis
|
16
16
|
\{? # allow brace wrapping
|
17
17
|
([A-Z0-9_]+)? # optional alpha nums
|
18
18
|
\}? # closing brace
|
19
19
|
/xi
|
20
20
|
|
21
|
-
def call(value, env
|
22
|
-
combined_env = overwrite ? ENV.to_h.merge(env) : env.merge(ENV)
|
21
|
+
def call(value, env)
|
23
22
|
value.gsub(VARIABLE) do |variable|
|
24
23
|
match = $LAST_MATCH_INFO
|
25
|
-
substitute(match, variable, combined_env)
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
private
|
30
24
|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
25
|
+
if match[1] == "\\"
|
26
|
+
variable[1..]
|
27
|
+
elsif match[3]
|
28
|
+
env[match[3]] || ENV[match[3]] || ""
|
29
|
+
else
|
30
|
+
variable
|
31
|
+
end
|
38
32
|
end
|
39
33
|
end
|
40
34
|
end
|
data/lib/dotenv/template.rb
CHANGED
data/lib/dotenv/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dotenv
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.1.
|
4
|
+
version: 3.1.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Brandon Keepers
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-
|
11
|
+
date: 2024-12-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -100,7 +100,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
100
100
|
- !ruby/object:Gem::Version
|
101
101
|
version: '0'
|
102
102
|
requirements: []
|
103
|
-
rubygems_version: 3.5.
|
103
|
+
rubygems_version: 3.5.22
|
104
104
|
signing_key:
|
105
105
|
specification_version: 4
|
106
106
|
summary: Loads environment variables from `.env`.
|