dotenv 3.1.4 → 3.1.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- 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 +6 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a4ad122f937755147ff0cc52552f17b1e1cf8f9a290ff791ac5ba2f8ef8b43c7
|
4
|
+
data.tar.gz: 3116d767ea5dc67adca867395ea7effbc1b4ee30769223961f8d041d5c34482d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 86436a8b6d193590befeb48a29882b9bd5523f8b80fff961faf1feda91cc720b8b44c1e2705bfbebb73836b99684273d49f78b94c09d05a3077e62996473a47c
|
7
|
+
data.tar.gz: bb6f98a159c821e0f1cb98ab6c3f0b54a109acc3c934770fd6603f7497c047f031c7124f839963f0f5132a12bd2736e25ab2e4c648cfa56589a30b328d03de61
|
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(/\r\n?/, "\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
|
+
if existing?(match[:key])
|
56
|
+
# Use value from already defined variable
|
57
|
+
@hash[match[:key]] = ENV[match[:key]]
|
58
|
+
elsif match[:export] && !match[:value]
|
59
|
+
# Check for exported variable with no 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 a variable is already defined and should not be overwritten.
|
74
|
+
def existing?(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.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Brandon Keepers
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-
|
11
|
+
date: 2024-12-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -85,7 +85,7 @@ licenses:
|
|
85
85
|
- MIT
|
86
86
|
metadata:
|
87
87
|
changelog_uri: https://github.com/bkeepers/dotenv/releases
|
88
|
-
post_install_message:
|
88
|
+
post_install_message:
|
89
89
|
rdoc_options: []
|
90
90
|
require_paths:
|
91
91
|
- lib
|
@@ -100,8 +100,8 @@ 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.
|
104
|
-
signing_key:
|
103
|
+
rubygems_version: 3.5.22
|
104
|
+
signing_key:
|
105
105
|
specification_version: 4
|
106
106
|
summary: Loads environment variables from `.env`.
|
107
107
|
test_files: []
|