yaml2env 0.1.2 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +6 -1
- data/.travis.yml +0 -1
- data/Gemfile +3 -1
- data/README.textile +144 -21
- data/Rakefile +1 -2
- data/TODO +13 -0
- data/lib/yaml2env/version.rb +1 -1
- data/lib/yaml2env.rb +246 -12
- data/spec/fixtures/example2.yml +19 -0
- data/spec/spec_helper.rb +49 -4
- data/spec/yaml2env_spec.rb +668 -55
- data/yaml2env.gemspec +8 -8
- metadata +97 -74
data/.gitignore
CHANGED
data/.travis.yml
CHANGED
data/Gemfile
CHANGED
@@ -1,11 +1,13 @@
|
|
1
1
|
source "http://rubygems.org"
|
2
2
|
|
3
|
-
# Specify your gem's dependencies in yaml2env.gemspec
|
4
3
|
gemspec
|
5
4
|
|
6
5
|
group :test do
|
7
6
|
group :darwin do
|
8
7
|
gem 'rb-fsevent'
|
9
8
|
end
|
9
|
+
|
10
|
+
# Solving runner bug: https://github.com/guard/guard-minitest/pull/25
|
11
|
+
gem 'guard-minitest', :git => 'https://github.com/grimen/guard-minitest'
|
10
12
|
end
|
11
13
|
|
data/README.textile
CHANGED
@@ -22,13 +22,13 @@ Add to your @Gemfile@:
|
|
22
22
|
|
23
23
|
h2. Usage
|
24
24
|
|
25
|
-
To give this some context; this is how we use @
|
25
|
+
To give this some context; this is how we use @Yaml2env@ to initialize "Hoptoad":http://hoptoadapp.com:
|
26
26
|
|
27
27
|
<pre>
|
28
|
-
|
28
|
+
Yaml2env.require! 'config/hoptoad.yml', {'HOPTOAD_API_KEY' => 'api_key'}
|
29
29
|
|
30
30
|
# ...or if a warning note in the logs is enough:
|
31
|
-
#
|
31
|
+
# Yaml2env.require 'config/hoptoad.yml', {'HOPTOAD_API_KEY' => 'api_key'}
|
32
32
|
|
33
33
|
if defined?(HoptoadNotifier)
|
34
34
|
HoptoadNotifier.configure do |config|
|
@@ -56,37 +56,160 @@ To give this some context; this is how we use @Yaml2Env@ to initialize "Hoptoad"
|
|
56
56
|
...which will yield:
|
57
57
|
|
58
58
|
<pre>
|
59
|
-
# Rails.env => 'development'
|
60
|
-
ENV['HOPTOAD_API_KEY']
|
59
|
+
# For: Rails.env => 'development'
|
60
|
+
ENV['HOPTOAD_API_KEY']
|
61
|
+
=> 'NONE'
|
61
62
|
|
62
|
-
# Rails.env => 'staging'
|
63
|
-
ENV['HOPTOAD_API_KEY']
|
63
|
+
# For: Rails.env => 'staging'
|
64
|
+
ENV['HOPTOAD_API_KEY']
|
65
|
+
=> '123abc'
|
64
66
|
|
65
|
-
# Rails.env => 'production'
|
66
|
-
ENV['HOPTOAD_API_KEY']
|
67
|
+
# For: Rails.env => 'production'
|
68
|
+
ENV['HOPTOAD_API_KEY']
|
69
|
+
=> 'abc123'
|
67
70
|
|
68
|
-
# Rails.env => 'test'
|
69
|
-
ENV['HOPTOAD_API_KEY']
|
71
|
+
# For: Rails.env => 'test'
|
72
|
+
ENV['HOPTOAD_API_KEY']
|
73
|
+
=> 'NONE'
|
70
74
|
|
71
|
-
# Rails.env => 'other'
|
72
|
-
|
75
|
+
# For: Rails.env => 'other'
|
76
|
+
=> STDOUT: "Failed to load required config for environment 'other': /Users/grimen/development/example.com/config/hoptoad.yml"
|
73
77
|
</pre>
|
74
78
|
|
75
|
-
h2.
|
79
|
+
h2. API
|
76
80
|
|
77
|
-
|
81
|
+
Being lazy and just dropping a lot of examples here.
|
82
|
+
|
83
|
+
*@Yaml2env.require@*
|
84
|
+
|
85
|
+
<pre>
|
86
|
+
# Case: If config file exists with proper keys
|
87
|
+
Yaml2env.require 'config/hoptoad.yml', {'HOPTOAD_API_KEY' => 'api_key'}
|
88
|
+
=> true
|
89
|
+
Yaml2env.require 'config/hoptoad.yml', {'HOPTOAD_API_KEY' => 'api_key'}
|
90
|
+
=> false + STDOUT: (already loaded warning)
|
91
|
+
|
92
|
+
# Case: If config file don't exists, or it don't contain expected setting-key(s)
|
93
|
+
Yaml2env.require 'config/hoptoad2.yml', {'HOPTOAD_API_KEY' => 'api_key'}
|
94
|
+
=> false + STDOUT: (invalid file or missing key warning)
|
95
|
+
</pre>
|
96
|
+
|
97
|
+
*@Yaml2env.require!@*
|
98
|
+
|
99
|
+
See above: Same as @Yaml2env.require@ but raises error instead of log warning.
|
100
|
+
|
101
|
+
*@Yaml2env.load@*
|
102
|
+
|
103
|
+
<pre>
|
104
|
+
# Case: If config file exists with proper keys
|
105
|
+
Yaml2env.load 'config/hoptoad.yml', {'HOPTOAD_API_KEY' => 'api_key'}
|
106
|
+
=> true
|
107
|
+
|
108
|
+
# Case: If config file don't exists, or it don't contain expected setting-key(s)
|
109
|
+
Yaml2env.load 'config/hoptoad.yml', {'HOPTOAD_API_KEY' => 'api_key'}
|
110
|
+
=> STDOUT: (warning)
|
111
|
+
</pre>
|
112
|
+
|
113
|
+
*@Yaml2env.load!@*
|
114
|
+
|
115
|
+
See above: Same as @Yaml2env.require@ but raises error instead of log warning.
|
116
|
+
|
117
|
+
*@Yaml2env.assert_keys@*
|
78
118
|
|
79
119
|
<pre>
|
80
|
-
|
81
|
-
|
120
|
+
Yaml2env.assert_keys 'HOPTOAD_API_KEY'
|
121
|
+
=> true
|
122
|
+
|
123
|
+
Yaml2env.assert_keys 'BAZOOKA'
|
124
|
+
=> false + STDOUT: (warning)
|
125
|
+
</pre>
|
126
|
+
|
127
|
+
*@Yaml2env.assert_keys!@*
|
82
128
|
|
83
|
-
|
84
|
-
|
129
|
+
See above: Same as @Yaml2env.assert_keys@ but raises error instead of log warning.
|
130
|
+
|
131
|
+
*@Yaml2env.assert_values@*
|
132
|
+
|
133
|
+
<pre>
|
134
|
+
Yaml2env.assert_values 'HOPTOAD_API_KEY' => /[a-z0-9]+/
|
135
|
+
=> true
|
85
136
|
|
86
|
-
|
87
|
-
|
137
|
+
Yaml2env.assert_values 'HOPTOAD_API_KEY' => /[0-9]+/
|
138
|
+
=> false + STDOUT: (warning)
|
139
|
+
</pre>
|
140
|
+
|
141
|
+
*@Yaml2env.assert_values!@*
|
142
|
+
|
143
|
+
See above: Same as @Yaml2env.assert_values@ but raises error instead of log warning.
|
144
|
+
|
145
|
+
*@Yaml2env.log_root@*
|
146
|
+
|
147
|
+
<pre>
|
148
|
+
Yaml2env.log_root
|
149
|
+
=> STDOUT: :: Yaml2env.root = '/path/to/project/root'
|
88
150
|
</pre>
|
89
151
|
|
152
|
+
*@Yaml2env.log_env@*
|
153
|
+
|
154
|
+
<pre>
|
155
|
+
Yaml2env.log_env
|
156
|
+
=> STDOUT: :: Yaml2env.env = 'development' (default)
|
157
|
+
</pre>
|
158
|
+
|
159
|
+
*@Yaml2env.log_values@*
|
160
|
+
|
161
|
+
<pre>
|
162
|
+
Yaml2env.log_values
|
163
|
+
=> STDOUT: :: ENV = {'HOPTOAD_API_KEY' => 'api_key', 'RUBY_VERSION' => 'ruby-1.9.3-p0', ...}
|
164
|
+
|
165
|
+
Yaml2env.log_values 'HOPTOAD_API_KEY'
|
166
|
+
=> STDOUT: :: ENV = {'HOPTOAD_API_KEY' => 'api_key'}
|
167
|
+
|
168
|
+
Yaml2env.log_values 'BAZOOKA'
|
169
|
+
=> STDOUT: :: ENV = {}
|
170
|
+
|
171
|
+
Yaml2env.log_values /RACK|MERCHII/
|
172
|
+
=> STDOUT: :: ENV = {'RACK_ENV' => 'api_key', 'MERCHII_ASSETS_DOMAIN' => 'assets.merchii.com'}
|
173
|
+
</pre>
|
174
|
+
|
175
|
+
*@Yaml2env.detect_root!@*
|
176
|
+
|
177
|
+
<pre>
|
178
|
+
# For: Rack, Sinatra, Rails, or Yaml2env.default_env is set
|
179
|
+
Yaml2env.detect_root!
|
180
|
+
=> Yaml2env.root = '/path/to/project/root'
|
181
|
+
|
182
|
+
# For: Failed detection
|
183
|
+
Yaml2env.detect_root!
|
184
|
+
=> DetectionFailedError
|
185
|
+
</pre>
|
186
|
+
|
187
|
+
*@Yaml2env.detect_env!@*
|
188
|
+
|
189
|
+
<pre>
|
190
|
+
# For: Rack, Sinatra, Rails, or Yaml2env.default_env is set
|
191
|
+
Yaml2env.detect_env!
|
192
|
+
=> Yaml2env.env = ENV['RACK_ENV'] # ...example for Rack
|
193
|
+
|
194
|
+
# For: Failed detection
|
195
|
+
Yaml2env.detect_env!
|
196
|
+
=> DetectionFailedError
|
197
|
+
|
198
|
+
# For: Default environment set
|
199
|
+
Yaml2env.default_env = 'development'
|
200
|
+
Yaml2env.detect_env!
|
201
|
+
=> Yaml2env.env = 'development'
|
202
|
+
</pre>
|
203
|
+
|
204
|
+
*@Yaml2env.loaded@*
|
205
|
+
|
206
|
+
<pre>
|
207
|
+
Yaml2env.loaded
|
208
|
+
=> STDOUT: '/path/to/project/root/config/hoptoad.yml' => {'HOPTOAD_API_KEY' => 'abc123'}
|
209
|
+
</pre>
|
210
|
+
|
211
|
+
There are a few more, but these are the most useful ones.
|
212
|
+
|
90
213
|
h2. Notes
|
91
214
|
|
92
215
|
This gem was developed for our own requirements at *"Merchii":http://github.com/merchii*, so feel free to send pull-requests with enhancements of any kind (features, bug-fixes, documentation, tests, etc.) to make it better or useful for you as well.
|
data/Rakefile
CHANGED
data/TODO
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
|
2
|
+
== NEXT
|
3
|
+
|
4
|
+
- [feature]: Yaml2env.log_files => Array
|
5
|
+
|
6
|
+
- [enhancement]: Make use of 'awesome_print' - with fallback on 'pretty_print' - without breaking Ruby specs; "ordered hash keys" issue causes pain here so disabled it temporarily to get a green build.
|
7
|
+
|
8
|
+
|
9
|
+
== WOULD-BE-NICE
|
10
|
+
|
11
|
+
- [review]: Find out best practice for testing Pathname vs. String like this: Pathname.new("/tmp") == "/tmp"
|
12
|
+
|
13
|
+
- [spec]: Find out why many specs fail when Yaml2env.env is a ActiveSupport::StringInquirer - works in Rails.
|
data/lib/yaml2env/version.rb
CHANGED
data/lib/yaml2env.rb
CHANGED
@@ -1,5 +1,15 @@
|
|
1
1
|
require 'yaml'
|
2
2
|
require 'logger'
|
3
|
+
require 'active_support/string_inquirer'
|
4
|
+
require 'active_support/hash_with_indifferent_access'
|
5
|
+
require 'active_support/ordered_hash'
|
6
|
+
require 'active_support/core_ext/object/blank'
|
7
|
+
require 'pp'
|
8
|
+
begin
|
9
|
+
require 'awesome_print'
|
10
|
+
rescue LoadError
|
11
|
+
# optional
|
12
|
+
end
|
3
13
|
|
4
14
|
module Yaml2env
|
5
15
|
|
@@ -8,6 +18,9 @@ module Yaml2env
|
|
8
18
|
class Error < ::StandardError
|
9
19
|
end
|
10
20
|
|
21
|
+
class ArgumentError < ::ArgumentError
|
22
|
+
end
|
23
|
+
|
11
24
|
class DetectionFailedError < Error
|
12
25
|
end
|
13
26
|
|
@@ -17,6 +30,18 @@ module Yaml2env
|
|
17
30
|
class MissingConfigKeyError < Error
|
18
31
|
end
|
19
32
|
|
33
|
+
class InvalidConfigValueError < Error
|
34
|
+
end
|
35
|
+
|
36
|
+
class InvalidRootError < ArgumentError
|
37
|
+
end
|
38
|
+
|
39
|
+
class AlreadyLoadedError < Error
|
40
|
+
end
|
41
|
+
|
42
|
+
class HumanError < Error
|
43
|
+
end
|
44
|
+
|
20
45
|
# Hash tracking all of the loaded ENV-values via Yaml2env.load.
|
21
46
|
LOADED_ENV = {}
|
22
47
|
|
@@ -32,15 +57,51 @@ module Yaml2env
|
|
32
57
|
# Default: +::Logger.new(::STDOUT)+
|
33
58
|
@@logger = ::Logger.new(::STDOUT)
|
34
59
|
|
60
|
+
# Loaded files and their values.
|
61
|
+
@@loaded = {}
|
62
|
+
|
63
|
+
# Default env.
|
64
|
+
@@default_env = nil
|
65
|
+
|
35
66
|
class << self
|
36
67
|
|
37
|
-
[:
|
68
|
+
[:default_env, :logger].each do |name|
|
38
69
|
define_method name do
|
39
|
-
class_variable_get "@@#{name}"
|
70
|
+
class_variable_get :"@@#{name}"
|
71
|
+
end
|
72
|
+
define_method :"#{name}=" do |value|
|
73
|
+
class_variable_set :"@@#{name}", value
|
40
74
|
end
|
41
|
-
|
42
|
-
|
75
|
+
end
|
76
|
+
|
77
|
+
define_method :'root' do
|
78
|
+
class_variable_get :'@@root'
|
79
|
+
end
|
80
|
+
define_method :'root=' do |value|
|
81
|
+
if value.present?
|
82
|
+
value = File.expand_path(value) rescue value
|
83
|
+
value = Pathname.new(value) rescue value
|
84
|
+
# FIXME: Makes sense, but need to rewrite specs somewhat - later.
|
85
|
+
# if Dir.exists?(value)
|
86
|
+
# value = Pathname.new(value).expand_path
|
87
|
+
# else
|
88
|
+
# raise InvalidRootError, "Trying to set Yaml2env.root to a path that don't exists: #{value.inspect}. Yaml2env.root must point to existing path."
|
89
|
+
# end
|
43
90
|
end
|
91
|
+
class_variable_set :'@@root', value
|
92
|
+
end
|
93
|
+
|
94
|
+
define_method :'env' do
|
95
|
+
class_variable_get :'@@env'
|
96
|
+
end
|
97
|
+
define_method :'env=' do |value|
|
98
|
+
# FIXME: Specs "Yaml2env.env.must_equal" fails in really weird way with this line enabled.
|
99
|
+
# value = ActiveSupport::StringInquirer.new(value.to_s) unless value.is_a?(ActiveSupport::StringInquirer)
|
100
|
+
class_variable_set :'@@env', value
|
101
|
+
end
|
102
|
+
|
103
|
+
define_method :'loaded' do
|
104
|
+
class_variable_get :'@@loaded'
|
44
105
|
end
|
45
106
|
|
46
107
|
alias :environment :env
|
@@ -49,6 +110,8 @@ module Yaml2env
|
|
49
110
|
@@root ||= nil
|
50
111
|
@@env ||= nil
|
51
112
|
@@logger ||= ::Logger.new(::STDOUT)
|
113
|
+
@@loaded ||= {}
|
114
|
+
@@default_env ||= nil
|
52
115
|
end
|
53
116
|
|
54
117
|
def configure
|
@@ -62,7 +125,9 @@ module Yaml2env
|
|
62
125
|
config ||= {}
|
63
126
|
|
64
127
|
begin
|
65
|
-
|
128
|
+
unless File.exists?(config_path)
|
129
|
+
config_path = File.expand_path(File.join(self.root, config_path)).to_s
|
130
|
+
end
|
66
131
|
config = self.load_config_for_env(config_path, self.env)
|
67
132
|
rescue
|
68
133
|
raise ConfigLoadingError, "Failed to load required config for environment '#{self.env}': #{config_path}"
|
@@ -70,31 +135,62 @@ module Yaml2env
|
|
70
135
|
|
71
136
|
# Merge required + optional keys.
|
72
137
|
keys_values = optional_keys.merge(required_keys)
|
138
|
+
loaded_key_values = ActiveSupport::OrderedHash.new
|
73
139
|
|
74
140
|
# Stash found keys from the config into ENV.
|
75
141
|
keys_values.each do |extected_env_key, extected_yaml_key|
|
76
|
-
|
77
|
-
|
142
|
+
config_value = config[extected_yaml_key.to_s]
|
143
|
+
::Yaml2env::LOADED_ENV[extected_env_key.to_s] = ::ENV[extected_env_key.to_s] = config_value
|
144
|
+
info ":: ENV[#{extected_env_key.inspect}] = #{::ENV[extected_env_key.to_s].inspect}"
|
78
145
|
end
|
79
146
|
|
147
|
+
self.loaded[config_path] = config
|
148
|
+
|
80
149
|
# Raise error if any credentials are missing.
|
81
150
|
required_keys.keys.each do |env_key|
|
82
151
|
::Yaml2env::LOADED_ENV[env_key.to_s] ||
|
83
152
|
raise(MissingConfigKeyError, "ENV variable '#{env_key}' needs to be set. Query: #{keys_values.inspect}. Found: #{config.inspect}")
|
84
153
|
end
|
154
|
+
|
155
|
+
true
|
85
156
|
end
|
86
157
|
|
87
158
|
def load(config_path, required_keys = {}, optional_keys = {})
|
159
|
+
args = [config_path, required_keys, optional_keys]
|
160
|
+
|
88
161
|
begin
|
89
162
|
self.load!(config_path, required_keys, optional_keys)
|
90
163
|
rescue Error => e
|
91
|
-
|
92
|
-
::Yaml2env.logger.warn("[Yaml2env]: #{e} -- called from: #{__FILE__})")
|
93
|
-
end
|
164
|
+
warn "[Yaml2env]: #{e} -- arguments: #{args.inspect})"
|
94
165
|
end
|
95
166
|
true
|
96
167
|
end
|
97
168
|
|
169
|
+
public # force public - we are overriding private Ruby method here, but should be all good in the hood. >:)
|
170
|
+
|
171
|
+
def require!(config_path, required_keys = {}, optional_keys = {})
|
172
|
+
self.detect_root!
|
173
|
+
config_path = File.expand_path(File.join(self.root, config_path)).to_s
|
174
|
+
raise AlreadyLoadedError, "Already loaded:" if self.loaded_files.include?(config_path)
|
175
|
+
self.load!(config_path, required_keys, optional_keys)
|
176
|
+
end
|
177
|
+
|
178
|
+
def require(*args)
|
179
|
+
begin
|
180
|
+
self.require!(*args)
|
181
|
+
true
|
182
|
+
rescue AlreadyLoadedError => e
|
183
|
+
false
|
184
|
+
rescue Error => e
|
185
|
+
warn "[Yaml2env]: #{e} -- arguments: #{args.inspect})"
|
186
|
+
false
|
187
|
+
end
|
188
|
+
end
|
189
|
+
|
190
|
+
def loaded_files
|
191
|
+
self.loaded.keys
|
192
|
+
end
|
193
|
+
|
98
194
|
def loaded?(*constant_names)
|
99
195
|
constant_names.all? { |cn| ::Yaml2env::LOADED_ENV.key?(cn.to_s) }
|
100
196
|
end
|
@@ -112,14 +208,16 @@ module Yaml2env
|
|
112
208
|
end
|
113
209
|
|
114
210
|
def detect_env!
|
115
|
-
self.env ||= if ::ENV
|
211
|
+
self.env ||= if ::ENV['RACK_ENV'].present?
|
116
212
|
::ENV['RACK_ENV']
|
117
213
|
elsif defined?(::Rails)
|
118
214
|
::Rails.env
|
119
215
|
elsif defined?(::Sinatra::Application)
|
120
216
|
::Sinatra::Application.environment
|
217
|
+
elsif self.default_env.present?
|
218
|
+
self.default_env
|
121
219
|
else
|
122
|
-
raise DetectionFailedError, "Failed to auto-detect Yaml2env.
|
220
|
+
raise DetectionFailedError, "Failed to auto-detect Yaml2env.env (config environment). Specify environment before loading any configs/initializers using Yaml2env, e.g. Yaml2env.env = 'development'."
|
123
221
|
end
|
124
222
|
end
|
125
223
|
|
@@ -127,7 +225,142 @@ module Yaml2env
|
|
127
225
|
self.logger.respond_to?(:info)
|
128
226
|
end
|
129
227
|
|
228
|
+
def root?
|
229
|
+
self.root.present?
|
230
|
+
end
|
231
|
+
|
232
|
+
def env?(*args)
|
233
|
+
if args.size > 0
|
234
|
+
raise HumanError, "Seriously, what are you trying to do? *<:)" if args.size > 1 && args.count { |a| a.blank? } > 1
|
235
|
+
args.any? do |arg|
|
236
|
+
arg.is_a?(Regexp) ? self.env.to_s =~ arg : self.env.to_s == arg.to_s
|
237
|
+
end
|
238
|
+
else
|
239
|
+
self.env.present?
|
240
|
+
end
|
241
|
+
end
|
242
|
+
|
243
|
+
def default_env?
|
244
|
+
self.env == self.default_env
|
245
|
+
end
|
246
|
+
|
247
|
+
def log_env
|
248
|
+
value = self.env.inspect
|
249
|
+
output = ":: Yaml2env.env = #{value}"
|
250
|
+
output << " (default)" if self.default_env? && self.default_env.present?
|
251
|
+
puts output
|
252
|
+
end
|
253
|
+
|
254
|
+
def log_root
|
255
|
+
value = self.root.present? ? self.root.to_s.inspect : self.root.inspect
|
256
|
+
output = ":: Yaml2env.root = #{value}"
|
257
|
+
puts output
|
258
|
+
end
|
259
|
+
|
260
|
+
def log_values(*args)
|
261
|
+
if args.blank?
|
262
|
+
should_include = proc { true }
|
263
|
+
elsif args.first.is_a?(Regexp)
|
264
|
+
key_pattern = args.shift
|
265
|
+
should_include = proc { |key| key =~ key_pattern }
|
266
|
+
else
|
267
|
+
should_include = proc { |key| args.any? { |key_string| key == key_string; } }
|
268
|
+
end
|
269
|
+
key_values = {}
|
270
|
+
::ENV.keys.sort.each do |k,v|
|
271
|
+
key_values[k] = ENV[k] if should_include.call(k)
|
272
|
+
end
|
273
|
+
print ":: ENV = "
|
274
|
+
puts format_output key_values
|
275
|
+
end
|
276
|
+
|
277
|
+
def assert_keys!(*required_keys)
|
278
|
+
raise ArgumentError, "Expected ENV-keys, but got: #{required_keys.inspect}" if required_keys.blank?
|
279
|
+
raise ArgumentError, "Expected ENV-keys, but got: #{required_keys.inspect}" unless required_keys.first.is_a?(String) || required_keys.first.is_a?(Symbol)
|
280
|
+
|
281
|
+
required_keys = required_keys.collect { |k| k.to_s }
|
282
|
+
missing_keys = required_keys - ::ENV.keys
|
283
|
+
|
284
|
+
if missing_keys.size == 0
|
285
|
+
true
|
286
|
+
else
|
287
|
+
raise MissingConfigKeyError, "Assertion failed, no such ENV-keys loaded: #{missing_keys}"
|
288
|
+
end
|
289
|
+
end
|
290
|
+
|
291
|
+
def assert_keys(*required_keys)
|
292
|
+
begin
|
293
|
+
self.assert_keys!(*required_keys)
|
294
|
+
true
|
295
|
+
rescue Error => e
|
296
|
+
print "[Yaml2env] WARN: Assertion failed, no such ENV-keys loaded: "
|
297
|
+
puts format_output required_keys
|
298
|
+
false
|
299
|
+
end
|
300
|
+
end
|
301
|
+
|
302
|
+
def assert_values!(key_values)
|
303
|
+
raise ArgumentError, "Expected hash, but got: #{key_values.inspect}" unless key_values.is_a?(Hash) && key_values.present?
|
304
|
+
raise ArgumentError, "Expected hash with string-regexp values, but got: #{key_values.inspect}" unless key_values.all? { |k, v| v.is_a?(Regexp) }
|
305
|
+
|
306
|
+
self.assert_keys! *key_values.keys
|
307
|
+
|
308
|
+
failed_assertions = {}
|
309
|
+
|
310
|
+
key_values.each do |k, v|
|
311
|
+
k = k.to_s
|
312
|
+
failed_assertions[k] = ::ENV[k] unless ::ENV[k] =~ v
|
313
|
+
end
|
314
|
+
|
315
|
+
if failed_assertions.keys.size == 0
|
316
|
+
true
|
317
|
+
else
|
318
|
+
raise InvalidConfigValueError, "Assertion failed, invalid values: #{failed_assertions}"
|
319
|
+
end
|
320
|
+
end
|
321
|
+
|
322
|
+
def assert_values(key_values)
|
323
|
+
begin
|
324
|
+
self.assert_values!(key_values)
|
325
|
+
true
|
326
|
+
rescue Error => e
|
327
|
+
print "[Yaml2env] WARN: Assertion failed, invalid values: "
|
328
|
+
puts format_output key_values
|
329
|
+
false
|
330
|
+
end
|
331
|
+
end
|
332
|
+
|
130
333
|
protected
|
334
|
+
|
335
|
+
# Work around helpers to make output specs pass on different Ruby version (ordered hash problem). Grrr...
|
336
|
+
def format_output(array_or_hash)
|
337
|
+
if array_or_hash.is_a?(Array)
|
338
|
+
array_or_hash.collect(&:to_s).sort.collect { |k| "#{k.inspect}" }.join(", ")
|
339
|
+
elsif array_or_hash.is_a?(Hash)
|
340
|
+
array_or_hash = ActiveSupport::HashWithIndifferentAccess.new(array_or_hash)
|
341
|
+
array_or_hash.keys.collect(&:to_s).sort.collect { |k| "#{k.inspect} => #{array_or_hash[k].inspect}" }.join(", ")
|
342
|
+
end
|
343
|
+
end
|
344
|
+
|
345
|
+
def pretty(*args)
|
346
|
+
begin
|
347
|
+
ap *args
|
348
|
+
rescue
|
349
|
+
pp *args
|
350
|
+
end
|
351
|
+
end
|
352
|
+
|
353
|
+
def info(message)
|
354
|
+
self.logger.info message if self.logger?
|
355
|
+
# puts message
|
356
|
+
end
|
357
|
+
|
358
|
+
# FIXME: Should show filepath for calle.
|
359
|
+
def warn(message)
|
360
|
+
self.logger.warn(message) if self.logger?
|
361
|
+
# puts message
|
362
|
+
end
|
363
|
+
|
131
364
|
def load_config(config_file)
|
132
365
|
YAML.load(File.open(config_file))
|
133
366
|
end
|
@@ -136,6 +369,7 @@ module Yaml2env
|
|
136
369
|
config = self.load_config(config_file)
|
137
370
|
config[env]
|
138
371
|
end
|
372
|
+
|
139
373
|
end
|
140
374
|
|
141
375
|
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
development:
|
2
|
+
api_key: DEVELOPMENT_KEY_2
|
3
|
+
api_secret: DEVELOPMENT_SECRET_2
|
4
|
+
|
5
|
+
staging:
|
6
|
+
api_key: STAGING_KEY_2
|
7
|
+
api_secret: STAGING_SECRET_2
|
8
|
+
|
9
|
+
production:
|
10
|
+
api_key: PRODUCTION_KEY_2
|
11
|
+
api_secret: PRODUCTION_SECRET_2
|
12
|
+
|
13
|
+
test:
|
14
|
+
api_key: TEST_KEY_2
|
15
|
+
api_secret: TEST_SECRET_2
|
16
|
+
|
17
|
+
nyan_cat_mode:
|
18
|
+
api_key: NYAN_CAT_MODE_KEY_2
|
19
|
+
api_secret: NYAN_CAT_MODE_SECRET_2
|
data/spec/spec_helper.rb
CHANGED
@@ -1,16 +1,61 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
|
-
require 'rubygems'
|
3
|
-
require 'bundler'
|
4
|
-
Bundler.require
|
5
|
-
|
6
2
|
require 'minitest/autorun'
|
7
3
|
require 'minitest/unit'
|
8
4
|
require 'minitest/spec'
|
9
5
|
require 'minitest/pride'
|
10
6
|
require 'minitest/mock'
|
7
|
+
require 'mocha'
|
11
8
|
|
12
9
|
require 'yaml2env'
|
13
10
|
|
11
|
+
def rack!(loaded)
|
12
|
+
if loaded
|
13
|
+
::ENV['RACK_ROOT'] = '/home/grimen/development/rack_app'
|
14
|
+
::ENV['RACK_ENV'] = 'rack_env'
|
15
|
+
else
|
16
|
+
::ENV['RACK_ROOT'] = nil
|
17
|
+
::ENV['RACK_ENV'] = nil
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def rails!(loaded)
|
22
|
+
if loaded
|
23
|
+
eval <<-EVAL
|
24
|
+
unless defined?(::Rails)
|
25
|
+
module ::Rails
|
26
|
+
class << self
|
27
|
+
attr_accessor :root, :env
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
EVAL
|
32
|
+
Rails.root = '/home/grimen/development/rails_app'
|
33
|
+
Rails.env = 'rails_env'
|
34
|
+
else
|
35
|
+
Object.send(:remove_const, :Rails) if defined?(::Rails)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def sinatra!(loaded)
|
40
|
+
if loaded
|
41
|
+
eval <<-EVAL
|
42
|
+
unless defined?(::Sinatra::Application)
|
43
|
+
module ::Sinatra
|
44
|
+
class Application
|
45
|
+
class << self
|
46
|
+
attr_accessor :root, :environment
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
EVAL
|
52
|
+
Sinatra::Application.root = '/home/grimen/development/sinatra_app'
|
53
|
+
Sinatra::Application.environment = 'sinatra_env'
|
54
|
+
else
|
55
|
+
Object.send(:remove_const, :Sinatra) if defined?(::Sinatra::Application)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
14
59
|
def silence_all_warnings
|
15
60
|
# Ruby 1.8: Kernel.silence_warnings { yield }
|
16
61
|
old_verbose = $VERBOSE
|