dotenv 2.7.3 → 2.8.0

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: b740f417d40477078789125a03eafd06c1c8398a014f5760e609238d661c7a24
4
- data.tar.gz: c7a53ef47fb24998a9ed63bb4ef7565d4f10040d187827f23ccb4e9e162168d9
3
+ metadata.gz: 53a782f20ccb8d0ed6e70eb3f6e626313a96460297e75dfc02356da40bea9ef1
4
+ data.tar.gz: 7dee9ae1c761ad5f069883443f8ccb0c3e8540187e432fc184783c88f1844ac6
5
5
  SHA512:
6
- metadata.gz: dc1efb88938da1dca0bd4f7d50296177b23bb349f0c3aa566b284fff950e83bfd1827e9c9fd6bab008ea4e5811d91777920445ecc4b59312b42ac62e2898cde4
7
- data.tar.gz: ca8827d32698f274232af990871b7e8396d9a6c0c7b9887fe28ab0af10d3ec1fb7c9ccefe6d228436103c03e5a7a3f259a2bf9ced2e8523699befecc3de4936b
6
+ metadata.gz: 0425caeb0f6321954bc8731438793f3d24306b72e0bf17c7de14e45c093bd2af892899d9b91780e0b688371e5db03a819eead8fe2475a7550fb1fc4762989e23
7
+ data.tar.gz: 52414dd28acab5d41879dc1740c08f8fe4265c66476dd04e1580291104701c0c5b5dcda87575d9aa4433f92587efcee87bf3aa7bb32eab35685b2fa2dd580855
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
  ```
@@ -62,7 +65,7 @@ Dotenv.load
62
65
 
63
66
  By default, `load` will look for a file called `.env` in the current working directory. Pass in multiple files and they will be loaded in order. The first value set for a variable will win.
64
67
 
65
- ```
68
+ ```ruby
66
69
  require 'dotenv'
67
70
  Dotenv.load('file1.env', 'file2.env')
68
71
  ```
@@ -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) if @argv.present?
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
@@ -12,21 +12,21 @@ module Dotenv
12
12
  [Dotenv::Substitutions::Variable, Dotenv::Substitutions::Command]
13
13
 
14
14
  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
- '(?:\\'|[^'])*' # single quoted value
22
- | # or
23
- "(?:\\"|[^"])*" # 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
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
30
30
  /x
31
31
 
32
32
  class << self
@@ -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)
@@ -92,7 +84,26 @@ module Dotenv
92
84
  end
93
85
 
94
86
  def variable_not_set?(line)
95
- !line.split[1..-1].all? { |var| @hash.member?(var) }
87
+ !line.split[1..].all? { |var| @hash.member?(var) }
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
96
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
@@ -28,7 +28,7 @@ module Dotenv
28
28
 
29
29
  if $LAST_MATCH_INFO[:backslash]
30
30
  # Command is escaped, don't replace it.
31
- $LAST_MATCH_INFO[0][1..-1]
31
+ $LAST_MATCH_INFO[0][1..]
32
32
  else
33
33
  # Execute the command and return the value
34
34
  `#{command}`.chomp
@@ -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,8 +29,8 @@ module Dotenv
33
29
  private
34
30
 
35
31
  def substitute(match, variable, env)
36
- if match[1] == '\\'
37
- variable[1..-1]
32
+ if match[1] == "\\"
33
+ variable[1..]
38
34
  elsif match[3]
39
35
  env.fetch(match[3], "")
40
36
  else
@@ -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.3".freeze
2
+ VERSION = "2.8.0".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.3
4
+ version: 2.8.0
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-06-22 00:00:00.000000000 Z
11
+ date: 2022-07-26 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: []