dotenv 2.7.5 → 2.8.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4ae9316b6ae85fca9d3c5724f6179127fa17444b94ba27ea1000fd5677e73249
4
- data.tar.gz: da1ac6f6c94bda168a504f58244dd382012fa60ca56ba9dc44524bccc1a687b8
3
+ metadata.gz: ac1a9e8e04f95f111da23bf2541070f2617864fc3c55ac4e11e6b91c78d11a9b
4
+ data.tar.gz: 24bf24be5be7020e72877e32d8cd2d9cf022a74b182569c77b2c81c0d482696b
5
5
  SHA512:
6
- metadata.gz: b3fe0a0048d061128625f85f7f475a4f5a2d2e296906d108225275384ff22e249adc7a1e59e3f3585a912279796de06b9606dc842311e3c43c47de92561f6f99
7
- data.tar.gz: 420e753ad539b9eabc26e7b13997d0b7272ccf1c4ec1b42f2455c2531976ec07514be88ca7fd691b422c4534f46ec0de77af7a86d6220b4cf0af2c11fbc0dff4
6
+ metadata.gz: 1f8d6ae3bfd67c1b57bc4d72346d670c02570d07cee953930d6b7f9018cc053ac42624d1bc4d59dd112abe5ba711d551199ff92a2672d5434577356b0a24d0c0
7
+ data.tar.gz: 38488d0f24d6f6014f87415775f94969cd56236bcb848c77b5dd4d066285ec47993006f87f1ab1afb50a033db101fe206741ec8e561de602a88717ad6fff3bdf
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # dotenv [![Build Status](https://secure.travis-ci.org/bkeepers/dotenv.svg?branch=master)](https://travis-ci.org/bkeepers/dotenv) [![Gem Version](https://badge.fury.io/rb/dotenv.svg)](https://badge.fury.io/rb/dotenv) [![Join the chat at https://gitter.im/bkeepers/dotenv](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/bkeepers/dotenv?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
1
+ # dotenv [![Gem Version](https://badge.fury.io/rb/dotenv.svg)](https://badge.fury.io/rb/dotenv) [![Join the chat at https://gitter.im/bkeepers/dotenv](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/bkeepers/dotenv?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
2
2
 
3
3
  Shim to load environment variables from `.env` into `ENV` in *development*.
4
4
 
@@ -30,7 +30,10 @@ dotenv is initialized in your Rails app during the `before_configuration` callba
30
30
  # config/application.rb
31
31
  Bundler.require(*Rails.groups)
32
32
 
33
- Dotenv::Railtie.load
33
+ # Load dotenv only in development or test environment
34
+ if ['development', 'test'].include? ENV['RAILS_ENV']
35
+ Dotenv::Railtie.load
36
+ end
34
37
 
35
38
  HOSTNAME = ENV['HOSTNAME']
36
39
  ```
@@ -223,7 +226,7 @@ You can use the `-t` or `--template` flag on the dotenv cli to create a template
223
226
  ```shell
224
227
  $ dotenv -t .env
225
228
  ```
226
- A template will be created in your working directory named `{FINAME}.template`. So in the above example, it would create a `.env.template` file.
229
+ A template will be created in your working directory named `{FINAME}.template`. So in the above example, it would create a `.env.template` file.
227
230
 
228
231
  The template will contain all the environment variables in your `.env` file but with their values set to the variable names.
229
232
 
@@ -233,7 +236,7 @@ S3_BUCKET=YOURS3BUCKET
233
236
  SECRET_KEY=YOURSECRETKEYGOESHERE
234
237
  ```
235
238
 
236
- Would become
239
+ Would become
237
240
 
238
241
  ```shell
239
242
  # .env.template
@@ -247,6 +250,11 @@ Personally, I prefer to commit the `.env` file with development-only settings. T
247
250
 
248
251
  By default, it **won't** overwrite existing environment variables as dotenv assumes the deployment environment has more knowledge about configuration than the application does. To overwrite existing environment variables you can use `Dotenv.overload`.
249
252
 
253
+ You can also use the `-o` or `--overload` flag on the dotenv cli to override existing `ENV` variables.
254
+ ```shell
255
+ $ dotenv -o -f ".env.local,.env"
256
+ ```
257
+
250
258
  ## Contributing
251
259
 
252
260
  If you want a better idea of how dotenv works, check out the [Ruby Rogues Code Reading of dotenv](https://www.youtube.com/watch?v=lKmY_0uY86s).
data/lib/dotenv/cli.rb CHANGED
@@ -4,77 +4,54 @@ require "dotenv/template"
4
4
  require "optparse"
5
5
 
6
6
  module Dotenv
7
- # The CLI is a class responsible of handling all the command line interface
8
- # logic.
9
- class CLI
10
- attr_reader :argv, :filenames
7
+ # The command line interface
8
+ class CLI < OptionParser
9
+ attr_reader :argv, :filenames, :overload
11
10
 
12
11
  def initialize(argv = [])
13
12
  @argv = argv.dup
14
13
  @filenames = []
15
- end
14
+ @overload = false
16
15
 
17
- def run
18
- parse_argv!(@argv)
16
+ super "Usage: dotenv [options]"
17
+ separator ""
19
18
 
20
- begin
21
- Dotenv.load!(*@filenames)
22
- rescue Errno::ENOENT => e
23
- abort e.message
24
- else
25
- exec(*@argv) unless @argv.empty?
19
+ on("-f FILES", Array, "List of env files to parse") do |list|
20
+ @filenames = list
26
21
  end
27
- end
28
-
29
- private
30
-
31
- def parse_argv!(argv)
32
- parser = create_option_parser
33
- add_options(parser)
34
- parser.order!(argv)
35
-
36
- @filenames
37
- end
38
22
 
39
- def add_options(parser)
40
- add_files_option(parser)
41
- add_help_option(parser)
42
- add_version_option(parser)
43
- add_template_option(parser)
44
- end
45
-
46
- def add_files_option(parser)
47
- parser.on("-f FILES", Array, "List of env files to parse") do |list|
48
- @filenames = list
23
+ on("-o", "--overload", "override existing ENV variables") do
24
+ @overload = true
49
25
  end
50
- end
51
26
 
52
- def add_help_option(parser)
53
- parser.on("-h", "--help", "Display help") do
54
- puts parser
27
+ on("-h", "--help", "Display help") do
28
+ puts self
55
29
  exit
56
30
  end
57
- end
58
31
 
59
- def add_version_option(parser)
60
- parser.on("-v", "--version", "Show version") do
32
+ on("-v", "--version", "Show version") do
61
33
  puts "dotenv #{Dotenv::VERSION}"
62
34
  exit
63
35
  end
64
- end
65
36
 
66
- def add_template_option(parser)
67
- parser.on("-t", "--template=FILE", "Create a template env file") do |file|
37
+ on("-t", "--template=FILE", "Create a template env file") do |file|
68
38
  template = Dotenv::EnvTemplate.new(file)
69
39
  template.create_template
70
40
  end
41
+
42
+ order!(@argv)
71
43
  end
72
44
 
73
- def create_option_parser
74
- OptionParser.new do |parser|
75
- parser.banner = "Usage: dotenv [options]"
76
- parser.separator ""
45
+ def run
46
+ if @overload
47
+ Dotenv.overload!(*@filenames)
48
+ else
49
+ Dotenv.load!(*@filenames)
77
50
  end
51
+ rescue Errno::ENOENT => e
52
+ abort e.message
53
+ else
54
+ exec(*@argv) unless @argv.empty?
78
55
  end
79
56
  end
80
57
  end
@@ -3,7 +3,7 @@ module Dotenv
3
3
 
4
4
  class MissingKeys < Error # :nodoc:
5
5
  def initialize(keys)
6
- key_word = "key#{keys.size > 1 ? 's' : ''}"
6
+ key_word = "key#{keys.size > 1 ? "s" : ""}"
7
7
  super("Missing required configuration #{key_word}: #{keys.inspect}")
8
8
  end
9
9
  end
data/lib/dotenv/parser.rb CHANGED
@@ -15,7 +15,7 @@ module Dotenv
15
15
  (?:^|\A) # beginning of line
16
16
  \s* # leading whitespace
17
17
  (?:export\s+)? # optional export
18
- ([\w\.]+) # key
18
+ ([\w.]+) # key
19
19
  (?:\s*=\s*?|:\s+?) # separator
20
20
  ( # optional value begin
21
21
  \s*'(?:\\'|[^'])*' # single quoted value
@@ -70,17 +70,9 @@ module Dotenv
70
70
  def parse_value(value)
71
71
  # Remove surrounding quotes
72
72
  value = value.strip.sub(/\A(['"])(.*)\1\z/m, '\2')
73
-
74
- if Regexp.last_match(1) == '"'
75
- value = unescape_characters(expand_newlines(value))
76
- end
77
-
78
- if Regexp.last_match(1) != "'"
79
- self.class.substitutions.each do |proc|
80
- value = proc.call(value, @hash, @is_load)
81
- end
82
- end
83
- value
73
+ maybe_quote = Regexp.last_match(1)
74
+ value = unescape_value(value, maybe_quote)
75
+ perform_substitutions(value, maybe_quote)
84
76
  end
85
77
 
86
78
  def unescape_characters(value)
@@ -94,5 +86,24 @@ module Dotenv
94
86
  def variable_not_set?(line)
95
87
  !line.split[1..-1].all? { |var| @hash.member?(var) }
96
88
  end
89
+
90
+ def unescape_value(value, maybe_quote)
91
+ if maybe_quote == '"'
92
+ unescape_characters(expand_newlines(value))
93
+ elsif maybe_quote.nil?
94
+ unescape_characters(value)
95
+ else
96
+ value
97
+ end
98
+ end
99
+
100
+ def perform_substitutions(value, maybe_quote)
101
+ if maybe_quote != "'"
102
+ self.class.substitutions.each do |proc|
103
+ value = proc.call(value, @hash, @is_load)
104
+ end
105
+ end
106
+ value
107
+ end
97
108
  end
98
109
  end
@@ -13,7 +13,7 @@ module Dotenv
13
13
  \$ # literal $
14
14
  (?<cmd> # collect command content for eval
15
15
  \( # require opening paren
16
- ([^()]|\g<cmd>)+ # allow any number of non-parens, or balanced
16
+ (?:[^()]|\g<cmd>)+ # allow any number of non-parens, or balanced
17
17
  # parens (by nesting the <cmd> expression
18
18
  # recursively)
19
19
  \) # require closing paren
@@ -19,11 +19,7 @@ module Dotenv
19
19
  /xi
20
20
 
21
21
  def call(value, env, is_load)
22
- combined_env = if is_load
23
- env.merge(ENV)
24
- else
25
- ENV.to_h.merge(env)
26
- end
22
+ combined_env = is_load ? env.merge(ENV) : ENV.to_h.merge(env)
27
23
  value.gsub(VARIABLE) do |variable|
28
24
  match = $LAST_MATCH_INFO
29
25
  substitute(match, variable, combined_env)
@@ -33,7 +29,7 @@ module Dotenv
33
29
  private
34
30
 
35
31
  def substitute(match, variable, env)
36
- if match[1] == '\\'
32
+ if match[1] == "\\"
37
33
  variable[1..-1]
38
34
  elsif match[3]
39
35
  env.fetch(match[3], "")
@@ -1,4 +1,5 @@
1
1
  module Dotenv
2
+ EXPORT_COMMAND = "export ".freeze
2
3
  # Class for creating a template from a env file
3
4
  class EnvTemplate
4
5
  def initialize(env_file)
@@ -9,11 +10,17 @@ module Dotenv
9
10
  File.open(@env_file, "r") do |env_file|
10
11
  File.open("#{@env_file}.template", "w") do |env_template|
11
12
  env_file.each do |line|
12
- variable = line.split("=").first
13
- env_template.puts "#{variable}=#{variable}"
13
+ env_template.puts template_line(line)
14
14
  end
15
15
  end
16
16
  end
17
17
  end
18
+
19
+ def template_line(line)
20
+ var, value = line.split("=")
21
+ template = var.gsub(EXPORT_COMMAND, "")
22
+ is_a_comment = var.strip[0].eql?("#")
23
+ value.nil? || is_a_comment ? line : "#{var}=#{template}"
24
+ end
18
25
  end
19
26
  end
@@ -1,3 +1,3 @@
1
1
  module Dotenv
2
- VERSION = "2.7.5".freeze
2
+ VERSION = "2.8.1".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: 2.7.5
4
+ version: 2.8.1
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: 2019-07-31 00:00:00.000000000 Z
11
+ date: 2022-07-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -39,19 +39,19 @@ dependencies:
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
- name: rubocop
42
+ name: standard
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - "~>"
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
- version: 0.40.0
47
+ version: '0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - "~>"
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
- version: 0.40.0
54
+ version: '0'
55
55
  description: Loads environment variables from `.env`.
56
56
  email:
57
57
  - brandon@opensoul.org
@@ -78,7 +78,7 @@ homepage: https://github.com/bkeepers/dotenv
78
78
  licenses:
79
79
  - MIT
80
80
  metadata: {}
81
- post_install_message:
81
+ post_install_message:
82
82
  rdoc_options: []
83
83
  require_paths:
84
84
  - lib
@@ -93,8 +93,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
93
93
  - !ruby/object:Gem::Version
94
94
  version: '0'
95
95
  requirements: []
96
- rubygems_version: 3.0.1
97
- signing_key:
96
+ rubygems_version: 3.2.32
97
+ signing_key:
98
98
  specification_version: 4
99
99
  summary: Loads environment variables from `.env`.
100
100
  test_files: []