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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bbefe05cecd5a12cdd9a9dc72b3ff2feee883c10f84c40c9105d3d9c1c5ffc3c
4
- data.tar.gz: a61066d9119426fa542acf6a17d7134033d17cef8bdf71fe87e964a5d4b5e925
3
+ metadata.gz: '0538a4a253670835a8677a2aedfd452bf30ddd8518eef0ca6a0ba448bf7ddebf'
4
+ data.tar.gz: 6070c643cedccf246d9e24304a42da54736a573f1c15da6343a061f1f525e1be
5
5
  SHA512:
6
- metadata.gz: 9941e0374f811e9048db77c77731106b1b3f75eff0358d6c2a8ef24265857ccfb02d76409f733f1f930815f4b032e5939520ee6c0a48d3eaa00d734be036eb4b
7
- data.tar.gz: 83b856b11c9817f51151e4249a1fb5fd38b057720a049270ffa7fcf05a20547ce2db29e6690feb8f536478b0995549eaa52a90b5d1a019af5aa73ce6def3e561
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 unqoted or double-quoted values.
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
- [Dotenv::Substitutions::Variable, Dotenv::Substitutions::Command]
11
+ @substitutions = [
12
+ Dotenv::Substitutions::Variable,
13
+ Dotenv::Substitutions::Command
14
+ ]
13
15
 
14
16
  LINE = /
15
- (?:^|\A) # beginning of line
16
- \s* # leading whitespace
17
- (?:export\s+)? # optional export
18
- ([\w.]+) # key
19
- (?:\s*=\s*?|:\s+?) # separator
20
- ( # optional value begin
21
- \s*'(?:\\'|[^'])*' # single quoted value
22
- | # or
23
- \s*"(?:\\"|[^"])*" # double quoted value
24
- | # or
25
- [^\#\r\n]+ # unquoted value
26
- )? # value end
27
- \s* # trailing whitespace
28
- (?:\#.*)? # optional comment
29
- (?:$|\z) # end of line
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
- @string = string
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
- # Convert line breaks to same format
48
- lines = @string.gsub(/\r\n?/, "\n")
49
- # Process matches
50
- lines.scan(LINE).each do |key, value|
51
- @hash[key] = parse_value(value || "")
52
- end
53
- # Process non-matches
54
- lines.gsub(LINE, "").split(/[\n\r]+/).each do |line|
55
- parse_line(line)
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
- def parse_line(line)
63
- if line.split.first == "export"
64
- if variable_not_set?(line)
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(/\A(['"])(.*)\1\z/m, '\2')
81
+ value = value.strip.sub(QUOTED_STRING, '\2')
73
82
  maybe_quote = Regexp.last_match(1)
74
- value = unescape_value(value, maybe_quote)
75
- perform_substitutions(value, maybe_quote)
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
@@ -20,7 +20,7 @@ module Dotenv
20
20
  )
21
21
  /x
22
22
 
23
- def call(value, _env, overwrite: false)
23
+ def call(value, _env)
24
24
  # Process interpolated shell commands
25
25
  value.gsub(INTERPOLATED_SHELL_COMMAND) do |*|
26
26
  # Eliminate opening and closing parentheses
@@ -12,29 +12,23 @@ module Dotenv
12
12
  VARIABLE = /
13
13
  (\\)? # is it escaped with a backslash?
14
14
  (\$) # literal $
15
- (?!\() # shouldnt be followed by paranthesis
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, overwrite: false)
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
- def substitute(match, variable, env)
32
- if match[1] == "\\"
33
- variable[1..]
34
- elsif match[3]
35
- env.fetch(match[3], "")
36
- else
37
- variable
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
@@ -34,7 +34,7 @@ module Dotenv
34
34
 
35
35
  def var_defined?(line)
36
36
  match = Dotenv::Parser::LINE.match(line)
37
- match && match[1]
37
+ match && match[:key]
38
38
  end
39
39
 
40
40
  def line_blank?(line)
@@ -1,3 +1,3 @@
1
1
  module Dotenv
2
- VERSION = "3.1.4".freeze
2
+ VERSION = "3.1.5".freeze
3
3
  end
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
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-09-18 00:00:00.000000000 Z
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.16
103
+ rubygems_version: 3.5.22
104
104
  signing_key:
105
105
  specification_version: 4
106
106
  summary: Loads environment variables from `.env`.