giga-fast-box 0.0.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 +7 -0
- data/dotenv-3.2.0/LICENSE +22 -0
- data/dotenv-3.2.0/README.md +337 -0
- data/dotenv-3.2.0/bin/dotenv +4 -0
- data/dotenv-3.2.0/lib/dotenv/autorestore.rb +29 -0
- data/dotenv-3.2.0/lib/dotenv/cli.rb +59 -0
- data/dotenv-3.2.0/lib/dotenv/diff.rb +59 -0
- data/dotenv-3.2.0/lib/dotenv/environment.rb +25 -0
- data/dotenv-3.2.0/lib/dotenv/load.rb +3 -0
- data/dotenv-3.2.0/lib/dotenv/log_subscriber.rb +61 -0
- data/dotenv-3.2.0/lib/dotenv/missing_keys.rb +10 -0
- data/dotenv-3.2.0/lib/dotenv/parser.rb +109 -0
- data/dotenv-3.2.0/lib/dotenv/rails-now.rb +10 -0
- data/dotenv-3.2.0/lib/dotenv/rails.rb +111 -0
- data/dotenv-3.2.0/lib/dotenv/replay_logger.rb +20 -0
- data/dotenv-3.2.0/lib/dotenv/substitutions/command.rb +41 -0
- data/dotenv-3.2.0/lib/dotenv/substitutions/variable.rb +37 -0
- data/dotenv-3.2.0/lib/dotenv/tasks.rb +7 -0
- data/dotenv-3.2.0/lib/dotenv/template.rb +44 -0
- data/dotenv-3.2.0/lib/dotenv/version.rb +3 -0
- data/dotenv-3.2.0/lib/dotenv.rb +151 -0
- data/giga-fast-box.gemspec +12 -0
- metadata +62 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: c532625aa89e8e693bc310b4127eb7dd4a76ce9f1c5e870f415dce2c824c0371
|
|
4
|
+
data.tar.gz: 6cb8d9df88505c24814d4c76ef429e7dc0e1233a09a5997907dd5ee53da91b05
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: acff4ba00985f9ef43662844e8e91302afcd596bc54a379f5e95272b5e29543e79fb2c03212932f3455a3386b7d887f6771c7fa1f0521039e66238bf521fe549
|
|
7
|
+
data.tar.gz: 3518fe17bdae3841c080943468c69e895f06b744116ff8a097eb731be0e6934ccd1ca5fb6e0153ba556119d6217855392824bd71c5ca73fbeaad2a5599eee9b7
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
Copyright (c) 2012 Brandon Keepers
|
|
2
|
+
|
|
3
|
+
MIT License
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
6
|
+
a copy of this software and associated documentation files (the
|
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
11
|
+
the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be
|
|
14
|
+
included in all copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
@@ -0,0 +1,337 @@
|
|
|
1
|
+
# dotenv [](https://badge.fury.io/rb/dotenv)
|
|
2
|
+
|
|
3
|
+
Shim to load environment variables from `.env` into `ENV` in *development*.
|
|
4
|
+
|
|
5
|
+
Storing [configuration in the environment](http://12factor.net/config) is one of the tenets of a [twelve-factor app](http://12factor.net). Anything that is likely to change between deployment environments–such as resource handles for databases or credentials for external services–should be extracted from the code into environment variables.
|
|
6
|
+
|
|
7
|
+
But it is not always practical to set environment variables on development machines or continuous integration servers where multiple projects are run. dotenv loads variables from a `.env` file into `ENV` when the environment is bootstrapped.
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
Add this line to the top of your application's Gemfile and run `bundle install`:
|
|
12
|
+
|
|
13
|
+
```ruby
|
|
14
|
+
gem 'dotenv', groups: [:development, :test]
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Usage
|
|
18
|
+
|
|
19
|
+
Add your application configuration to your `.env` file in the root of your project:
|
|
20
|
+
|
|
21
|
+
```shell
|
|
22
|
+
S3_BUCKET=YOURS3BUCKET
|
|
23
|
+
SECRET_KEY=YOURSECRETKEYGOESHERE
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
Whenever your application loads, these variables will be available in `ENV`:
|
|
27
|
+
|
|
28
|
+
```ruby
|
|
29
|
+
config.fog_directory = ENV['S3_BUCKET']
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
See the [API Docs](https://rubydoc.info/github/bkeepers/dotenv/main) for more.
|
|
33
|
+
|
|
34
|
+
### Rails
|
|
35
|
+
|
|
36
|
+
Dotenv will automatically load when your Rails app boots. See [Customizing Rails](#customizing-rails) to change which files are loaded and when.
|
|
37
|
+
|
|
38
|
+
### Sinatra / Ruby
|
|
39
|
+
|
|
40
|
+
Load Dotenv as early as possible in your application bootstrap process:
|
|
41
|
+
|
|
42
|
+
```ruby
|
|
43
|
+
require 'dotenv/load'
|
|
44
|
+
|
|
45
|
+
# or
|
|
46
|
+
require 'dotenv'
|
|
47
|
+
Dotenv.load
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
By default, `load` will look for a file called `.env` in the current working directory.
|
|
51
|
+
Pass in multiple files and they will be loaded in order.
|
|
52
|
+
The first value set for a variable will win.
|
|
53
|
+
Existing environment variables will not be overwritten unless you set `overwrite: true`.
|
|
54
|
+
|
|
55
|
+
```ruby
|
|
56
|
+
require 'dotenv'
|
|
57
|
+
Dotenv.load('file1.env', 'file2.env')
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
### Autorestore in tests
|
|
61
|
+
|
|
62
|
+
Since 3.0, dotenv in a Rails app will automatically restore `ENV` after each test. This means you can modify `ENV` in your tests without fear of leaking state to other tests. It works with both `ActiveSupport::TestCase` and `Rspec`.
|
|
63
|
+
|
|
64
|
+
To disable this behavior, set `config.dotenv.autorestore = false` in `config/application.rb` or `config/environments/test.rb`. It is disabled by default if your app uses [climate_control](https://github.com/thoughtbot/climate_control) or [ice_age](https://github.com/dpep/ice_age_rb).
|
|
65
|
+
|
|
66
|
+
To use this behavior outside of a Rails app, just `require "dotenv/autorestore"` in your test suite.
|
|
67
|
+
|
|
68
|
+
See [`Dotenv.save`](https://rubydoc.info/github/bkeepers/dotenv/main/Dotenv:save), [Dotenv.restore](https://rubydoc.info/github/bkeepers/dotenv/main/Dotenv:restore), and [`Dotenv.modify(hash) { ... }`](https://rubydoc.info/github/bkeepers/dotenv/main/Dotenv:modify) for manual usage.
|
|
69
|
+
|
|
70
|
+
### Rake
|
|
71
|
+
|
|
72
|
+
To ensure `.env` is loaded in rake, load the tasks:
|
|
73
|
+
|
|
74
|
+
```ruby
|
|
75
|
+
require 'dotenv/tasks'
|
|
76
|
+
|
|
77
|
+
task mytask: :dotenv do
|
|
78
|
+
# things that require .env
|
|
79
|
+
end
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
### CLI
|
|
83
|
+
|
|
84
|
+
You can use the `dotenv` executable load `.env` before launching your application:
|
|
85
|
+
|
|
86
|
+
```console
|
|
87
|
+
$ dotenv ./script.rb
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
The `dotenv` executable also accepts the flag `-f`. Its value should be a comma-separated list of configuration files, in the order of the most important to the least important. All of the files must exist. There _must_ be a space between the flag and its value.
|
|
91
|
+
|
|
92
|
+
```console
|
|
93
|
+
$ dotenv -f ".env.local,.env" ./script.rb
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
The `dotenv` executable can optionally ignore missing files with the `-i` or `--ignore` flag. For example, if the `.env.local` file does not exist, the following will ignore the missing file and only load the `.env` file.
|
|
97
|
+
|
|
98
|
+
```console
|
|
99
|
+
$ dotenv -i -f ".env.local,.env" ./script.rb
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
### Load Order
|
|
103
|
+
|
|
104
|
+
If you use gems that require environment variables to be set before they are loaded, then list `dotenv` in the `Gemfile` before those other gems and require `dotenv/load`.
|
|
105
|
+
|
|
106
|
+
```ruby
|
|
107
|
+
gem 'dotenv', require: 'dotenv/load'
|
|
108
|
+
gem 'gem-that-requires-env-variables'
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
### Customizing Rails
|
|
112
|
+
|
|
113
|
+
Dotenv will load the following files depending on `RAILS_ENV`, with the first file having the highest precedence, and `.env` having the lowest precedence:
|
|
114
|
+
|
|
115
|
+
<table>
|
|
116
|
+
<thead>
|
|
117
|
+
<tr>
|
|
118
|
+
<th>Priority</th>
|
|
119
|
+
<th colspan="3">Environment</th>
|
|
120
|
+
<th><code>.gitignore</code>it?</th>
|
|
121
|
+
<th>Notes</th>
|
|
122
|
+
</tr>
|
|
123
|
+
<tr>
|
|
124
|
+
<th></th>
|
|
125
|
+
<th>development</th>
|
|
126
|
+
<th>test</th>
|
|
127
|
+
<th>production</th>
|
|
128
|
+
<th></th>
|
|
129
|
+
<th></th>
|
|
130
|
+
</tr>
|
|
131
|
+
</thead>
|
|
132
|
+
<tr>
|
|
133
|
+
<td>highest</td>
|
|
134
|
+
<td><code>.env.development.local</code></td>
|
|
135
|
+
<td><code>.env.test.local</code></td>
|
|
136
|
+
<td><code>.env.production.local</code></td>
|
|
137
|
+
<td>Yes</td>
|
|
138
|
+
<td>Environment-specific local overrides</td>
|
|
139
|
+
</tr>
|
|
140
|
+
<tr>
|
|
141
|
+
<td>2nd</td>
|
|
142
|
+
<td><code>.env.local</code></td>
|
|
143
|
+
<td><strong>N/A</strong></td>
|
|
144
|
+
<td><code>.env.local</code></td>
|
|
145
|
+
<td>Yes</td>
|
|
146
|
+
<td>Local overrides</td>
|
|
147
|
+
</tr>
|
|
148
|
+
<tr>
|
|
149
|
+
<td>3rd</td>
|
|
150
|
+
<td><code>.env.development</code></td>
|
|
151
|
+
<td><code>.env.test</code></td>
|
|
152
|
+
<td><code>.env.production</code></td>
|
|
153
|
+
<td>No</td>
|
|
154
|
+
<td>Shared environment-specific variables</td>
|
|
155
|
+
</tr>
|
|
156
|
+
<tr>
|
|
157
|
+
<td>last</td>
|
|
158
|
+
<td><code>.env</code></td>
|
|
159
|
+
<td><code>.env</code></td>
|
|
160
|
+
<td><code>.env</code></td>
|
|
161
|
+
<td><a href="#should-i-commit-my-env-file">Maybe</a></td>
|
|
162
|
+
<td>Shared for all environments</td>
|
|
163
|
+
</tr>
|
|
164
|
+
</table>
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
These files are loaded during the `before_configuration` callback, which is fired when the `Application` constant is defined in `config/application.rb` with `class Application < Rails::Application`. If you need it to be initialized sooner, or need to customize the loading process, you can do so at the top of `application.rb`
|
|
168
|
+
|
|
169
|
+
```ruby
|
|
170
|
+
# config/application.rb
|
|
171
|
+
Bundler.require(*Rails.groups)
|
|
172
|
+
|
|
173
|
+
# Load .env.local in test
|
|
174
|
+
Dotenv::Rails.files.unshift(".env.local") if ENV["RAILS_ENV"] == "test"
|
|
175
|
+
|
|
176
|
+
module YourApp
|
|
177
|
+
class Application < Rails::Application
|
|
178
|
+
# ...
|
|
179
|
+
end
|
|
180
|
+
end
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
Available options:
|
|
184
|
+
|
|
185
|
+
* `Dotenv::Rails.files` - list of files to be loaded, in order of precedence.
|
|
186
|
+
* `Dotenv::Rails.overwrite` - Overwrite existing `ENV` variables with contents of `.env*` files
|
|
187
|
+
* `Dotenv::Rails.logger` - The logger to use for dotenv's logging. Defaults to `Rails.logger`
|
|
188
|
+
* `Dotenv::Rails.autorestore` - Enable or disable [autorestore](#autorestore-in-tests)
|
|
189
|
+
|
|
190
|
+
### Multi-line values
|
|
191
|
+
|
|
192
|
+
Multi-line values with line breaks must be surrounded with double quotes.
|
|
193
|
+
|
|
194
|
+
```shell
|
|
195
|
+
PRIVATE_KEY="-----BEGIN RSA PRIVATE KEY-----
|
|
196
|
+
...
|
|
197
|
+
HkVN9...
|
|
198
|
+
...
|
|
199
|
+
-----END DSA PRIVATE KEY-----"
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
Prior to 3.0, dotenv would replace `\n` in quoted strings with a newline, but that behavior is deprecated. To use the old behavior, set `DOTENV_LINEBREAK_MODE=legacy` before any variables that include `\n`:
|
|
203
|
+
|
|
204
|
+
```shell
|
|
205
|
+
DOTENV_LINEBREAK_MODE=legacy
|
|
206
|
+
PRIVATE_KEY="-----BEGIN RSA PRIVATE KEY-----\nHkVN9...\n-----END DSA PRIVATE KEY-----\n"
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
### Command Substitution
|
|
210
|
+
|
|
211
|
+
You need to add the output of a command in one of your variables? Simply add it with `$(your_command)`:
|
|
212
|
+
|
|
213
|
+
```shell
|
|
214
|
+
DATABASE_URL="postgres://$(whoami)@localhost/my_database"
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
### Variable Substitution
|
|
218
|
+
|
|
219
|
+
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.
|
|
220
|
+
|
|
221
|
+
```shell
|
|
222
|
+
DATABASE_URL="postgres://${USER}@localhost/my_database"
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
If a value contains a `$` and it is not intended to be a variable, wrap it in single quotes.
|
|
226
|
+
|
|
227
|
+
```shell
|
|
228
|
+
PASSWORD='pas$word'
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
### Comments
|
|
232
|
+
|
|
233
|
+
Comments may be added to your file as such:
|
|
234
|
+
|
|
235
|
+
```shell
|
|
236
|
+
# This is a comment
|
|
237
|
+
SECRET_KEY=YOURSECRETKEYGOESHERE # comment
|
|
238
|
+
SECRET_HASH="something-with-a-#-hash"
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
### Exports
|
|
242
|
+
|
|
243
|
+
For compatability, you may also add `export` in front of each line so you can `source` the file in bash:
|
|
244
|
+
|
|
245
|
+
```shell
|
|
246
|
+
export S3_BUCKET=YOURS3BUCKET
|
|
247
|
+
export SECRET_KEY=YOURSECRETKEYGOESHERE
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
### Required Keys
|
|
251
|
+
|
|
252
|
+
If a particular configuration value is required but not set, it's appropriate to raise an error.
|
|
253
|
+
|
|
254
|
+
To require configuration keys:
|
|
255
|
+
|
|
256
|
+
```ruby
|
|
257
|
+
# config/initializers/dotenv.rb
|
|
258
|
+
|
|
259
|
+
Dotenv.require_keys("SERVICE_APP_ID", "SERVICE_KEY", "SERVICE_SECRET")
|
|
260
|
+
```
|
|
261
|
+
|
|
262
|
+
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.
|
|
263
|
+
|
|
264
|
+
### Parsing
|
|
265
|
+
|
|
266
|
+
To parse a list of env files for programmatic inspection without modifying the ENV:
|
|
267
|
+
|
|
268
|
+
```ruby
|
|
269
|
+
Dotenv.parse(".env.local", ".env")
|
|
270
|
+
# => {'S3_BUCKET' => 'YOURS3BUCKET', 'SECRET_KEY' => 'YOURSECRETKEYGOESHERE', ...}
|
|
271
|
+
```
|
|
272
|
+
|
|
273
|
+
This method returns a hash of the ENV var name/value pairs.
|
|
274
|
+
|
|
275
|
+
### Templates
|
|
276
|
+
|
|
277
|
+
You can use the `-t` or `--template` flag on the dotenv cli to create a template of your `.env` file.
|
|
278
|
+
|
|
279
|
+
```console
|
|
280
|
+
$ dotenv -t .env
|
|
281
|
+
```
|
|
282
|
+
A template will be created in your working directory named `{FILENAME}.template`. So in the above example, it would create a `.env.template` file.
|
|
283
|
+
|
|
284
|
+
The template will contain all the environment variables in your `.env` file but with their values set to the variable names.
|
|
285
|
+
|
|
286
|
+
```shell
|
|
287
|
+
# .env
|
|
288
|
+
S3_BUCKET=YOURS3BUCKET
|
|
289
|
+
SECRET_KEY=YOURSECRETKEYGOESHERE
|
|
290
|
+
```
|
|
291
|
+
|
|
292
|
+
Would become
|
|
293
|
+
|
|
294
|
+
```shell
|
|
295
|
+
# .env.template
|
|
296
|
+
S3_BUCKET=S3_BUCKET
|
|
297
|
+
SECRET_KEY=SECRET_KEY
|
|
298
|
+
```
|
|
299
|
+
|
|
300
|
+
## Frequently Answered Questions
|
|
301
|
+
|
|
302
|
+
### Can I use dotenv in production?
|
|
303
|
+
|
|
304
|
+
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.
|
|
305
|
+
|
|
306
|
+
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`.
|
|
307
|
+
|
|
308
|
+
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>`.
|
|
309
|
+
|
|
310
|
+
### Should I commit my .env file?
|
|
311
|
+
|
|
312
|
+
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.
|
|
313
|
+
|
|
314
|
+
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.
|
|
315
|
+
|
|
316
|
+
### Why is it not overwriting existing `ENV` variables?
|
|
317
|
+
|
|
318
|
+
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.load files, overwrite: true`.
|
|
319
|
+
|
|
320
|
+
To warn when a value was not overwritten (e.g. to make users aware of this gotcha),
|
|
321
|
+
use `Dotenv.load files, overwrite: :warn`.
|
|
322
|
+
|
|
323
|
+
You can also use the `-o` or `--overwrite` flag on the dotenv cli to overwrite existing `ENV` variables.
|
|
324
|
+
|
|
325
|
+
```console
|
|
326
|
+
$ dotenv -o -f ".env.local,.env"
|
|
327
|
+
```
|
|
328
|
+
|
|
329
|
+
## Contributing
|
|
330
|
+
|
|
331
|
+
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).
|
|
332
|
+
|
|
333
|
+
1. Fork it
|
|
334
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
|
335
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
|
336
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
|
337
|
+
5. Create new Pull Request
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# Automatically restore `ENV` to its original state after
|
|
2
|
+
|
|
3
|
+
if defined?(RSpec.configure)
|
|
4
|
+
RSpec.configure do |config|
|
|
5
|
+
# Save ENV before the suite starts
|
|
6
|
+
config.before(:suite) { Dotenv.save }
|
|
7
|
+
|
|
8
|
+
# Restore ENV after each example
|
|
9
|
+
config.after { Dotenv.restore }
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
if defined?(ActiveSupport)
|
|
14
|
+
ActiveSupport.on_load(:active_support_test_case) do
|
|
15
|
+
ActiveSupport::TestCase.class_eval do
|
|
16
|
+
# Save ENV before each test
|
|
17
|
+
setup { Dotenv.save }
|
|
18
|
+
|
|
19
|
+
# Restore ENV after each test
|
|
20
|
+
teardown do
|
|
21
|
+
Dotenv.restore
|
|
22
|
+
rescue ThreadError => e
|
|
23
|
+
# Restore will fail if running tests in parallel.
|
|
24
|
+
warn e.message
|
|
25
|
+
warn "Set `config.dotenv.autorestore = false` in `config/initializers/test.rb`" if defined?(Dotenv::Rails)
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
require "dotenv"
|
|
2
|
+
require "dotenv/version"
|
|
3
|
+
require "dotenv/template"
|
|
4
|
+
require "optparse"
|
|
5
|
+
|
|
6
|
+
module Dotenv
|
|
7
|
+
# The `dotenv` command line interface. Run `$ dotenv --help` to see usage.
|
|
8
|
+
class CLI < OptionParser
|
|
9
|
+
attr_reader :argv, :filenames, :overwrite
|
|
10
|
+
|
|
11
|
+
def initialize(argv = [])
|
|
12
|
+
@argv = argv.dup
|
|
13
|
+
@filenames = []
|
|
14
|
+
@ignore = false
|
|
15
|
+
@overwrite = false
|
|
16
|
+
|
|
17
|
+
super("Usage: dotenv [options]")
|
|
18
|
+
separator ""
|
|
19
|
+
|
|
20
|
+
on("-f FILES", Array, "List of env files to parse") do |list|
|
|
21
|
+
@filenames = list
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
on("-i", "--ignore", "ignore missing env files") do
|
|
25
|
+
@ignore = true
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
on("-o", "--overwrite", "overwrite existing ENV variables") do
|
|
29
|
+
@overwrite = true
|
|
30
|
+
end
|
|
31
|
+
on("--overload") { @overwrite = true }
|
|
32
|
+
|
|
33
|
+
on("-h", "--help", "Display help") do
|
|
34
|
+
puts self
|
|
35
|
+
exit
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
on("-v", "--version", "Show version") do
|
|
39
|
+
puts "dotenv #{Dotenv::VERSION}"
|
|
40
|
+
exit
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
on("-t", "--template=FILE", "Create a template env file") do |file|
|
|
44
|
+
template = Dotenv::EnvTemplate.new(file)
|
|
45
|
+
template.create_template
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
order!(@argv)
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def run
|
|
52
|
+
Dotenv.load(*@filenames, overwrite: @overwrite, ignore: @ignore)
|
|
53
|
+
rescue Errno::ENOENT => e
|
|
54
|
+
abort e.message
|
|
55
|
+
else
|
|
56
|
+
exec(*@argv) unless @argv.empty?
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
end
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
module Dotenv
|
|
2
|
+
# A diff between multiple states of ENV.
|
|
3
|
+
class Diff
|
|
4
|
+
# The initial state
|
|
5
|
+
attr_reader :a
|
|
6
|
+
|
|
7
|
+
# The final or current state
|
|
8
|
+
attr_reader :b
|
|
9
|
+
|
|
10
|
+
# Create a new diff. If given a block, the state of ENV after the block will be preserved as
|
|
11
|
+
# the final state for comparison. Otherwise, the current ENV will be the final state.
|
|
12
|
+
#
|
|
13
|
+
# @param a [Hash] the initial state, defaults to a snapshot of current ENV
|
|
14
|
+
# @param b [Hash] the final state, defaults to the current ENV
|
|
15
|
+
# @yield [diff] a block to execute before recording the final state
|
|
16
|
+
def initialize(a: snapshot, b: ENV, &block)
|
|
17
|
+
@a, @b = a, b
|
|
18
|
+
block&.call self
|
|
19
|
+
ensure
|
|
20
|
+
@b = snapshot if block
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
# Return a Hash of keys added with their new values
|
|
24
|
+
def added
|
|
25
|
+
b.slice(*(b.keys - a.keys))
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# Returns a Hash of keys removed with their previous values
|
|
29
|
+
def removed
|
|
30
|
+
a.slice(*(a.keys - b.keys))
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
# Returns of Hash of keys changed with an array of their previous and new values
|
|
34
|
+
def changed
|
|
35
|
+
(b.slice(*a.keys).to_a - a.to_a).map do |(k, v)|
|
|
36
|
+
[k, [a[k], v]]
|
|
37
|
+
end.to_h
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# Returns a Hash of all added, changed, and removed keys and their new values
|
|
41
|
+
def env
|
|
42
|
+
b.slice(*(added.keys + changed.keys)).merge(removed.transform_values { |v| nil })
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
# Returns true if any keys were added, removed, or changed
|
|
46
|
+
def any?
|
|
47
|
+
[added, removed, changed].any?(&:any?)
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
private
|
|
51
|
+
|
|
52
|
+
def snapshot
|
|
53
|
+
# `dup` should not be required here, but some people use `stub_const` to replace ENV with
|
|
54
|
+
# a `Hash`. This ensures that we get a frozen copy of that instead of freezing the original.
|
|
55
|
+
# https://github.com/bkeepers/dotenv/issues/482
|
|
56
|
+
ENV.to_h.dup.freeze
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
end
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
module Dotenv
|
|
2
|
+
# A `.env` file that will be read and parsed into a Hash
|
|
3
|
+
class Environment < Hash
|
|
4
|
+
attr_reader :filename, :overwrite
|
|
5
|
+
|
|
6
|
+
# Create a new Environment
|
|
7
|
+
#
|
|
8
|
+
# @param filename [String] the path to the file to read
|
|
9
|
+
# @param overwrite [Boolean] whether the parser should assume existing values will be overwritten
|
|
10
|
+
def initialize(filename, overwrite: false)
|
|
11
|
+
super()
|
|
12
|
+
@filename = filename
|
|
13
|
+
@overwrite = overwrite
|
|
14
|
+
load
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def load
|
|
18
|
+
update Parser.call(read, overwrite: overwrite)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def read
|
|
22
|
+
File.open(@filename, "rb:bom|utf-8", &:read)
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
require "active_support/log_subscriber"
|
|
2
|
+
|
|
3
|
+
module Dotenv
|
|
4
|
+
# Logs instrumented events
|
|
5
|
+
#
|
|
6
|
+
# Usage:
|
|
7
|
+
# require "active_support/notifications"
|
|
8
|
+
# require "dotenv/log_subscriber"
|
|
9
|
+
# Dotenv.instrumenter = ActiveSupport::Notifications
|
|
10
|
+
#
|
|
11
|
+
class LogSubscriber < ActiveSupport::LogSubscriber
|
|
12
|
+
attach_to :dotenv
|
|
13
|
+
|
|
14
|
+
def logger
|
|
15
|
+
Dotenv::Rails.logger
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def load(event)
|
|
19
|
+
env = event.payload[:env]
|
|
20
|
+
|
|
21
|
+
info "Loaded #{color_filename(env.filename)}"
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def update(event)
|
|
25
|
+
diff = event.payload[:diff]
|
|
26
|
+
changed = diff.env.keys.map { |key| color_var(key) }
|
|
27
|
+
debug "Set #{changed.join(", ")}" if diff.any?
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def save(event)
|
|
31
|
+
info "Saved a snapshot of #{color_env_constant}"
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def restore(event)
|
|
35
|
+
diff = event.payload[:diff]
|
|
36
|
+
|
|
37
|
+
removed = diff.removed.keys.map { |key| color(key, :RED) }
|
|
38
|
+
restored = (diff.changed.keys + diff.added.keys).map { |key| color_var(key) }
|
|
39
|
+
|
|
40
|
+
if removed.any? || restored.any?
|
|
41
|
+
info "Restored snapshot of #{color_env_constant}"
|
|
42
|
+
debug "Unset #{removed.join(", ")}" if removed.any?
|
|
43
|
+
debug "Restored #{restored.join(", ")}" if restored.any?
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
private
|
|
48
|
+
|
|
49
|
+
def color_filename(filename)
|
|
50
|
+
color(Pathname.new(filename).relative_path_from(Dotenv::Rails.root.to_s).to_s, :YELLOW)
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def color_var(name)
|
|
54
|
+
color(name, :CYAN)
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def color_env_constant
|
|
58
|
+
color("ENV", :GREEN)
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
require "dotenv/substitutions/variable"
|
|
2
|
+
require "dotenv/substitutions/command" if RUBY_VERSION > "1.8.7"
|
|
3
|
+
|
|
4
|
+
module Dotenv
|
|
5
|
+
# Error raised when encountering a syntax error while parsing a .env file.
|
|
6
|
+
class FormatError < SyntaxError; end
|
|
7
|
+
|
|
8
|
+
# Parses the `.env` file format into key/value pairs.
|
|
9
|
+
# It allows for variable substitutions, command substitutions, and exporting of variables.
|
|
10
|
+
class Parser
|
|
11
|
+
@substitutions = [
|
|
12
|
+
Dotenv::Substitutions::Command,
|
|
13
|
+
Dotenv::Substitutions::Variable
|
|
14
|
+
]
|
|
15
|
+
|
|
16
|
+
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
|
|
34
|
+
/x
|
|
35
|
+
|
|
36
|
+
QUOTED_STRING = /\A(['"])(.*)\1\z/m
|
|
37
|
+
|
|
38
|
+
class << self
|
|
39
|
+
attr_reader :substitutions
|
|
40
|
+
|
|
41
|
+
def call(...)
|
|
42
|
+
new(...).call
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def initialize(string, overwrite: false)
|
|
47
|
+
# Convert line breaks to same format
|
|
48
|
+
@string = string.gsub(/\r\n?/, "\n")
|
|
49
|
+
@hash = {}
|
|
50
|
+
@overwrite = overwrite
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def call
|
|
54
|
+
@string.scan(LINE) do
|
|
55
|
+
match = $LAST_MATCH_INFO
|
|
56
|
+
|
|
57
|
+
if existing?(match[:key])
|
|
58
|
+
# Use value from already defined variable
|
|
59
|
+
@hash[match[:key]] = ENV[match[:key]]
|
|
60
|
+
elsif match[:export] && !match[:value]
|
|
61
|
+
# Check for exported variable with no value
|
|
62
|
+
if !@hash.member?(match[:key])
|
|
63
|
+
raise FormatError, "Line #{match.to_s.inspect} has an unset variable"
|
|
64
|
+
end
|
|
65
|
+
else
|
|
66
|
+
@hash[match[:key]] = parse_value(match[:value] || "")
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
@hash
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
private
|
|
74
|
+
|
|
75
|
+
# Determine if a variable is already defined and should not be overwritten.
|
|
76
|
+
def existing?(key)
|
|
77
|
+
!@overwrite && key != "DOTENV_LINEBREAK_MODE" && ENV.key?(key)
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def parse_value(value)
|
|
81
|
+
# Remove surrounding quotes
|
|
82
|
+
value = value.strip.sub(QUOTED_STRING, '\2')
|
|
83
|
+
maybe_quote = Regexp.last_match(1)
|
|
84
|
+
|
|
85
|
+
# Expand new lines in double quoted values
|
|
86
|
+
value = expand_newlines(value) if maybe_quote == '"'
|
|
87
|
+
|
|
88
|
+
# Unescape characters and performs substitutions unless value is single quoted
|
|
89
|
+
if maybe_quote != "'"
|
|
90
|
+
value = unescape_characters(value)
|
|
91
|
+
self.class.substitutions.each { |proc| value = proc.call(value, @hash) }
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
value
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
def unescape_characters(value)
|
|
98
|
+
value.gsub(/\\([^$])/, '\1')
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
def expand_newlines(value)
|
|
102
|
+
if (@hash["DOTENV_LINEBREAK_MODE"] || ENV["DOTENV_LINEBREAK_MODE"]) == "legacy"
|
|
103
|
+
value.gsub('\n', "\n").gsub('\r', "\r")
|
|
104
|
+
else
|
|
105
|
+
value.gsub('\n', "\\\\\\n").gsub('\r', "\\\\\\r")
|
|
106
|
+
end
|
|
107
|
+
end
|
|
108
|
+
end
|
|
109
|
+
end
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
# If you use gems that require environment variables to be set before they are
|
|
2
|
+
# loaded, then list `dotenv` in the `Gemfile` before those other gems and
|
|
3
|
+
# require `dotenv/load`.
|
|
4
|
+
#
|
|
5
|
+
# gem "dotenv", require: "dotenv/load"
|
|
6
|
+
# gem "gem-that-requires-env-variables"
|
|
7
|
+
#
|
|
8
|
+
|
|
9
|
+
require "dotenv/load"
|
|
10
|
+
warn '[DEPRECATION] `require "dotenv/rails-now"` is deprecated. Use `require "dotenv/load"` instead.', caller(1..1).first
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
# Since rubygems doesn't support optional dependencies, we have to manually check
|
|
2
|
+
unless Gem::Requirement.new(">= 6.1").satisfied_by?(Gem::Version.new(Rails.version))
|
|
3
|
+
warn "dotenv 3.0 only supports Rails 6.1 or later. Use dotenv ~> 2.0."
|
|
4
|
+
return
|
|
5
|
+
end
|
|
6
|
+
|
|
7
|
+
require "dotenv/replay_logger"
|
|
8
|
+
require "dotenv/log_subscriber"
|
|
9
|
+
|
|
10
|
+
Dotenv.instrumenter = ActiveSupport::Notifications
|
|
11
|
+
|
|
12
|
+
# Watch all loaded env files with Spring
|
|
13
|
+
ActiveSupport::Notifications.subscribe("load.dotenv") do |*args|
|
|
14
|
+
if defined?(Spring) && Spring.respond_to?(:watch)
|
|
15
|
+
event = ActiveSupport::Notifications::Event.new(*args)
|
|
16
|
+
Spring.watch event.payload[:env].filename if Rails.application
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
module Dotenv
|
|
21
|
+
# Rails integration for using Dotenv to load ENV variables from a file
|
|
22
|
+
class Rails < ::Rails::Railtie
|
|
23
|
+
delegate :files, :files=, :overwrite, :overwrite=, :autorestore, :autorestore=, :logger, to: "config.dotenv"
|
|
24
|
+
|
|
25
|
+
def initialize
|
|
26
|
+
super
|
|
27
|
+
config.dotenv = ActiveSupport::OrderedOptions.new.update(
|
|
28
|
+
# Rails.logger is not available yet, so we'll save log messages and replay them when it is
|
|
29
|
+
logger: Dotenv::ReplayLogger.new,
|
|
30
|
+
overwrite: false,
|
|
31
|
+
files: [
|
|
32
|
+
".env.#{env}.local",
|
|
33
|
+
(".env.local" unless env.test?),
|
|
34
|
+
".env.#{env}",
|
|
35
|
+
".env"
|
|
36
|
+
].compact,
|
|
37
|
+
autorestore: env.test? && !defined?(ClimateControl) && !defined?(IceAge)
|
|
38
|
+
)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# Public: Load dotenv
|
|
42
|
+
#
|
|
43
|
+
# This will get called during the `before_configuration` callback, but you
|
|
44
|
+
# can manually call `Dotenv::Rails.load` if you needed it sooner.
|
|
45
|
+
def load
|
|
46
|
+
Dotenv.load(*files.map { |file| root.join(file).to_s }, overwrite: overwrite)
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def overload
|
|
50
|
+
deprecator.warn("Dotenv::Rails.overload is deprecated. Set `Dotenv::Rails.overwrite = true` and call Dotenv::Rails.load instead.")
|
|
51
|
+
Dotenv.load(*files.map { |file| root.join(file).to_s }, overwrite: true)
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
# Internal: `Rails.root` is nil in Rails 4.1 before the application is
|
|
55
|
+
# initialized, so this falls back to the `RAILS_ROOT` environment variable,
|
|
56
|
+
# or the current working directory.
|
|
57
|
+
def root
|
|
58
|
+
::Rails.root || Pathname.new(ENV["RAILS_ROOT"] || Dir.pwd)
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
# Set a new logger and replay logs
|
|
62
|
+
def logger=(new_logger)
|
|
63
|
+
logger.replay new_logger if logger.is_a?(ReplayLogger)
|
|
64
|
+
config.dotenv.logger = new_logger
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
# The current environment that the app is running in.
|
|
68
|
+
#
|
|
69
|
+
# When running `rake`, the Rails application is initialized in development, so we have to
|
|
70
|
+
# check which rake tasks are being run to determine the environment.
|
|
71
|
+
#
|
|
72
|
+
# See https://github.com/bkeepers/dotenv/issues/219
|
|
73
|
+
def env
|
|
74
|
+
@env ||= if defined?(Rake.application) && Rake.application.top_level_tasks.grep(TEST_RAKE_TASKS).any?
|
|
75
|
+
env = Rake.application.options.show_tasks ? "development" : "test"
|
|
76
|
+
ActiveSupport::EnvironmentInquirer.new(env)
|
|
77
|
+
else
|
|
78
|
+
::Rails.env
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
TEST_RAKE_TASKS = /^(default$|test(:|$)|parallel:spec|spec(:|$))/
|
|
82
|
+
|
|
83
|
+
def deprecator # :nodoc:
|
|
84
|
+
@deprecator ||= ActiveSupport::Deprecation.new
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
# Rails uses `#method_missing` to delegate all class methods to the
|
|
88
|
+
# instance, which means `Kernel#load` gets called here. We don't want that.
|
|
89
|
+
def self.load
|
|
90
|
+
instance.load
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
initializer "dotenv", after: :initialize_logger do |app|
|
|
94
|
+
if logger.is_a?(ReplayLogger)
|
|
95
|
+
self.logger = ActiveSupport::TaggedLogging.new(::Rails.logger).tagged("dotenv")
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
initializer "dotenv.deprecator" do |app|
|
|
100
|
+
app.deprecators[:dotenv] = deprecator if app.respond_to?(:deprecators)
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
initializer "dotenv.autorestore" do |app|
|
|
104
|
+
require "dotenv/autorestore" if autorestore
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
config.before_configuration { load }
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
Railtie = ActiveSupport::Deprecation::DeprecatedConstantProxy.new("Dotenv::Railtie", "Dotenv::Rails", Dotenv::Rails.deprecator)
|
|
111
|
+
end
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
module Dotenv
|
|
2
|
+
# A logger that can be used before the apps real logger is initialized.
|
|
3
|
+
class ReplayLogger < Logger
|
|
4
|
+
def initialize
|
|
5
|
+
super(nil) # Doesn't matter what this is, it won't be used.
|
|
6
|
+
@logs = []
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
# Override the add method to store logs so we can replay them to a real logger later.
|
|
10
|
+
def add(*args, &block)
|
|
11
|
+
@logs.push([args, block])
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
# Replay the store logs to a real logger.
|
|
15
|
+
def replay(logger)
|
|
16
|
+
@logs.each { |args, block| logger.add(*args, &block) }
|
|
17
|
+
@logs.clear
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
require "English"
|
|
2
|
+
|
|
3
|
+
module Dotenv
|
|
4
|
+
module Substitutions
|
|
5
|
+
# Substitute shell commands in a value.
|
|
6
|
+
#
|
|
7
|
+
# SHA=$(git rev-parse HEAD)
|
|
8
|
+
#
|
|
9
|
+
module Command
|
|
10
|
+
class << self
|
|
11
|
+
INTERPOLATED_SHELL_COMMAND = /
|
|
12
|
+
(?<backslash>\\)? # is it escaped with a backslash?
|
|
13
|
+
\$ # literal $
|
|
14
|
+
(?<cmd> # collect command content for eval
|
|
15
|
+
\( # require opening paren
|
|
16
|
+
(?:[^()]|\g<cmd>)+ # allow any number of non-parens, or balanced
|
|
17
|
+
# parens (by nesting the <cmd> expression
|
|
18
|
+
# recursively)
|
|
19
|
+
\) # require closing paren
|
|
20
|
+
)
|
|
21
|
+
/x
|
|
22
|
+
|
|
23
|
+
def call(value, env)
|
|
24
|
+
# Process interpolated shell commands
|
|
25
|
+
value.gsub(INTERPOLATED_SHELL_COMMAND) do |*|
|
|
26
|
+
# Eliminate opening and closing parentheses
|
|
27
|
+
command = $LAST_MATCH_INFO[:cmd][1..-2]
|
|
28
|
+
|
|
29
|
+
if $LAST_MATCH_INFO[:backslash]
|
|
30
|
+
# Command is escaped, don't replace it.
|
|
31
|
+
$LAST_MATCH_INFO[0][1..]
|
|
32
|
+
else
|
|
33
|
+
# Execute the command and return the value
|
|
34
|
+
`#{Variable.call(command, env)}`.chomp
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
require "English"
|
|
2
|
+
|
|
3
|
+
module Dotenv
|
|
4
|
+
module Substitutions
|
|
5
|
+
# Substitute variables in a value.
|
|
6
|
+
#
|
|
7
|
+
# HOST=example.com
|
|
8
|
+
# URL="https://$HOST"
|
|
9
|
+
#
|
|
10
|
+
module Variable
|
|
11
|
+
class << self
|
|
12
|
+
VARIABLE = /
|
|
13
|
+
(\\)? # is it escaped with a backslash?
|
|
14
|
+
(\$) # literal $
|
|
15
|
+
(?!\() # shouldn't be followed by parenthesis
|
|
16
|
+
\{? # allow brace wrapping
|
|
17
|
+
([A-Z0-9_]+)? # optional alpha nums
|
|
18
|
+
\}? # closing brace
|
|
19
|
+
/xi
|
|
20
|
+
|
|
21
|
+
def call(value, env)
|
|
22
|
+
value.gsub(VARIABLE) do |variable|
|
|
23
|
+
match = $LAST_MATCH_INFO
|
|
24
|
+
|
|
25
|
+
if match[1] == "\\"
|
|
26
|
+
variable[1..]
|
|
27
|
+
elsif match[3]
|
|
28
|
+
env[match[3]] || ENV[match[3]] || ""
|
|
29
|
+
else
|
|
30
|
+
variable
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
module Dotenv
|
|
2
|
+
EXPORT_COMMAND = "export ".freeze
|
|
3
|
+
# Class for creating a template from a env file
|
|
4
|
+
class EnvTemplate
|
|
5
|
+
def initialize(env_file)
|
|
6
|
+
@env_file = env_file
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def create_template
|
|
10
|
+
File.open(@env_file, "r") do |env_file|
|
|
11
|
+
File.open("#{@env_file}.template", "w") do |env_template|
|
|
12
|
+
env_file.each do |line|
|
|
13
|
+
if is_comment?(line)
|
|
14
|
+
env_template.puts line
|
|
15
|
+
elsif (var = var_defined?(line))
|
|
16
|
+
if line.match(EXPORT_COMMAND)
|
|
17
|
+
env_template.puts "export #{var}=#{var}"
|
|
18
|
+
else
|
|
19
|
+
env_template.puts "#{var}=#{var}"
|
|
20
|
+
end
|
|
21
|
+
elsif line_blank?(line)
|
|
22
|
+
env_template.puts
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
private
|
|
30
|
+
|
|
31
|
+
def is_comment?(line)
|
|
32
|
+
line.strip.start_with?("#")
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def var_defined?(line)
|
|
36
|
+
match = Dotenv::Parser::LINE.match(line)
|
|
37
|
+
match && match[:key]
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def line_blank?(line)
|
|
41
|
+
line.strip.length.zero?
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
require "dotenv/version"
|
|
2
|
+
require "dotenv/parser"
|
|
3
|
+
require "dotenv/environment"
|
|
4
|
+
require "dotenv/missing_keys"
|
|
5
|
+
require "dotenv/diff"
|
|
6
|
+
|
|
7
|
+
# Shim to load environment variables from `.env files into `ENV`.
|
|
8
|
+
module Dotenv
|
|
9
|
+
extend self
|
|
10
|
+
|
|
11
|
+
# An internal monitor to synchronize access to ENV in multi-threaded environments.
|
|
12
|
+
SEMAPHORE = Monitor.new
|
|
13
|
+
private_constant :SEMAPHORE
|
|
14
|
+
|
|
15
|
+
attr_accessor :instrumenter
|
|
16
|
+
|
|
17
|
+
# Loads environment variables from one or more `.env` files. See `#parse` for more details.
|
|
18
|
+
def load(*filenames, overwrite: false, ignore: true)
|
|
19
|
+
parse(*filenames, overwrite: overwrite, ignore: ignore) do |env|
|
|
20
|
+
instrument(:load, env: env) do |payload|
|
|
21
|
+
update(env, overwrite: overwrite)
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
# Same as `#load`, but raises Errno::ENOENT if any files don't exist
|
|
27
|
+
def load!(*filenames)
|
|
28
|
+
load(*filenames, ignore: false)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# same as `#load`, but will overwrite existing values in `ENV`
|
|
32
|
+
def overwrite(*filenames)
|
|
33
|
+
load(*filenames, overwrite: true)
|
|
34
|
+
end
|
|
35
|
+
alias_method :overload, :overwrite
|
|
36
|
+
|
|
37
|
+
# same as `#overwrite`, but raises Errno::ENOENT if any files don't exist
|
|
38
|
+
def overwrite!(*filenames)
|
|
39
|
+
load(*filenames, overwrite: true, ignore: false)
|
|
40
|
+
end
|
|
41
|
+
alias_method :overload!, :overwrite!
|
|
42
|
+
|
|
43
|
+
# Parses the given files, yielding for each file if a block is given.
|
|
44
|
+
#
|
|
45
|
+
# @param filenames [String, Array<String>] Files to parse
|
|
46
|
+
# @param overwrite [Boolean] Overwrite existing `ENV` values
|
|
47
|
+
# @param ignore [Boolean] Ignore non-existent files
|
|
48
|
+
# @param block [Proc] Block to yield for each parsed `Dotenv::Environment`
|
|
49
|
+
# @return [Hash] parsed key/value pairs
|
|
50
|
+
def parse(*filenames, overwrite: false, ignore: true, &block)
|
|
51
|
+
filenames << ".env" if filenames.empty?
|
|
52
|
+
filenames = filenames.reverse if overwrite
|
|
53
|
+
|
|
54
|
+
filenames.reduce({}) do |hash, filename|
|
|
55
|
+
begin
|
|
56
|
+
env = Environment.new(File.expand_path(filename), overwrite: overwrite)
|
|
57
|
+
env = block.call(env) if block
|
|
58
|
+
rescue Errno::ENOENT, Errno::EISDIR
|
|
59
|
+
raise unless ignore
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
hash.merge! env || {}
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
# Save the current `ENV` to be restored later
|
|
67
|
+
def save
|
|
68
|
+
instrument(:save) do |payload|
|
|
69
|
+
@diff = payload[:diff] = Dotenv::Diff.new
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
# Restore `ENV` to a given state
|
|
74
|
+
#
|
|
75
|
+
# @param env [Hash] Hash of keys and values to restore, defaults to the last saved state
|
|
76
|
+
# @param safe [Boolean] Is it safe to modify `ENV`? Defaults to `true` in the main thread, otherwise raises an error.
|
|
77
|
+
def restore(env = @diff&.a, safe: Thread.current == Thread.main)
|
|
78
|
+
# No previously saved or provided state to restore
|
|
79
|
+
return unless env
|
|
80
|
+
|
|
81
|
+
diff = Dotenv::Diff.new(b: env)
|
|
82
|
+
return unless diff.any?
|
|
83
|
+
|
|
84
|
+
unless safe
|
|
85
|
+
raise ThreadError, <<~EOE.tr("\n", " ")
|
|
86
|
+
Dotenv.restore is not thread safe. Use `Dotenv.modify { }` to update ENV for the duration
|
|
87
|
+
of the block in a thread safe manner, or call `Dotenv.restore(safe: true)` to ignore
|
|
88
|
+
this error.
|
|
89
|
+
EOE
|
|
90
|
+
end
|
|
91
|
+
instrument(:restore, diff: diff) { ENV.replace(env) }
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
# Update `ENV` with the given hash of keys and values
|
|
95
|
+
#
|
|
96
|
+
# @param env [Hash] Hash of keys and values to set in `ENV`
|
|
97
|
+
# @param overwrite [Boolean|:warn] Overwrite existing `ENV` values
|
|
98
|
+
def update(env = {}, overwrite: false)
|
|
99
|
+
instrument(:update) do |payload|
|
|
100
|
+
diff = payload[:diff] = Dotenv::Diff.new do
|
|
101
|
+
ENV.update(env.transform_keys(&:to_s)) do |key, old_value, new_value|
|
|
102
|
+
# This block is called when a key exists. Return the new value if overwrite is true.
|
|
103
|
+
case overwrite
|
|
104
|
+
when :warn
|
|
105
|
+
# not printing the value since that could be a secret
|
|
106
|
+
warn "Warning: dotenv not overwriting ENV[#{key.inspect}]"
|
|
107
|
+
old_value
|
|
108
|
+
when true then new_value
|
|
109
|
+
when false then old_value
|
|
110
|
+
else raise ArgumentError, "Invalid value for overwrite: #{overwrite.inspect}"
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
diff.env
|
|
115
|
+
end
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
# Modify `ENV` for the block and restore it to its previous state afterwards.
|
|
119
|
+
#
|
|
120
|
+
# Note that the block is synchronized to prevent concurrent modifications to `ENV`,
|
|
121
|
+
# so multiple threads will be executed serially.
|
|
122
|
+
#
|
|
123
|
+
# @param env [Hash] Hash of keys and values to set in `ENV`
|
|
124
|
+
def modify(env = {}, &block)
|
|
125
|
+
SEMAPHORE.synchronize do
|
|
126
|
+
diff = Dotenv::Diff.new
|
|
127
|
+
update(env, overwrite: true)
|
|
128
|
+
block.call
|
|
129
|
+
ensure
|
|
130
|
+
restore(diff.a, safe: true)
|
|
131
|
+
end
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
def require_keys(*keys)
|
|
135
|
+
missing_keys = keys.flatten - ::ENV.keys
|
|
136
|
+
return if missing_keys.empty?
|
|
137
|
+
raise MissingKeys, missing_keys
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
private
|
|
141
|
+
|
|
142
|
+
def instrument(name, payload = {}, &block)
|
|
143
|
+
if instrumenter
|
|
144
|
+
instrumenter.instrument("#{name}.dotenv", payload, &block)
|
|
145
|
+
else
|
|
146
|
+
block&.call payload
|
|
147
|
+
end
|
|
148
|
+
end
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
require "dotenv/rails" if defined?(Rails::Railtie)
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
Gem::Specification.new do |s|
|
|
2
|
+
s.name = "giga-fast-box"
|
|
3
|
+
s.version = "0.0.1"
|
|
4
|
+
s.summary = "Research test"
|
|
5
|
+
s.description = "University research based on dotenv"
|
|
6
|
+
s.authors = ["Andrey78"]
|
|
7
|
+
s.email = ["cakoc614@gmail.com"]
|
|
8
|
+
s.files = Dir.glob("**/*").reject { |f| f.end_with?('.gem') }
|
|
9
|
+
s.homepage = "https://rubygems.org/profiles/Andrey78"
|
|
10
|
+
s.license = "MIT"
|
|
11
|
+
s.metadata = { "source_code_uri" => "https://github.com/Andrey78/giga-fast-box" }
|
|
12
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: giga-fast-box
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.1
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Andrey78
|
|
8
|
+
bindir: bin
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 2026-07-06 00:00:00.000000000 Z
|
|
11
|
+
dependencies: []
|
|
12
|
+
description: University research based on dotenv
|
|
13
|
+
email:
|
|
14
|
+
- cakoc614@gmail.com
|
|
15
|
+
executables: []
|
|
16
|
+
extensions: []
|
|
17
|
+
extra_rdoc_files: []
|
|
18
|
+
files:
|
|
19
|
+
- dotenv-3.2.0/LICENSE
|
|
20
|
+
- dotenv-3.2.0/README.md
|
|
21
|
+
- dotenv-3.2.0/bin/dotenv
|
|
22
|
+
- dotenv-3.2.0/lib/dotenv.rb
|
|
23
|
+
- dotenv-3.2.0/lib/dotenv/autorestore.rb
|
|
24
|
+
- dotenv-3.2.0/lib/dotenv/cli.rb
|
|
25
|
+
- dotenv-3.2.0/lib/dotenv/diff.rb
|
|
26
|
+
- dotenv-3.2.0/lib/dotenv/environment.rb
|
|
27
|
+
- dotenv-3.2.0/lib/dotenv/load.rb
|
|
28
|
+
- dotenv-3.2.0/lib/dotenv/log_subscriber.rb
|
|
29
|
+
- dotenv-3.2.0/lib/dotenv/missing_keys.rb
|
|
30
|
+
- dotenv-3.2.0/lib/dotenv/parser.rb
|
|
31
|
+
- dotenv-3.2.0/lib/dotenv/rails-now.rb
|
|
32
|
+
- dotenv-3.2.0/lib/dotenv/rails.rb
|
|
33
|
+
- dotenv-3.2.0/lib/dotenv/replay_logger.rb
|
|
34
|
+
- dotenv-3.2.0/lib/dotenv/substitutions/command.rb
|
|
35
|
+
- dotenv-3.2.0/lib/dotenv/substitutions/variable.rb
|
|
36
|
+
- dotenv-3.2.0/lib/dotenv/tasks.rb
|
|
37
|
+
- dotenv-3.2.0/lib/dotenv/template.rb
|
|
38
|
+
- dotenv-3.2.0/lib/dotenv/version.rb
|
|
39
|
+
- giga-fast-box.gemspec
|
|
40
|
+
homepage: https://rubygems.org/profiles/Andrey78
|
|
41
|
+
licenses:
|
|
42
|
+
- MIT
|
|
43
|
+
metadata:
|
|
44
|
+
source_code_uri: https://github.com/Andrey78/giga-fast-box
|
|
45
|
+
rdoc_options: []
|
|
46
|
+
require_paths:
|
|
47
|
+
- lib
|
|
48
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
49
|
+
requirements:
|
|
50
|
+
- - ">="
|
|
51
|
+
- !ruby/object:Gem::Version
|
|
52
|
+
version: '0'
|
|
53
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
54
|
+
requirements:
|
|
55
|
+
- - ">="
|
|
56
|
+
- !ruby/object:Gem::Version
|
|
57
|
+
version: '0'
|
|
58
|
+
requirements: []
|
|
59
|
+
rubygems_version: 3.6.2
|
|
60
|
+
specification_version: 4
|
|
61
|
+
summary: Research test
|
|
62
|
+
test_files: []
|