dotenv-rails 2.2.2 → 2.8.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/README.md +90 -11
- data/lib/dotenv/rails.rb +16 -6
- metadata +8 -15
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 7d0c13519d33404b622266fbf7ff71079a199716ac564e4a2729384b15b970d7
|
4
|
+
data.tar.gz: f2da97acce1fb1e953e000114f9f01edb596adb0676b20a1e11cc8d77e6c405b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8d2102375cd88ce4b90c44629d0976062f9d9ebe05f95e480f38f6dcd509c28d23d8dff295339e2f4870263e595fcff6f45d6f972fee2e00a67a8b946979032a
|
7
|
+
data.tar.gz: 420d8614065db0398d72e13548c53ee25aa1da6d1676a96a7a0f93dbe33aca24e5ea4c48073b37ee29645a0bbcc56e6082289bd9473e37ade4f395eec2176099
|
data/README.md
CHANGED
@@ -1,6 +1,4 @@
|
|
1
|
-
# dotenv [![
|
2
|
-
|
3
|
-
[![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)
|
4
2
|
|
5
3
|
Shim to load environment variables from `.env` into `ENV` in *development*.
|
6
4
|
|
@@ -32,7 +30,10 @@ dotenv is initialized in your Rails app during the `before_configuration` callba
|
|
32
30
|
# config/application.rb
|
33
31
|
Bundler.require(*Rails.groups)
|
34
32
|
|
35
|
-
|
33
|
+
# Load dotenv only in development or test environment
|
34
|
+
if ['development', 'test'].include? ENV['RAILS_ENV']
|
35
|
+
Dotenv::Railtie.load
|
36
|
+
end
|
36
37
|
|
37
38
|
HOSTNAME = ENV['HOSTNAME']
|
38
39
|
```
|
@@ -64,7 +65,7 @@ Dotenv.load
|
|
64
65
|
|
65
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.
|
66
67
|
|
67
|
-
```
|
68
|
+
```ruby
|
68
69
|
require 'dotenv'
|
69
70
|
Dotenv.load('file1.env', 'file2.env')
|
70
71
|
```
|
@@ -75,6 +76,12 @@ Alternatively, you can use the `dotenv` executable to launch your application:
|
|
75
76
|
$ dotenv ./script.rb
|
76
77
|
```
|
77
78
|
|
79
|
+
The `dotenv` executable also accepts a single flag, `-f`. Its value should be a comma-separated list of configuration files, in the order of most important to least. All of the files must exist. There _must_ be a space between the flag and its value.
|
80
|
+
|
81
|
+
```
|
82
|
+
$ dotenv -f ".env.local,.env" ./script.rb
|
83
|
+
```
|
84
|
+
|
78
85
|
To ensure `.env` is loaded in rake, load the tasks:
|
79
86
|
|
80
87
|
```ruby
|
@@ -112,9 +119,21 @@ export SECRET_KEY=YOURSECRETKEYGOESHERE
|
|
112
119
|
If you need multiline variables, for example private keys, you can double quote strings and use the `\n` character for newlines:
|
113
120
|
|
114
121
|
```shell
|
115
|
-
PRIVATE_KEY="-----BEGIN RSA PRIVATE KEY-----\nHkVN9
|
122
|
+
PRIVATE_KEY="-----BEGIN RSA PRIVATE KEY-----\nHkVN9...\n-----END DSA PRIVATE KEY-----\n"
|
123
|
+
```
|
124
|
+
|
125
|
+
Alternatively, multi-line values with line breaks are now supported for quoted values.
|
126
|
+
|
127
|
+
```shell
|
128
|
+
PRIVATE_KEY="-----BEGIN RSA PRIVATE KEY-----
|
129
|
+
...
|
130
|
+
HkVN9...
|
131
|
+
...
|
132
|
+
-----END DSA PRIVATE KEY-----"
|
116
133
|
```
|
117
134
|
|
135
|
+
This is particularly helpful when using the Heroku command line plugin [`heroku-config`](https://github.com/xavdid/heroku-config) to pull configuration variables down that may have line breaks.
|
136
|
+
|
118
137
|
### Command Substitution
|
119
138
|
|
120
139
|
You need to add the output of a command in one of your variables? Simply add it with `$(your_command)`:
|
@@ -147,6 +166,31 @@ SECRET_KEY=YOURSECRETKEYGOESHERE # comment
|
|
147
166
|
SECRET_HASH="something-with-a-#-hash"
|
148
167
|
```
|
149
168
|
|
169
|
+
### Required Keys
|
170
|
+
|
171
|
+
If a particular configuration value is required but not set, it's appropriate to raise an error.
|
172
|
+
|
173
|
+
To require configuration keys:
|
174
|
+
|
175
|
+
```ruby
|
176
|
+
# config/initializers/dotenv.rb
|
177
|
+
|
178
|
+
Dotenv.require_keys("SERVICE_APP_ID", "SERVICE_KEY", "SERVICE_SECRET")
|
179
|
+
```
|
180
|
+
|
181
|
+
If any of the configuration keys above are not set, your application will raise an error during initialization. This method is preferred because it prevents runtime errors in a production application due to improper configuration.
|
182
|
+
|
183
|
+
### Parsing
|
184
|
+
|
185
|
+
To parse a list of env files for programmatic inspection without modifying the ENV:
|
186
|
+
|
187
|
+
```ruby
|
188
|
+
Dotenv.parse(".env.local", ".env")
|
189
|
+
# => {'S3_BUCKET' => 'YOURS3BUCKET', 'SECRET_KEY' => 'YOURSECRETKEYGOESHERE', ...}
|
190
|
+
```
|
191
|
+
|
192
|
+
This method returns a hash of the ENV var name/value pairs.
|
193
|
+
|
150
194
|
## Frequently Answered Questions
|
151
195
|
|
152
196
|
### Can I use dotenv in production?
|
@@ -159,23 +203,58 @@ If you use this gem to handle env vars for multiple Rails environments (developm
|
|
159
203
|
|
160
204
|
### What other .env* files can I use?
|
161
205
|
|
162
|
-
`dotenv-rails` will
|
206
|
+
`dotenv-rails` will override in the following order (highest defined variable overrides lower):
|
207
|
+
|
208
|
+
| Hierarchy Priority | Filename | Environment | Should I `.gitignore`it? | Notes |
|
209
|
+
| ------------------ | ------------------------ | -------------------- | --------------------------------------------------- | ------------------------------------------------------------ |
|
210
|
+
| 1st (highest) | `.env.development.local` | Development | Yes! | Local overrides of environment-specific settings. |
|
211
|
+
| 1st | `.env.test.local` | Test | Yes! | Local overrides of environment-specific settings. |
|
212
|
+
| 1st | `.env.production.local` | Production | Yes! | Local overrides of environment-specific settings. |
|
213
|
+
| 2nd | `.env.local` | Wherever the file is | Definitely. | Local overrides. This file is loaded for all environments _except_ `test`. |
|
214
|
+
| 3rd | `.env.development` | Development | No. | Shared environment-specific settings |
|
215
|
+
| 3rd | `.env.test` | Test | No. | Shared environment-specific settings |
|
216
|
+
| 3rd | `.env.production` | Production | No. | Shared environment-specific settings |
|
217
|
+
| Last | `.env` | All Environments | Depends (See [below](#should-i-commit-my-env-file)) | The Original® |
|
163
218
|
|
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
219
|
|
169
220
|
### Should I commit my .env file?
|
170
221
|
|
171
222
|
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.
|
172
223
|
|
224
|
+
|
225
|
+
You can use the `-t` or `--template` flag on the dotenv cli to create a template of your `.env` file.
|
226
|
+
```shell
|
227
|
+
$ dotenv -t .env
|
228
|
+
```
|
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.
|
230
|
+
|
231
|
+
The template will contain all the environment variables in your `.env` file but with their values set to the variable names.
|
232
|
+
|
233
|
+
```shell
|
234
|
+
# .env
|
235
|
+
S3_BUCKET=YOURS3BUCKET
|
236
|
+
SECRET_KEY=YOURSECRETKEYGOESHERE
|
237
|
+
```
|
238
|
+
|
239
|
+
Would become
|
240
|
+
|
241
|
+
```shell
|
242
|
+
# .env.template
|
243
|
+
S3_BUCKET=S3_BUCKET
|
244
|
+
SECRET_KEY=SECRET_KEY
|
245
|
+
```
|
246
|
+
|
173
247
|
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.
|
174
248
|
|
175
249
|
### Why is it not overriding existing `ENV` variables?
|
176
250
|
|
177
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`.
|
178
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
|
+
|
179
258
|
## Contributing
|
180
259
|
|
181
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/rails.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
require "dotenv"
|
2
2
|
|
3
|
-
# Fix for
|
3
|
+
# Fix for rake tasks loading in development
|
4
4
|
#
|
5
5
|
# Dotenv loads environment variables when the Rails application is initialized.
|
6
6
|
# When running `rake`, the Rails application is initialized in development.
|
@@ -9,8 +9,11 @@ require "dotenv"
|
|
9
9
|
#
|
10
10
|
# See https://github.com/bkeepers/dotenv/issues/219
|
11
11
|
if defined?(Rake.application)
|
12
|
-
|
13
|
-
|
12
|
+
task_regular_expression = /^(default$|parallel:spec|spec(:|$))/
|
13
|
+
if Rake.application.top_level_tasks.grep(task_regular_expression).any?
|
14
|
+
environment = Rake.application.options.show_tasks ? "development" : "test"
|
15
|
+
Rails.env = ENV["RAILS_ENV"] ||= environment
|
16
|
+
end
|
14
17
|
end
|
15
18
|
|
16
19
|
Dotenv.instrumenter = ActiveSupport::Notifications
|
@@ -22,7 +25,7 @@ begin
|
|
22
25
|
event = ActiveSupport::Notifications::Event.new(*args)
|
23
26
|
Spring.watch event.payload[:env].filename if Rails.application
|
24
27
|
end
|
25
|
-
rescue LoadError
|
28
|
+
rescue LoadError, ArgumentError
|
26
29
|
# Spring is not available
|
27
30
|
end
|
28
31
|
|
@@ -38,6 +41,13 @@ module Dotenv
|
|
38
41
|
Dotenv.load(*dotenv_files)
|
39
42
|
end
|
40
43
|
|
44
|
+
# Public: Reload dotenv
|
45
|
+
#
|
46
|
+
# Same as `load`, but will override existing values in `ENV`
|
47
|
+
def overload
|
48
|
+
Dotenv.overload(*dotenv_files)
|
49
|
+
end
|
50
|
+
|
41
51
|
# Internal: `Rails.root` is nil in Rails 4.1 before the application is
|
42
52
|
# initialized, so this falls back to the `RAILS_ROOT` environment variable,
|
43
53
|
# or the current working directory.
|
@@ -51,8 +61,6 @@ module Dotenv
|
|
51
61
|
instance.load
|
52
62
|
end
|
53
63
|
|
54
|
-
config.before_configuration { load }
|
55
|
-
|
56
64
|
private
|
57
65
|
|
58
66
|
def dotenv_files
|
@@ -63,5 +71,7 @@ module Dotenv
|
|
63
71
|
root.join(".env")
|
64
72
|
].compact
|
65
73
|
end
|
74
|
+
|
75
|
+
config.before_configuration { load }
|
66
76
|
end
|
67
77
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dotenv-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
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:
|
11
|
+
date: 2022-07-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: dotenv
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - '='
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: 2.
|
19
|
+
version: 2.8.1
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - '='
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: 2.
|
26
|
+
version: 2.8.1
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: railties
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -31,9 +31,6 @@ dependencies:
|
|
31
31
|
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '3.2'
|
34
|
-
- - "<"
|
35
|
-
- !ruby/object:Gem::Version
|
36
|
-
version: '6.0'
|
37
34
|
type: :runtime
|
38
35
|
prerelease: false
|
39
36
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -41,9 +38,6 @@ dependencies:
|
|
41
38
|
- - ">="
|
42
39
|
- !ruby/object:Gem::Version
|
43
40
|
version: '3.2'
|
44
|
-
- - "<"
|
45
|
-
- !ruby/object:Gem::Version
|
46
|
-
version: '6.0'
|
47
41
|
- !ruby/object:Gem::Dependency
|
48
42
|
name: spring
|
49
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -74,7 +68,7 @@ homepage: https://github.com/bkeepers/dotenv
|
|
74
68
|
licenses:
|
75
69
|
- MIT
|
76
70
|
metadata: {}
|
77
|
-
post_install_message:
|
71
|
+
post_install_message:
|
78
72
|
rdoc_options: []
|
79
73
|
require_paths:
|
80
74
|
- lib
|
@@ -89,9 +83,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
89
83
|
- !ruby/object:Gem::Version
|
90
84
|
version: '0'
|
91
85
|
requirements: []
|
92
|
-
|
93
|
-
|
94
|
-
signing_key:
|
86
|
+
rubygems_version: 3.2.32
|
87
|
+
signing_key:
|
95
88
|
specification_version: 4
|
96
89
|
summary: Autoload dotenv in Rails.
|
97
90
|
test_files: []
|