require_env 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +51 -0
- data/Rakefile +34 -0
- data/lib/require_env.rb +57 -0
- data/lib/require_env/missing.rb +13 -0
- data/lib/require_env/version.rb +3 -0
- data/test/require_env_test.rb +100 -0
- data/test/test.yml +4 -0
- data/test/test_helper.rb +16 -0
- metadata +70 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 8873d2fe4dd5389196b6854022c31f7bfc49f757
|
4
|
+
data.tar.gz: 01ac5d2b6ef15c421f72bc265ebd2fe3e38b51cf
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 871c3aa4c3fa584e68e521ec607e7058589f21869a3446c00f33a98d7e14641ea873d709ce6913d58cc36b5d91687533a4986120532d2b74f4a2547308ab0b53
|
7
|
+
data.tar.gz: a792a8397f994323bb6fed208119061de24a76c192304fae00df409dc8d3e1104d62727d67d01042db352fb1e742694c9396b77f9892afb7fb873fcf4fd0b7e2
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2016 Geoffroy Planquart
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
# RequireEnv
|
2
|
+
|
3
|
+
Check environment variable presence easily.
|
4
|
+
|
5
|
+
## Install
|
6
|
+
|
7
|
+
`bundle install require_env`
|
8
|
+
|
9
|
+
## Usage
|
10
|
+
|
11
|
+
Create a YAML file, e.g. `config/environment.yml`, where you define the environment variables
|
12
|
+
required by your application, for the different environment it will run
|
13
|
+
(test, development, production by default)
|
14
|
+
|
15
|
+
- You can use ERB code to provide computed values
|
16
|
+
- You can use YAML defaults.
|
17
|
+
- An optional default value can be given for any key
|
18
|
+
- Keys ending with _IF_OTHER_VARIABLE will only be checked if OTHER_VARIABLE is defined and not equal 'false'
|
19
|
+
- Default value have precedence over the _IF_OTHER_VARIABLE rule
|
20
|
+
|
21
|
+
Here is an example presenting the different possibilities:
|
22
|
+
|
23
|
+
```yaml
|
24
|
+
DEFAULTS: &DEFAULTS
|
25
|
+
BEANSTALK_URL: 'beanstalk://127.0.0.1'
|
26
|
+
|
27
|
+
SLACK_TOKEN:
|
28
|
+
|
29
|
+
# Use the environment variable TOKEN_SCALINGO as a default value if provided
|
30
|
+
SCALINGO_TOKEN: "<%= ENV['TOKEN_SCALINGO'] %>"
|
31
|
+
|
32
|
+
# Declare STRIPE_ENABLED to check for STRIPE_PUBLIC_KEY and STRIPE_SECRET_KEY
|
33
|
+
STRIPE_PUBLIC_KEY_IF_STRIPE_ENABLED:
|
34
|
+
STRIPE_SECRET_KEY_IF_STRIPE_ENABLED:
|
35
|
+
|
36
|
+
test:
|
37
|
+
<<: *DEFAULTS
|
38
|
+
IGNORE_EVENT_DELAY: 'true'
|
39
|
+
|
40
|
+
development:
|
41
|
+
<<: *DEFAULTS
|
42
|
+
IGNORE_EVENT_DELAY: 'true'
|
43
|
+
|
44
|
+
production:
|
45
|
+
<<: *DEFAULTS
|
46
|
+
```
|
47
|
+
|
48
|
+
When your application starts, simply call `RequireEnv.check(Rails.root.join('config', 'environment.yml'))`
|
49
|
+
to check if the required environment variables are provided.
|
50
|
+
|
51
|
+
If any variable is missing, `RequireEnv::Missing` will be raised, detailing the missing environment variable.
|
data/Rakefile
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
begin
|
2
|
+
require 'bundler/setup'
|
3
|
+
rescue LoadError
|
4
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
5
|
+
end
|
6
|
+
|
7
|
+
require 'rdoc/task'
|
8
|
+
|
9
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
10
|
+
rdoc.rdoc_dir = 'rdoc'
|
11
|
+
rdoc.title = 'RequireEnv'
|
12
|
+
rdoc.options << '--line-numbers'
|
13
|
+
rdoc.rdoc_files.include('README.rdoc')
|
14
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
|
+
end
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
|
22
|
+
Bundler::GemHelper.install_tasks
|
23
|
+
|
24
|
+
require 'rake/testtask'
|
25
|
+
|
26
|
+
Rake::TestTask.new(:test) do |t|
|
27
|
+
t.libs << 'lib'
|
28
|
+
t.libs << 'test'
|
29
|
+
t.pattern = 'test/**/*_test.rb'
|
30
|
+
t.verbose = false
|
31
|
+
end
|
32
|
+
|
33
|
+
|
34
|
+
task default: :test
|
data/lib/require_env.rb
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'require_env/missing'
|
2
|
+
require 'safe_yaml/load'
|
3
|
+
require 'erb'
|
4
|
+
|
5
|
+
module RequireEnv
|
6
|
+
def self.check(*files)
|
7
|
+
check_multiple_requirements(files.map do |file|
|
8
|
+
requirements = requirements_from_file(file)[application_environment]
|
9
|
+
if requirements.nil?
|
10
|
+
raise ArgumentError.new("#{file} does not define anything for environment: #{application_environment}")
|
11
|
+
end
|
12
|
+
[file, requirements]
|
13
|
+
end)
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.requirements_from_file(file)
|
17
|
+
if File.exist?(file)
|
18
|
+
SafeYAML.load(ERB.new(File.read(file)).result)
|
19
|
+
else
|
20
|
+
raise ArgumentError.new("File not found: #{file}")
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.check_multiple_requirements(multiple_requirements)
|
25
|
+
missing_requirements = multiple_requirements.map do |file, requirements|
|
26
|
+
missing = check_requirements(requirements)
|
27
|
+
if missing.present?
|
28
|
+
[file, missing]
|
29
|
+
end
|
30
|
+
end.compact
|
31
|
+
|
32
|
+
if missing_requirements.present?
|
33
|
+
raise RequireEnv::Missing.new(missing_requirements)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def self.check_requirements(requirements)
|
38
|
+
requirements.map do |key, value|
|
39
|
+
if ENV.has_key?(key) || ENV.has_key?(key.split('_IF_').first)
|
40
|
+
next nil
|
41
|
+
elsif value
|
42
|
+
ENV[key] = value.to_s
|
43
|
+
next nil
|
44
|
+
elsif key.include?('_IF_')
|
45
|
+
if (condition = ENV[key.split('_IF_').last]) && condition != 'false'
|
46
|
+
next key.split('_IF_').first
|
47
|
+
end
|
48
|
+
else
|
49
|
+
next key
|
50
|
+
end
|
51
|
+
end.compact
|
52
|
+
end
|
53
|
+
|
54
|
+
def self.application_environment
|
55
|
+
ENV['RAILS_ENV']
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module RequireEnv
|
2
|
+
class Missing < StandardError
|
3
|
+
def initialize(requirements)
|
4
|
+
message = "Missing environment variables!\n"
|
5
|
+
requirements.each do |file, reqs|
|
6
|
+
message += "- Declared in `#{file}':\n"
|
7
|
+
message += reqs.map{|env| "\t- #{env}"}.join("\n")
|
8
|
+
message += "\n"
|
9
|
+
end
|
10
|
+
super(message)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,100 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class RequireEnvTest < ActiveSupport::TestCase
|
4
|
+
setup do
|
5
|
+
ENV.delete('RAILS_ENV')
|
6
|
+
ENV.delete('VAR')
|
7
|
+
ENV.delete('SUM')
|
8
|
+
ENV.delete('FOO')
|
9
|
+
ENV.delete('BAR')
|
10
|
+
ENV.delete('BAZ')
|
11
|
+
end
|
12
|
+
|
13
|
+
test 'truth' do
|
14
|
+
assert_kind_of Module, RequireEnv
|
15
|
+
end
|
16
|
+
|
17
|
+
test 'check' do
|
18
|
+
file = File.expand_path('../test.yml', __FILE__)
|
19
|
+
|
20
|
+
assert_raise(ArgumentError) do
|
21
|
+
RequireEnv.check(file, file)
|
22
|
+
end
|
23
|
+
|
24
|
+
ENV['RAILS_ENV'] = 'test'
|
25
|
+
assert_raise(RequireEnv::Missing) do
|
26
|
+
RequireEnv.check(file)
|
27
|
+
end
|
28
|
+
|
29
|
+
ENV['FOO'] = 'bar'
|
30
|
+
RequireEnv.check(file)
|
31
|
+
end
|
32
|
+
|
33
|
+
test 'application_environment' do
|
34
|
+
assert_equal nil, RequireEnv.application_environment
|
35
|
+
|
36
|
+
ENV['RAILS_ENV'] = 'test'
|
37
|
+
assert_equal 'test', RequireEnv.application_environment
|
38
|
+
|
39
|
+
ENV['RAILS_ENV'] = 'foo'
|
40
|
+
assert_equal 'foo', RequireEnv.application_environment
|
41
|
+
end
|
42
|
+
|
43
|
+
test 'requirements_from_file' do
|
44
|
+
assert_raise(ArgumentError) do
|
45
|
+
RequireEnv.requirements_from_file('foobar')
|
46
|
+
end
|
47
|
+
|
48
|
+
file = File.expand_path('../test.yml', __FILE__)
|
49
|
+
result = RequireEnv.requirements_from_file(file)
|
50
|
+
|
51
|
+
assert_kind_of Hash, result
|
52
|
+
assert_equal 'foo', result.dig('test', 'VAR')
|
53
|
+
assert_equal 2, result.dig('test', 'SUM')
|
54
|
+
assert_equal nil, result.dig('test', 'FOO')
|
55
|
+
end
|
56
|
+
|
57
|
+
test 'check_requirements' do
|
58
|
+
requirements = {
|
59
|
+
'VAR' => nil,
|
60
|
+
'FOO' => 'BAR',
|
61
|
+
'BAR_IF_CONDITION' => nil,
|
62
|
+
}
|
63
|
+
|
64
|
+
missing = RequireEnv.check_requirements(requirements)
|
65
|
+
assert_equal ['VAR'], missing
|
66
|
+
assert_equal 'BAR', ENV['FOO']
|
67
|
+
|
68
|
+
ENV['CONDITION'] = 'false'
|
69
|
+
ENV['VAR'] = 'bla'
|
70
|
+
missing = RequireEnv.check_requirements(requirements)
|
71
|
+
|
72
|
+
ENV['CONDITION'] = 'foo'
|
73
|
+
missing = RequireEnv.check_requirements(requirements)
|
74
|
+
assert_equal ['BAR'], missing
|
75
|
+
end
|
76
|
+
|
77
|
+
test 'check_multiple_requirements' do
|
78
|
+
multiple_requirements = {
|
79
|
+
'foo.yml' => {
|
80
|
+
'VAR' => nil,
|
81
|
+
'FOO' => 'BAR',
|
82
|
+
},
|
83
|
+
'bar.yml' => {
|
84
|
+
'BAR' => nil,
|
85
|
+
'BAZ' => nil,
|
86
|
+
},
|
87
|
+
}
|
88
|
+
|
89
|
+
ENV['BAR'] = 'test'
|
90
|
+
|
91
|
+
assert_raise(RequireEnv::Missing) do
|
92
|
+
RequireEnv.check_multiple_requirements(multiple_requirements)
|
93
|
+
end
|
94
|
+
ENV['BAZ'] = 'test'
|
95
|
+
assert_raise(RequireEnv::Missing) do
|
96
|
+
RequireEnv.check_multiple_requirements(multiple_requirements)
|
97
|
+
end
|
98
|
+
ENV['VAR'] = 'bla'
|
99
|
+
end
|
100
|
+
end
|
data/test/test.yml
ADDED
data/test/test_helper.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'bundler/setup'
|
2
|
+
require 'simplecov'
|
3
|
+
SimpleCov.configure do
|
4
|
+
add_filter '/test/'
|
5
|
+
end
|
6
|
+
SimpleCov.start if ENV['COVERAGE']
|
7
|
+
|
8
|
+
require 'active_support'
|
9
|
+
require 'minitest/autorun'
|
10
|
+
|
11
|
+
require 'require_env'
|
12
|
+
|
13
|
+
# Load support files
|
14
|
+
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
|
15
|
+
|
16
|
+
ActiveSupport::TestCase.test_order = :random
|
metadata
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: require_env
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Geoffroy Planquart
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-06-23 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: safe_yaml
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.0'
|
27
|
+
description: Check environment variable presence with minimal YAML file
|
28
|
+
email:
|
29
|
+
- geoffroy@planquart.fr
|
30
|
+
executables: []
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- MIT-LICENSE
|
35
|
+
- README.md
|
36
|
+
- Rakefile
|
37
|
+
- lib/require_env.rb
|
38
|
+
- lib/require_env/missing.rb
|
39
|
+
- lib/require_env/version.rb
|
40
|
+
- test/require_env_test.rb
|
41
|
+
- test/test.yml
|
42
|
+
- test/test_helper.rb
|
43
|
+
homepage: https://github.com/Aethelflaed/require_env
|
44
|
+
licenses:
|
45
|
+
- MIT
|
46
|
+
metadata: {}
|
47
|
+
post_install_message:
|
48
|
+
rdoc_options: []
|
49
|
+
require_paths:
|
50
|
+
- lib
|
51
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '0'
|
61
|
+
requirements: []
|
62
|
+
rubyforge_project:
|
63
|
+
rubygems_version: 2.5.1
|
64
|
+
signing_key:
|
65
|
+
specification_version: 4
|
66
|
+
summary: Simple environment variable check
|
67
|
+
test_files:
|
68
|
+
- test/require_env_test.rb
|
69
|
+
- test/test.yml
|
70
|
+
- test/test_helper.rb
|