env_bang 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/.gitignore +18 -0
- data/.travis.yml +5 -0
- data/Gemfile +7 -0
- data/LICENSE.txt +22 -0
- data/README.md +102 -0
- data/Rakefile +10 -0
- data/env_bang.gemspec +23 -0
- data/lib/env_bang/version.rb +3 -0
- data/lib/env_bang.rb +43 -0
- data/test/env_bang_test.rb +47 -0
- data/test/test_helper.rb +14 -0
- metadata +85 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 220e4f75a1ec92e0ce04098d70ef4e163af873cf
|
4
|
+
data.tar.gz: e919ad22b321abf7936a2eb1f00d81d51d2078f5
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 00edd476fb5125e6899e15e7c2e606d0f007bdebbf96557fe409acb0ef61d8ffa8f6d53465a5117695c955813893a645fdc407b35519e79f3dc122236e8b91a9
|
7
|
+
data.tar.gz: 3cc0b1d0080929e7215dc29ea449c1ca91c6b09c5fd941f6d307946a66a125567540ba457b8242b22989b4714532fe61ca8a4ab8ba3f876a4612c7d13fead935
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Jonathan Camenisch
|
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.
|
data/README.md
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
[](https://travis-ci.org/jcamenisch/ENV_BANG)
|
2
|
+
[](https://gemnasium.com/jcamenisch/ENV_BANG)
|
3
|
+
[](https://codeclimate.com/github/jcamenisch/ENV_BANG)
|
4
|
+
[](https://coveralls.io/r/jcamenisch/ENV_BANG)
|
5
|
+
|
6
|
+
# ENV!
|
7
|
+
|
8
|
+
Do a bang-up job managing your environment variables.
|
9
|
+
|
10
|
+
ENV! provides a thin wrapper around ENV to accomplish a few things:
|
11
|
+
|
12
|
+
- Provide a central place to specify what environment variables you intend to use
|
13
|
+
- Fail loudly with an informative error message if environment variables are not
|
14
|
+
properly configured.
|
15
|
+
- Prevent an application from starting up if a needed environment variable is not set.
|
16
|
+
(This is especially helpful in environment like Heroku, as it prevents launch of a
|
17
|
+
new version of your app if it depends on missing environment variables—a problem
|
18
|
+
that might go unnoticed until your customers come across a 500 error.)
|
19
|
+
|
20
|
+
## Installation
|
21
|
+
|
22
|
+
Add this line to your application's Gemfile:
|
23
|
+
|
24
|
+
gem 'env_bang'
|
25
|
+
|
26
|
+
And then execute:
|
27
|
+
|
28
|
+
$ bundle
|
29
|
+
|
30
|
+
Or install it yourself as:
|
31
|
+
|
32
|
+
$ gem install env_bang
|
33
|
+
|
34
|
+
## Usage
|
35
|
+
|
36
|
+
### Basic Configuration
|
37
|
+
|
38
|
+
First, configure your environment variables somewhere in your app’s
|
39
|
+
startup process. In a Rails app, this could live in `config/initializers/env.rb`.
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
ENV!.config do
|
43
|
+
use 'APP_HOST'
|
44
|
+
use 'RAILS_SECRET_TOKEN'
|
45
|
+
use 'STRIPE_SECRET_KEY'
|
46
|
+
use 'STRIPE_PUBLISHABLE_KEY'
|
47
|
+
# ... etc.
|
48
|
+
end
|
49
|
+
```
|
50
|
+
|
51
|
+
A single variable can also be configured with `ENV!.use MY_VAR`, but the `ENV!.config` block
|
52
|
+
will typically contain all the variables for your app.
|
53
|
+
|
54
|
+
Once a variable is specified with `ENV!.use`, you can access it with
|
55
|
+
|
56
|
+
```ruby
|
57
|
+
ENV!['MY_VAR']
|
58
|
+
```
|
59
|
+
|
60
|
+
This will function just like accessing the environment variable directly, except that it will
|
61
|
+
require the variable to be specified with `ENV!.use`, and present in the current environment.
|
62
|
+
If either of these conditions is not met, a KeyError will be raised with an explanation of
|
63
|
+
what needs to be configured.
|
64
|
+
|
65
|
+
### Adding a default value
|
66
|
+
|
67
|
+
In some cases, you don't need to require each environment to set a variable explicitly.
|
68
|
+
You might have a reasonable default value that can be overridden. You can specify a default
|
69
|
+
value with the `:default` option:
|
70
|
+
|
71
|
+
```ruby
|
72
|
+
ENV!.config do
|
73
|
+
# ...
|
74
|
+
use MAIL_DELIVERY_METHOD, default: :smtp
|
75
|
+
# ...
|
76
|
+
end
|
77
|
+
```
|
78
|
+
|
79
|
+
### Adding a description
|
80
|
+
|
81
|
+
When a new team members installs or deploys your project, they may run into a missing
|
82
|
+
environment variable error. It can save them a great deal of time to include documentation
|
83
|
+
on the missing environment variable directly in the error that is raised. To accomplish this,
|
84
|
+
provide a description (of any length) to the `use` method:
|
85
|
+
|
86
|
+
```ruby
|
87
|
+
ENV!.config do
|
88
|
+
use 'RAILS_SECRET_KEY_BASE',
|
89
|
+
'Generate a fresh one with `SecureRandom.urlsafe_base64(64)`; see http://guides.rubyonrails.org/security.html#session-storage'
|
90
|
+
end
|
91
|
+
```
|
92
|
+
|
93
|
+
If someone installs or deploys the app and forgets to set the RAILS_SECRET_KEY_BASE variable in
|
94
|
+
their environment, they will see these instructions immediately upon running the app.
|
95
|
+
|
96
|
+
## Contributing
|
97
|
+
|
98
|
+
1. Fork it
|
99
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
100
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
101
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
102
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/env_bang.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'env_bang/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "env_bang"
|
8
|
+
spec.version = ENV_BANG::VERSION
|
9
|
+
spec.authors = ["Jonathan Camenisch"]
|
10
|
+
spec.email = ["jonathan@camenisch.net"]
|
11
|
+
spec.summary = %q{Do a bang-up job managing your environment variables}
|
12
|
+
spec.description = %q{}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "rake"
|
22
|
+
spec.add_development_dependency "minitest", "~> 5.1"
|
23
|
+
end
|
data/lib/env_bang.rb
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
require "env_bang/version"
|
2
|
+
|
3
|
+
class ENV_BANG
|
4
|
+
class << self
|
5
|
+
def config(&block)
|
6
|
+
instance_eval(&block)
|
7
|
+
end
|
8
|
+
|
9
|
+
def use(var, *args)
|
10
|
+
description = args.first.is_a?(String) && args.shift
|
11
|
+
options = args.last.is_a?(Hash) ? args.pop : {}
|
12
|
+
|
13
|
+
unless ENV.has_key?(var)
|
14
|
+
ENV[var] = options.fetch(:default) do
|
15
|
+
message = "Missing required environment variable: #{var}"
|
16
|
+
message << "--#{description}" if description
|
17
|
+
indent = ' '
|
18
|
+
raise KeyError.new "\n#{indent}#{message.gsub "\n", "\n#{indent}"}\n"
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
used_vars << var
|
23
|
+
end
|
24
|
+
|
25
|
+
def used_vars
|
26
|
+
@used_vars ||= Set.new
|
27
|
+
end
|
28
|
+
|
29
|
+
def [](var)
|
30
|
+
raise KeyError.new("ENV_BANG is not configured to use var #{var}") unless used_vars.include?(var)
|
31
|
+
|
32
|
+
ENV[var]
|
33
|
+
end
|
34
|
+
|
35
|
+
def method_missing(method, *args, &block)
|
36
|
+
ENV.send(method, *args, &block)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def ENV!
|
42
|
+
ENV_BANG
|
43
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require_relative 'test_helper'
|
2
|
+
|
3
|
+
describe ENV_BANG do
|
4
|
+
it "Raises exception if unconfigured ENV var requested" do
|
5
|
+
ENV['UNCONFIGURED'] = 'unconfigured'
|
6
|
+
proc { ENV!['UNCONFIGURED'] }.must_raise KeyError
|
7
|
+
end
|
8
|
+
|
9
|
+
it "Raises exception if configured ENV var is not present" do
|
10
|
+
ENV.delete('NOT_PRESENT')
|
11
|
+
|
12
|
+
proc {
|
13
|
+
ENV!.config do
|
14
|
+
use 'NOT_PRESENT'
|
15
|
+
end
|
16
|
+
}.must_raise KeyError
|
17
|
+
end
|
18
|
+
|
19
|
+
it "Includes provided description in error message" do
|
20
|
+
ENV.delete('NOT_PRESENT')
|
21
|
+
|
22
|
+
e = proc {
|
23
|
+
ENV!.config do
|
24
|
+
use 'NOT_PRESENT', 'You need a NOT_PRESENT var in your ENV'
|
25
|
+
end
|
26
|
+
}.must_raise(KeyError)
|
27
|
+
e.message.must_include 'You need a NOT_PRESENT var in your ENV'
|
28
|
+
end
|
29
|
+
|
30
|
+
it "Uses provided default value if ENV var not already present" do
|
31
|
+
ENV.delete('WASNT_PRESENT')
|
32
|
+
|
33
|
+
ENV!.config do
|
34
|
+
use 'WASNT_PRESENT', default: 'a default value'
|
35
|
+
end
|
36
|
+
ENV!['WASNT_PRESENT'].must_equal 'a default value'
|
37
|
+
end
|
38
|
+
|
39
|
+
it "Returns actual value from ENV if present" do
|
40
|
+
ENV['PRESENT'] = 'present in environment'
|
41
|
+
|
42
|
+
ENV!.config do
|
43
|
+
use 'PRESENT', default: "You won't need this."
|
44
|
+
end
|
45
|
+
ENV!['PRESENT'].must_equal 'present in environment'
|
46
|
+
end
|
47
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'simplecov'
|
2
|
+
require 'coveralls'
|
3
|
+
|
4
|
+
require 'minitest/spec'
|
5
|
+
require 'minitest/pride'
|
6
|
+
require 'minitest/autorun'
|
7
|
+
|
8
|
+
SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
|
9
|
+
SimpleCov::Formatter::HTMLFormatter,
|
10
|
+
Coveralls::SimpleCov::Formatter
|
11
|
+
]
|
12
|
+
SimpleCov.start
|
13
|
+
|
14
|
+
require 'env_bang'
|
metadata
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: env_bang
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '0.1'
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jonathan Camenisch
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-12-07 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rake
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: minitest
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '5.1'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '5.1'
|
41
|
+
description: ''
|
42
|
+
email:
|
43
|
+
- jonathan@camenisch.net
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".gitignore"
|
49
|
+
- ".travis.yml"
|
50
|
+
- Gemfile
|
51
|
+
- LICENSE.txt
|
52
|
+
- README.md
|
53
|
+
- Rakefile
|
54
|
+
- env_bang.gemspec
|
55
|
+
- lib/env_bang.rb
|
56
|
+
- lib/env_bang/version.rb
|
57
|
+
- test/env_bang_test.rb
|
58
|
+
- test/test_helper.rb
|
59
|
+
homepage: ''
|
60
|
+
licenses:
|
61
|
+
- MIT
|
62
|
+
metadata: {}
|
63
|
+
post_install_message:
|
64
|
+
rdoc_options: []
|
65
|
+
require_paths:
|
66
|
+
- lib
|
67
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: '0'
|
72
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
requirements: []
|
78
|
+
rubyforge_project:
|
79
|
+
rubygems_version: 2.1.10
|
80
|
+
signing_key:
|
81
|
+
specification_version: 4
|
82
|
+
summary: Do a bang-up job managing your environment variables
|
83
|
+
test_files:
|
84
|
+
- test/env_bang_test.rb
|
85
|
+
- test/test_helper.rb
|