mixlib-config 2.2.13 → 2.2.18
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/mixlib/config.rb +21 -21
- data/lib/mixlib/config/version.rb +1 -1
- metadata +5 -69
- data/Gemfile +0 -3
- data/README.md +0 -316
- data/Rakefile +0 -32
- data/mixlib-config.gemspec +0 -32
- data/spec/mixlib/config_spec.rb +0 -1301
- data/spec/spec_helper.rb +0 -15
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 382fbe168de40edd2887293fc2849493c45bf48ab7f0b8ad159034ef9a3c1253
|
4
|
+
data.tar.gz: 541b841aaaf67f5f6f810f4b022d551daa99ade2fdef4e0355a30085cccc9f1e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bfa12f87d2d66c24d8b4090225ec65f85954dbe4949eeb009df46137cca9e567548b38af86a569024a7bbe40bf720762cb3b149051172dfc967aef1dfef4150b
|
7
|
+
data.tar.gz: 7f2865b262e58648736c32d4d50edb6c6d832fe25cf0fc4705b6e80bcc037ffdb2bad8c2824d8dbbf226faab2df0485a8e70bb36f2b1d4de66599a065cdeb8ad
|
data/lib/mixlib/config.rb
CHANGED
@@ -141,14 +141,14 @@ module Mixlib
|
|
141
141
|
# <True>:: If the config option exists
|
142
142
|
# <False>:: If the config option does not exist
|
143
143
|
def key?(key)
|
144
|
-
configuration.
|
144
|
+
configuration.key?(key.to_sym) || config_contexts.key?(key.to_sym)
|
145
145
|
end
|
146
146
|
|
147
147
|
alias_method :has_key?, :key?
|
148
148
|
|
149
149
|
def is_default?(key)
|
150
150
|
symbol = key.to_sym
|
151
|
-
if configurables.
|
151
|
+
if configurables.key?(symbol)
|
152
152
|
configurables[symbol].is_default?(configuration)
|
153
153
|
else
|
154
154
|
raise ArgumentError, "config option must exist, and not be a context to check for default values"
|
@@ -250,9 +250,9 @@ module Mixlib
|
|
250
250
|
# === Returns
|
251
251
|
# self
|
252
252
|
def restore(hash)
|
253
|
-
self.configuration = hash.reject { |key, value| config_contexts.
|
253
|
+
self.configuration = hash.reject { |key, value| config_contexts.key?(key) }
|
254
254
|
config_contexts.each do |key, config_context|
|
255
|
-
if hash.
|
255
|
+
if hash.key?(key)
|
256
256
|
config_context.restore(hash[key])
|
257
257
|
else
|
258
258
|
config_context.reset
|
@@ -260,7 +260,7 @@ module Mixlib
|
|
260
260
|
end
|
261
261
|
config_context_lists.each do |key, meta|
|
262
262
|
meta[:values] = []
|
263
|
-
if hash.
|
263
|
+
if hash.key?(key)
|
264
264
|
hash[key].each do |val|
|
265
265
|
context = define_context(meta[:definition_blocks])
|
266
266
|
context.restore(val)
|
@@ -270,7 +270,7 @@ module Mixlib
|
|
270
270
|
end
|
271
271
|
config_context_hashes.each do |key, meta|
|
272
272
|
meta[:values] = {}
|
273
|
-
if hash.
|
273
|
+
if hash.key?(key)
|
274
274
|
hash[key].each do |vkey, val|
|
275
275
|
context = define_context(meta[:definition_blocks])
|
276
276
|
context.restore(val)
|
@@ -289,7 +289,7 @@ module Mixlib
|
|
289
289
|
# self
|
290
290
|
def merge!(hash)
|
291
291
|
hash.each do |key, value|
|
292
|
-
if config_contexts.
|
292
|
+
if config_contexts.key?(key)
|
293
293
|
# Grab the config context and let internal_get cache it if so desired
|
294
294
|
config_contexts[key].restore(value)
|
295
295
|
else
|
@@ -369,7 +369,7 @@ module Mixlib
|
|
369
369
|
# The value of the config option.
|
370
370
|
def configurable(symbol, &block)
|
371
371
|
unless configurables[symbol]
|
372
|
-
if config_contexts.
|
372
|
+
if config_contexts.key?(symbol)
|
373
373
|
raise ReopenedConfigContextWithConfigurableError, "Cannot redefine config_context #{symbol} as a configurable value"
|
374
374
|
end
|
375
375
|
configurables[symbol] = Configurable.new(symbol)
|
@@ -397,11 +397,11 @@ module Mixlib
|
|
397
397
|
# block<Block>: a block that will be run in the context of this new config
|
398
398
|
# class.
|
399
399
|
def config_context(symbol, &block)
|
400
|
-
if configurables.
|
400
|
+
if configurables.key?(symbol)
|
401
401
|
raise ReopenedConfigurableWithConfigContextError, "Cannot redefine config value #{symbol} with a config context"
|
402
402
|
end
|
403
403
|
|
404
|
-
if config_contexts.
|
404
|
+
if config_contexts.key?(symbol)
|
405
405
|
context = config_contexts[symbol]
|
406
406
|
else
|
407
407
|
context = Class.new
|
@@ -435,11 +435,11 @@ module Mixlib
|
|
435
435
|
# block<Block>: a block that will be run in the context of this new config
|
436
436
|
# class.
|
437
437
|
def config_context_list(plural_symbol, singular_symbol, &block)
|
438
|
-
if configurables.
|
438
|
+
if configurables.key?(plural_symbol)
|
439
439
|
raise ReopenedConfigurableWithConfigContextError, "Cannot redefine config value #{plural_symbol} with a config context"
|
440
440
|
end
|
441
441
|
|
442
|
-
unless config_context_lists.
|
442
|
+
unless config_context_lists.key?(plural_symbol)
|
443
443
|
config_context_lists[plural_symbol] = {
|
444
444
|
definition_blocks: [],
|
445
445
|
values: [],
|
@@ -467,11 +467,11 @@ module Mixlib
|
|
467
467
|
# block<Block>: a block that will be run in the context of this new config
|
468
468
|
# class.
|
469
469
|
def config_context_hash(plural_symbol, singular_symbol, &block)
|
470
|
-
if configurables.
|
470
|
+
if configurables.key?(plural_symbol)
|
471
471
|
raise ReopenedConfigurableWithConfigContextError, "Cannot redefine config value #{plural_symbol} with a config context"
|
472
472
|
end
|
473
473
|
|
474
|
-
unless config_context_hashes.
|
474
|
+
unless config_context_hashes.key?(plural_symbol)
|
475
475
|
config_context_hashes[plural_symbol] = {
|
476
476
|
definition_blocks: [],
|
477
477
|
values: {},
|
@@ -604,9 +604,9 @@ module Mixlib
|
|
604
604
|
# value<Object>:: Value to be set in config hash
|
605
605
|
#
|
606
606
|
def internal_set(symbol, value)
|
607
|
-
if configurables.
|
607
|
+
if configurables.key?(symbol)
|
608
608
|
configurables[symbol].set(configuration, value)
|
609
|
-
elsif config_contexts.
|
609
|
+
elsif config_contexts.key?(symbol)
|
610
610
|
config_contexts[symbol].restore(value.to_hash)
|
611
611
|
else
|
612
612
|
if config_strict_mode == :warn
|
@@ -619,13 +619,13 @@ module Mixlib
|
|
619
619
|
end
|
620
620
|
|
621
621
|
def internal_get(symbol)
|
622
|
-
if configurables.
|
622
|
+
if configurables.key?(symbol)
|
623
623
|
configurables[symbol].get(configuration)
|
624
|
-
elsif config_contexts.
|
624
|
+
elsif config_contexts.key?(symbol)
|
625
625
|
config_contexts[symbol]
|
626
|
-
elsif config_context_lists.
|
626
|
+
elsif config_context_lists.key?(symbol)
|
627
627
|
config_context_lists[symbol]
|
628
|
-
elsif config_context_hashes.
|
628
|
+
elsif config_context_hashes.key?(symbol)
|
629
629
|
config_context_hashes[symbol]
|
630
630
|
else
|
631
631
|
if config_strict_mode == :warn
|
@@ -702,7 +702,7 @@ module Mixlib
|
|
702
702
|
# Adds a single new context to the list
|
703
703
|
meta.send :define_method, singular_symbol do |key, &block|
|
704
704
|
context_hash_details = internal_get(plural_symbol)
|
705
|
-
context = if context_hash_details[:values].
|
705
|
+
context = if context_hash_details[:values].key? key
|
706
706
|
context_hash_details[:values][key]
|
707
707
|
else
|
708
708
|
new_context = define_context(context_hash_details[:definition_blocks])
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mixlib-config
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.2.
|
4
|
+
version: 2.2.18
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Chef Software, Inc.
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-12-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: tomlrb
|
@@ -24,85 +24,21 @@ dependencies:
|
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0'
|
27
|
-
- !ruby/object:Gem::Dependency
|
28
|
-
name: rake
|
29
|
-
requirement: !ruby/object:Gem::Requirement
|
30
|
-
requirements:
|
31
|
-
- - ">="
|
32
|
-
- !ruby/object:Gem::Version
|
33
|
-
version: '0'
|
34
|
-
type: :development
|
35
|
-
prerelease: false
|
36
|
-
version_requirements: !ruby/object:Gem::Requirement
|
37
|
-
requirements:
|
38
|
-
- - ">="
|
39
|
-
- !ruby/object:Gem::Version
|
40
|
-
version: '0'
|
41
|
-
- !ruby/object:Gem::Dependency
|
42
|
-
name: rspec
|
43
|
-
requirement: !ruby/object:Gem::Requirement
|
44
|
-
requirements:
|
45
|
-
- - "~>"
|
46
|
-
- !ruby/object:Gem::Version
|
47
|
-
version: '3.0'
|
48
|
-
type: :development
|
49
|
-
prerelease: false
|
50
|
-
version_requirements: !ruby/object:Gem::Requirement
|
51
|
-
requirements:
|
52
|
-
- - "~>"
|
53
|
-
- !ruby/object:Gem::Version
|
54
|
-
version: '3.0'
|
55
|
-
- !ruby/object:Gem::Dependency
|
56
|
-
name: chefstyle
|
57
|
-
requirement: !ruby/object:Gem::Requirement
|
58
|
-
requirements:
|
59
|
-
- - ">="
|
60
|
-
- !ruby/object:Gem::Version
|
61
|
-
version: '0'
|
62
|
-
type: :development
|
63
|
-
prerelease: false
|
64
|
-
version_requirements: !ruby/object:Gem::Requirement
|
65
|
-
requirements:
|
66
|
-
- - ">="
|
67
|
-
- !ruby/object:Gem::Version
|
68
|
-
version: '0'
|
69
|
-
- !ruby/object:Gem::Dependency
|
70
|
-
name: rdoc
|
71
|
-
requirement: !ruby/object:Gem::Requirement
|
72
|
-
requirements:
|
73
|
-
- - ">="
|
74
|
-
- !ruby/object:Gem::Version
|
75
|
-
version: '0'
|
76
|
-
type: :development
|
77
|
-
prerelease: false
|
78
|
-
version_requirements: !ruby/object:Gem::Requirement
|
79
|
-
requirements:
|
80
|
-
- - ">="
|
81
|
-
- !ruby/object:Gem::Version
|
82
|
-
version: '0'
|
83
27
|
description: A class based configuration library
|
84
|
-
email:
|
28
|
+
email: info@chef.io
|
85
29
|
executables: []
|
86
30
|
extensions: []
|
87
|
-
extra_rdoc_files:
|
88
|
-
- LICENSE
|
89
|
-
- README.md
|
31
|
+
extra_rdoc_files: []
|
90
32
|
files:
|
91
|
-
- Gemfile
|
92
33
|
- LICENSE
|
93
34
|
- NOTICE
|
94
|
-
- README.md
|
95
|
-
- Rakefile
|
96
35
|
- lib/mixlib/config.rb
|
97
36
|
- lib/mixlib/config/configurable.rb
|
98
37
|
- lib/mixlib/config/reopened_config_context_with_configurable_error.rb
|
99
38
|
- lib/mixlib/config/reopened_configurable_with_config_context_error.rb
|
100
39
|
- lib/mixlib/config/unknown_config_option_error.rb
|
101
40
|
- lib/mixlib/config/version.rb
|
102
|
-
|
103
|
-
- spec/mixlib/config_spec.rb
|
104
|
-
- spec/spec_helper.rb
|
105
|
-
homepage: https://www.chef.io
|
41
|
+
homepage: https://github.com/chef/mixlib-config
|
106
42
|
licenses:
|
107
43
|
- Apache-2.0
|
108
44
|
metadata: {}
|
data/Gemfile
DELETED
data/README.md
DELETED
@@ -1,316 +0,0 @@
|
|
1
|
-
# Mixlib::Config
|
2
|
-
|
3
|
-
[![Build Status](https://travis-ci.org/chef/mixlib-config.svg?branch=master)](https://travis-ci.org/chef/mixlib-config)[![Gem Version](https://badge.fury.io/rb/mixlib-config.svg)](https://badge.fury.io/rb/mixlib-config)
|
4
|
-
|
5
|
-
Mixlib::Config provides a class-based configuration object, as used in Chef. To use in your project:
|
6
|
-
|
7
|
-
```ruby
|
8
|
-
require 'mixlib/config'
|
9
|
-
|
10
|
-
module MyConfig
|
11
|
-
extend Mixlib::Config
|
12
|
-
config_strict_mode true
|
13
|
-
default :first_value, 'something'
|
14
|
-
default :other_value, 'something_else'
|
15
|
-
end
|
16
|
-
```
|
17
|
-
|
18
|
-
You can use this to provide a configuration file for a user. For example, if you do this:
|
19
|
-
|
20
|
-
```ruby
|
21
|
-
MyConfig.from_file('~/.myconfig.rb')
|
22
|
-
```
|
23
|
-
|
24
|
-
A user could write a Ruby config file that looked like this:
|
25
|
-
|
26
|
-
```ruby
|
27
|
-
first_value 'hi'
|
28
|
-
second_value "#{first_value}! 10 times 10 is #{10*10}!"
|
29
|
-
```
|
30
|
-
|
31
|
-
Inside your app, you can check configuration values with this syntax:
|
32
|
-
|
33
|
-
```ruby
|
34
|
-
MyConfig.first_value # returns 'something'
|
35
|
-
MyConfig[:first_value] # returns 'something'
|
36
|
-
```
|
37
|
-
|
38
|
-
And you can modify configuration values with this syntax:
|
39
|
-
|
40
|
-
```ruby
|
41
|
-
MyConfig.first_value('foobar') # sets first_value to 'foobar'
|
42
|
-
MyConfig.first_value = 'foobar' # sets first_value to 'foobar'
|
43
|
-
MyConfig[:first_value] = 'foobar' # sets first_value to 'foobar'
|
44
|
-
```
|
45
|
-
|
46
|
-
If you prefer to allow your users to pass in configuration via YAML, JSON or TOML files, `mixlib-config` supports that too!
|
47
|
-
|
48
|
-
```ruby
|
49
|
-
MyConfig.from_file('~/.myconfig.yml')
|
50
|
-
MyConfig.from_file('~/.myconfig.json')
|
51
|
-
MyConfig.from_file('~/.myconfig.toml')
|
52
|
-
```
|
53
|
-
|
54
|
-
This way, a user could write a YAML config file that looked like this:
|
55
|
-
|
56
|
-
```yaml
|
57
|
-
---
|
58
|
-
first_value: 'hi'
|
59
|
-
second_value: 'goodbye'
|
60
|
-
```
|
61
|
-
|
62
|
-
or a JSON file that looks like this:
|
63
|
-
|
64
|
-
```json
|
65
|
-
{
|
66
|
-
"first_value": "hi",
|
67
|
-
"second_value": "goodbye"
|
68
|
-
}
|
69
|
-
```
|
70
|
-
|
71
|
-
or a TOML file that looks like this:
|
72
|
-
|
73
|
-
```toml
|
74
|
-
first_value = "hi"
|
75
|
-
second_value = "goodbye"
|
76
|
-
```
|
77
|
-
|
78
|
-
Please note: There is an inherent limitation in the logic you can do with YAML and JSON file. At this time, `mixlib-config` does not support ERB or other logic in YAML or JSON config (read "static content only").
|
79
|
-
|
80
|
-
## Nested Configuration
|
81
|
-
|
82
|
-
Often you want to be able to group configuration options to provide a common context. Mixlib::Config supports this thus:
|
83
|
-
|
84
|
-
```ruby
|
85
|
-
require 'mixlib/config'
|
86
|
-
|
87
|
-
module MyConfig
|
88
|
-
extend Mixlib::Config
|
89
|
-
config_context :logging do
|
90
|
-
default :base_filename, 'mylog'
|
91
|
-
default :max_log_files, 10
|
92
|
-
end
|
93
|
-
end
|
94
|
-
```
|
95
|
-
|
96
|
-
The user can write their config file in one of three formats:
|
97
|
-
|
98
|
-
### Method Style
|
99
|
-
|
100
|
-
```ruby
|
101
|
-
logging.base_filename 'superlog'
|
102
|
-
logging.max_log_files 2
|
103
|
-
```
|
104
|
-
|
105
|
-
### Block Style
|
106
|
-
|
107
|
-
Using this format the block is executed in the context, so all configurables on that context is directly accessible
|
108
|
-
|
109
|
-
```ruby
|
110
|
-
logging do
|
111
|
-
base_filename 'superlog'
|
112
|
-
max_log_files 2
|
113
|
-
end
|
114
|
-
```
|
115
|
-
|
116
|
-
### Block with Argument Style
|
117
|
-
|
118
|
-
Using this format the context is given to the block as an argument
|
119
|
-
|
120
|
-
```ruby
|
121
|
-
logging do |l|
|
122
|
-
l.base_filename = 'superlog'
|
123
|
-
l.max_log_files = 2
|
124
|
-
end
|
125
|
-
```
|
126
|
-
|
127
|
-
You can access these variables thus:
|
128
|
-
|
129
|
-
```ruby
|
130
|
-
MyConfig.logging.base_filename
|
131
|
-
MyConfig[:logging][:max_log_files]
|
132
|
-
```
|
133
|
-
|
134
|
-
### Lists of Contexts
|
135
|
-
For use cases where you need to be able to specify a list of things with identical configuration
|
136
|
-
you can define a `context_config_list` like so:
|
137
|
-
|
138
|
-
```ruby
|
139
|
-
require 'mixlib/config'
|
140
|
-
|
141
|
-
module MyConfig
|
142
|
-
extend Mixlib::Config
|
143
|
-
|
144
|
-
# The first argument is the plural word for your item, the second is the singular
|
145
|
-
config_context_list :apples, :apple do
|
146
|
-
default :species
|
147
|
-
default :color, 'red'
|
148
|
-
default :crispness, 10
|
149
|
-
end
|
150
|
-
end
|
151
|
-
```
|
152
|
-
|
153
|
-
With this definition everytime the `apple` is called within the config file it
|
154
|
-
will create a new item that can be configured with a block like so:
|
155
|
-
|
156
|
-
```ruby
|
157
|
-
apple do
|
158
|
-
species 'Royal Gala'
|
159
|
-
end
|
160
|
-
apple do
|
161
|
-
species 'Granny Smith'
|
162
|
-
color 'green'
|
163
|
-
end
|
164
|
-
```
|
165
|
-
|
166
|
-
You can then iterate over the defined values in code:
|
167
|
-
|
168
|
-
```ruby
|
169
|
-
MyConfig.apples.each do |apple|
|
170
|
-
puts "#{apple.species} are #{apple.color}"
|
171
|
-
end
|
172
|
-
|
173
|
-
# => Royal Gala are red
|
174
|
-
# => Granny Smith are green
|
175
|
-
```
|
176
|
-
|
177
|
-
_**Note**: When using the config context lists they must use the [block style](#block-style) or [block with argument style](#block-with-argument-style)_
|
178
|
-
|
179
|
-
### Hashes of Contexts
|
180
|
-
For use cases where you need to be able to specify a list of things with identical configuration
|
181
|
-
that are keyed to a specific value, you can define a `context_config_hash` like so:
|
182
|
-
|
183
|
-
```ruby
|
184
|
-
require 'mixlib/config'
|
185
|
-
|
186
|
-
module MyConfig
|
187
|
-
extend Mixlib::Config
|
188
|
-
|
189
|
-
# The first argument is the plural word for your item, the second is the singular
|
190
|
-
config_context_hash :apples, :apple do
|
191
|
-
default :species
|
192
|
-
default :color, 'red'
|
193
|
-
default :crispness, 10
|
194
|
-
end
|
195
|
-
end
|
196
|
-
```
|
197
|
-
|
198
|
-
This can then be used in the config file like so:
|
199
|
-
|
200
|
-
```ruby
|
201
|
-
apple 'Royal Gala' do
|
202
|
-
species 'Royal Gala'
|
203
|
-
end
|
204
|
-
apple 'Granny Smith' do
|
205
|
-
species 'Granny Smith'
|
206
|
-
color 'green'
|
207
|
-
end
|
208
|
-
|
209
|
-
# You can also reopen a context to edit a value
|
210
|
-
apple 'Royal Gala' do
|
211
|
-
crispness 3
|
212
|
-
end
|
213
|
-
```
|
214
|
-
|
215
|
-
You can then iterate over the defined values in code:
|
216
|
-
|
217
|
-
```ruby
|
218
|
-
MyConfig.apples.each do |key, apple|
|
219
|
-
puts "#{key} => #{apple.species} are #{apple.color}"
|
220
|
-
end
|
221
|
-
|
222
|
-
# => Royal Gala => Royal Gala are red
|
223
|
-
# => Granny Smith => Granny Smith are green
|
224
|
-
```
|
225
|
-
|
226
|
-
_**Note**: When using the config context hashes they must use the [block style](#block-style) or [block with argument style](#block-with-argument-style)_
|
227
|
-
|
228
|
-
## Default Values
|
229
|
-
|
230
|
-
Mixlib::Config has a powerful default value facility. In addition to being able to specify explicit default values, you can even specify Ruby code blocks that will run if the config value is not set. This can allow you to build options whose values are based on other options.
|
231
|
-
|
232
|
-
```ruby
|
233
|
-
require 'mixlib/config'
|
234
|
-
|
235
|
-
module MyConfig
|
236
|
-
extend Mixlib::Config
|
237
|
-
config_strict_mode true
|
238
|
-
default :verbosity, 1
|
239
|
-
default(:print_network_requests) { verbosity >= 2 }
|
240
|
-
default(:print_ridiculously_unimportant_stuff) { verbosity >= 10 }
|
241
|
-
end
|
242
|
-
```
|
243
|
-
|
244
|
-
This allows the user to quickly specify a number of values with one default, while still allowing them to override anything:
|
245
|
-
|
246
|
-
```ruby
|
247
|
-
verbosity 5
|
248
|
-
print_network_requests false
|
249
|
-
```
|
250
|
-
|
251
|
-
You can also inspect if the values are still their defaults or not:
|
252
|
-
|
253
|
-
```ruby
|
254
|
-
MyConfig.is_default?(:verbosity) # == true
|
255
|
-
MyConfig[:verbosity] = 5
|
256
|
-
MyConfig.is_default?(:verbosity) # == false
|
257
|
-
MyConfig[:verbosity] = 1
|
258
|
-
MyConfig.is_default?(:verbosity) # == true
|
259
|
-
```
|
260
|
-
|
261
|
-
Trying to call `is_default?` on a config context or a config which does not have a declared default is an error and will raise.
|
262
|
-
|
263
|
-
## Strict Mode
|
264
|
-
|
265
|
-
Misspellings are a common configuration problem, and Mixlib::Config has an answer: `config_strict_mode`. Setting `config_strict_mode` to `true` will cause any misspelled or incorrect configuration option references to throw `Mixlib::Config::UnknownConfigOptionError`.
|
266
|
-
|
267
|
-
```ruby
|
268
|
-
require 'mixlib/config'
|
269
|
-
|
270
|
-
module MyConfig
|
271
|
-
extend Mixlib::Config
|
272
|
-
config_strict_mode true
|
273
|
-
default :filename, '~/output.txt'
|
274
|
-
configurable :server_url # configurable declares an option with no default value
|
275
|
-
config_context :logging do
|
276
|
-
default :base_name, 'log'
|
277
|
-
default :max_files, 20
|
278
|
-
end
|
279
|
-
end
|
280
|
-
```
|
281
|
-
|
282
|
-
Now if a user types `fielname "~/output-mine.txt"` in their configuration file, it will toss an exception telling them that the option "fielname" is unknown. If you do not set config_strict_mode, the fielname option will be merrily set and the application just won't know about it.
|
283
|
-
|
284
|
-
Different config_contexts can have different strict modes; but they inherit the strict mode of their parent if you don't explicitly set it. So setting it once at the top level is sufficient. In the above example, `logging.base_naem 'mylog'` will raise an error.
|
285
|
-
|
286
|
-
In conclusion: _always set config_strict_mode to true_. You know you want to.
|
287
|
-
|
288
|
-
## Testing and Reset
|
289
|
-
|
290
|
-
Testing your application with different sets of arguments can by simplified with `reset`. Call `MyConfig.reset` before each test and all configuration will be reset to its default value. There's no need to explicitly unset all your options between each run.
|
291
|
-
|
292
|
-
NOTE: if you have arrays of arrays, or other deep nesting, we suggest you use code blocks to set up your default values (`default(:option) { [ [ 1, 2 ], [ 3, 4 ] ] }`). Deep children will not always be reset to their default values.
|
293
|
-
|
294
|
-
Enjoy!
|
295
|
-
|
296
|
-
## Contributing
|
297
|
-
|
298
|
-
For information on contributing to this project see <https://github.com/chef/chef/blob/master/CONTRIBUTING.md>
|
299
|
-
|
300
|
-
## License
|
301
|
-
|
302
|
-
- Copyright:: Copyright (c) 2009-2016 Chef Software, Inc.
|
303
|
-
- License:: Apache License, Version 2.0
|
304
|
-
|
305
|
-
```text
|
306
|
-
Licensed under the Apache License, Version 2.0 (the "License");
|
307
|
-
you may not use this file except in compliance with the License.
|
308
|
-
You may obtain a copy of the License at
|
309
|
-
|
310
|
-
http://www.apache.org/licenses/LICENSE-2.0
|
311
|
-
|
312
|
-
Unless required by applicable law or agreed to in writing, software
|
313
|
-
distributed under the License is distributed on an "AS IS" BASIS,
|
314
|
-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
315
|
-
See the License for the specific language governing permissions and
|
316
|
-
limitations under the License.
|