dotenv 2.1.0 → 2.2.0

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
  SHA1:
3
- metadata.gz: 97b35237fb293817afb2b3fb79b08b21a5425b65
4
- data.tar.gz: f1ec39284e95bcf5a9b96e5cf3cd4aaa1e4d9e67
3
+ metadata.gz: 709d168c1067da71893c77f0095e2926234c1563
4
+ data.tar.gz: f5dfdff21d1c187969960c117f4e4c34b50c586b
5
5
  SHA512:
6
- metadata.gz: aab63b855e90cb05ceedcc3877f425e2af3cdb94327423f3ce8f253b5da5e392a64f5b00b9114480ed83b42ad72a3ffdc1ec2bfaa0f3d94fed1c5d0858c0d459
7
- data.tar.gz: 4e097555c5daa7e2bcb800d771da52936298906ba5e3affa6b5835c50921690191d2610ceae5a1de329fa51ed64f494ea66ff5d80cc917fd34e5065019c73d4c
6
+ metadata.gz: 171cc72b428e5c94ce1d212799374473892c17c18f2ec71ffac6a52561fc0cd1b35919e2fd4450efec15b454bca794b26c427ef7ccecea56cb3544171db1bd45
7
+ data.tar.gz: 7e09916cd51b2f1e42f8fee2814c2a11c9fc72d4abb30b4e4dcfb311776ca9180fbd68cb731696ceb48802adb1c24ce3d9af0a100c3ab2cf9ba05e5596b8f7d5
data/README.md CHANGED
@@ -15,7 +15,7 @@ But it is not always practical to set environment variables on development machi
15
15
  Add this line to the top of your application's Gemfile:
16
16
 
17
17
  ```ruby
18
- gem 'dotenv-rails', :groups => [:development, :test]
18
+ gem 'dotenv-rails', groups: [:development, :test]
19
19
  ```
20
20
 
21
21
  And then execute:
@@ -40,7 +40,7 @@ HOSTNAME = ENV['HOSTNAME']
40
40
  If you use gems that require environment variables to be set before they are loaded, then list `dotenv-rails` in the `Gemfile` before those other gems and require `dotenv/rails-now`.
41
41
 
42
42
  ```ruby
43
- gem 'dotenv-rails', :require => 'dotenv/rails-now'
43
+ gem 'dotenv-rails', require: 'dotenv/rails-now'
44
44
  gem 'gem-that-requires-env-variables'
45
45
  ```
46
46
 
@@ -55,10 +55,20 @@ $ gem install dotenv
55
55
  As early as possible in your application bootstrap process, load `.env`:
56
56
 
57
57
  ```ruby
58
+ require 'dotenv/load'
59
+
60
+ # or
58
61
  require 'dotenv'
59
62
  Dotenv.load
60
63
  ```
61
64
 
65
+ 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.
66
+
67
+ ```
68
+ require 'dotenv'
69
+ Dotenv.load('file1.env', 'file2.env')
70
+ ```
71
+
62
72
  Alternatively, you can use the `dotenv` executable to launch your application:
63
73
 
64
74
  ```shell
@@ -70,7 +80,7 @@ To ensure `.env` is loaded in rake, load the tasks:
70
80
  ```ruby
71
81
  require 'dotenv/tasks'
72
82
 
73
- task :mytask => :dotenv do
83
+ task mytask: :dotenv do
74
84
  # things that require .env
75
85
  end
76
86
  ```
@@ -84,30 +94,51 @@ S3_BUCKET=YOURS3BUCKET
84
94
  SECRET_KEY=YOURSECRETKEYGOESHERE
85
95
  ```
86
96
 
97
+ Whenever your application loads, these variables will be available in `ENV`:
98
+
99
+ ```ruby
100
+ config.fog_directory = ENV['S3_BUCKET']
101
+ ```
102
+
103
+ You may also add `export` in front of each line so you can `source` the file in bash:
104
+
105
+ ```shell
106
+ export S3_BUCKET=YOURS3BUCKET
107
+ export SECRET_KEY=YOURSECRETKEYGOESHERE
108
+ ```
109
+
110
+ ### Multi-line values
111
+
87
112
  If you need multiline variables, for example private keys, you can double quote strings and use the `\n` character for newlines:
88
113
 
89
114
  ```shell
90
115
  PRIVATE_KEY="-----BEGIN RSA PRIVATE KEY-----\nHkVN9…\n-----END DSA PRIVATE KEY-----\n"
91
116
  ```
92
117
 
118
+ ### Command Substitution
119
+
93
120
  You need to add the output of a command in one of your variables? Simply add it with `$(your_command)`:
121
+
94
122
  ```shell
95
123
  DATABASE_URL="postgres://$(whoami)@localhost/my_database"
96
124
  ```
97
125
 
98
- You may also add `export` in front of each line so you can `source` the file in bash:
126
+ ### Variable Substitution
127
+
128
+ 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.
99
129
 
100
130
  ```shell
101
- export S3_BUCKET=YOURS3BUCKET
102
- export SECRET_KEY=YOURSECRETKEYGOESHERE
131
+ DATABASE_URL="postgres://${USER}@localhost/my_database"
103
132
  ```
104
133
 
105
- Whenever your application loads, these variables will be available in `ENV`:
134
+ If a value contains a `$` and it is not intended to be a variable, wrap it in single quotes.
106
135
 
107
- ```ruby
108
- config.fog_directory = ENV['S3_BUCKET']
136
+ ```shell
137
+ PASSWORD='pas$word'
109
138
  ```
110
139
 
140
+ ### Comments
141
+
111
142
  Comments may be added to your file as such:
112
143
 
113
144
  ```shell
@@ -116,24 +147,35 @@ SECRET_KEY=YOURSECRETKEYGOESHERE # comment
116
147
  SECRET_HASH="something-with-a-#-hash"
117
148
  ```
118
149
 
119
- Variable names may not contain the `#` symbol. Values can use the `#` if they are enclosed in quotes.
150
+ ## Frequently Answered Questions
120
151
 
121
- ## Multiple Rails Environments
152
+ ### Can I use dotenv in production?
122
153
 
123
154
  dotenv was originally created to load configuration variables into `ENV` in *development*. There are typically better ways to manage configuration in production environments - such as `/etc/environment` managed by [Puppet](https://github.com/puppetlabs/puppet) or [Chef](https://github.com/chef/chef), `heroku config`, etc.
124
155
 
125
156
  However, some find dotenv to be a convenient way to configure Rails applications in staging and production environments, and you can do that by defining environment-specific files like `.env.production` or `.env.test`.
126
157
 
127
- You can also use `.env.local` for local overrides.
158
+ If you use this gem to handle env vars for multiple Rails environments (development, test, production, etc.), please note that env vars that are general to all environments should be stored in `.env`. Then, environment specific env vars should be stored in `.env.<that environment's name>`.
128
159
 
129
- If you use this gem to handle env vars for multiple Rails environments (development, test, production, etc.), please note that env vars that are general to all environments should be stored in `.env`. Then, environment specific env vars should be stored in `.env.<that environment's name>`. When you load a certain environment, dotenv will first load general env vars from `.env`, then load environment specific env vars from `.env.<current environment>`. Variables defined in `.env.<current environment>` will override any values set in `.env` or already defined in the environment.
160
+ ### What other .env* files can I use?
130
161
 
131
- ## Should I commit my .env file?
162
+ `dotenv-rails` will load the following files, starting from the bottom. The first value set (or those already defined in the environment) take precedence:
163
+
164
+ - `.env` - The Original®
165
+ - `.env.development`, `.env.test`, `.env.production` - Environment-specific settings.
166
+ - `.env.local` - Local overrides. This file is loaded for all environments _except_ `test`.
167
+ - `.env.development.local`, `.env.test.local`, `.env.production.local` - Local overrides of environment-specific settings.
168
+
169
+ ### Should I commit my .env file?
132
170
 
133
171
  Credentials should only be accessible on the machines that need access to them. Never commit sensitive information to a repository that is not needed by every development machine and server.
134
172
 
135
173
  Personally, I prefer to commit the `.env` file with development-only settings. This makes it easy for other developers to get started on the project without compromising credentials for other environments. If you follow this advice, make sure that all the credentials for your development environment are different from your other deployments and that the development credentials do not have access to any confidential data.
136
174
 
175
+ ### Why is it not overriding existing `ENV` variables?
176
+
177
+ 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`.
178
+
137
179
  ## Contributing
138
180
 
139
181
  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).
@@ -14,7 +14,7 @@ module Dotenv
14
14
  end
15
15
 
16
16
  def read
17
- File.open(@filename, "rb:bom|utf-8") { |file| file.read }
17
+ File.open(@filename, "rb:bom|utf-8", &:read)
18
18
  end
19
19
 
20
20
  def apply
@@ -0,0 +1,2 @@
1
+ require "dotenv"
2
+ Dotenv.load
data/lib/dotenv/parser.rb CHANGED
@@ -9,10 +9,11 @@ module Dotenv
9
9
  # exporting of variables.
10
10
  class Parser
11
11
  @substitutions =
12
- Substitutions.constants.map { |const| Substitutions.const_get(const) }
12
+ [Dotenv::Substitutions::Variable, Dotenv::Substitutions::Command]
13
13
 
14
14
  LINE = /
15
15
  \A
16
+ \s*
16
17
  (?:export\s+)? # optional export
17
18
  ([\w\.]+) # key
18
19
  (?:\s*=\s*|:\s+?) # separator
@@ -23,7 +24,8 @@ module Dotenv
23
24
  | # or
24
25
  [^#\n]+ # unquoted value
25
26
  )? # value end
26
- (?:\s*\#.*)? # optional comment
27
+ \s*
28
+ (?:\#.*)? # optional comment
27
29
  \z
28
30
  /x
29
31
 
@@ -55,10 +57,10 @@ module Dotenv
55
57
  @hash[key] = parse_value(value || "")
56
58
  elsif line.split.first == "export"
57
59
  if variable_not_set?(line)
58
- fail FormatError, "Line #{line.inspect} has an unset variable"
60
+ raise FormatError, "Line #{line.inspect} has an unset variable"
59
61
  end
60
62
  elsif line !~ /\A\s*(?:#.*)?\z/ # not comment or blank line
61
- fail FormatError, "Line #{line.inspect} doesn't match format"
63
+ raise FormatError, "Line #{line.inspect} doesn't match format"
62
64
  end
63
65
  end
64
66
 
data/lib/dotenv/tasks.rb CHANGED
@@ -4,4 +4,4 @@ task :dotenv do
4
4
  Dotenv.load
5
5
  end
6
6
 
7
- task :environment => :dotenv
7
+ task environment: :dotenv
@@ -1,3 +1,3 @@
1
1
  module Dotenv
2
- VERSION = "2.1.0"
2
+ VERSION = "2.2.0".freeze
3
3
  end
data/lib/dotenv.rb CHANGED
@@ -13,7 +13,7 @@ module Dotenv
13
13
  with(*filenames) do |f|
14
14
  ignoring_nonexistent_files do
15
15
  env = Environment.new(f)
16
- instrument("dotenv.load", :env => env) { env.apply }
16
+ instrument("dotenv.load", env: env) { env.apply }
17
17
  end
18
18
  end
19
19
  end
@@ -22,7 +22,7 @@ module Dotenv
22
22
  def load!(*filenames)
23
23
  with(*filenames) do |f|
24
24
  env = Environment.new(f)
25
- instrument("dotenv.load", :env => env) { env.apply }
25
+ instrument("dotenv.load", env: env) { env.apply }
26
26
  end
27
27
  end
28
28
 
@@ -31,7 +31,7 @@ module Dotenv
31
31
  with(*filenames) do |f|
32
32
  ignoring_nonexistent_files do
33
33
  env = Environment.new(f)
34
- instrument("dotenv.overload", :env => env) { env.apply! }
34
+ instrument("dotenv.overload", env: env) { env.apply! }
35
35
  end
36
36
  end
37
37
  end
@@ -39,11 +39,11 @@ module Dotenv
39
39
  # Internal: Helper to expand list of filenames.
40
40
  #
41
41
  # Returns a hash of all the loaded environment variables.
42
- def with(*filenames, &block)
42
+ def with(*filenames)
43
43
  filenames << ".env" if filenames.empty?
44
44
 
45
45
  filenames.reduce({}) do |hash, filename|
46
- hash.merge! block.call(File.expand_path(filename)) || {}
46
+ hash.merge!(yield(File.expand_path(filename)) || {})
47
47
  end
48
48
  end
49
49
 
@@ -51,7 +51,7 @@ module Dotenv
51
51
  if instrumenter
52
52
  instrumenter.instrument(name, payload, &block)
53
53
  else
54
- block.call
54
+ yield
55
55
  end
56
56
  end
57
57
 
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.1.0
4
+ version: 2.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brandon Keepers
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-01-14 00:00:00.000000000 Z
11
+ date: 2017-01-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -42,16 +42,16 @@ dependencies:
42
42
  name: rubocop
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - ">="
45
+ - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '0'
47
+ version: 0.40.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'
54
+ version: 0.40.0
55
55
  description: Loads environment variables from `.env`.
56
56
  email:
57
57
  - brandon@opensoul.org
@@ -66,6 +66,7 @@ files:
66
66
  - lib/dotenv.rb
67
67
  - lib/dotenv/cli.rb
68
68
  - lib/dotenv/environment.rb
69
+ - lib/dotenv/load.rb
69
70
  - lib/dotenv/parser.rb
70
71
  - lib/dotenv/substitutions/command.rb
71
72
  - lib/dotenv/substitutions/variable.rb
@@ -91,7 +92,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
91
92
  version: '0'
92
93
  requirements: []
93
94
  rubyforge_project:
94
- rubygems_version: 2.4.5.1
95
+ rubygems_version: 2.5.2
95
96
  signing_key:
96
97
  specification_version: 4
97
98
  summary: Loads environment variables from `.env`.