mixlib-config 2.2.13 → 3.0.9
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 +43 -38
- data/lib/mixlib/config/version.rb +1 -1
- metadata +7 -72
- 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: 173fecb906c8de32acb4d86da76ffdbbc6f56344a1f4241f70703edc617aa70b
|
4
|
+
data.tar.gz: 2a139c38d1c2c611e7631004de53e5949c27287125b042dbde293290f0cb3106
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6672d34b127fdee131675e9afe208773c8dcc2e4aa57519af186d18a906d594d7a71b4cef25bb26fbc801c67bf0e964b4feeff5b3746460499cf000b20679ca3
|
7
|
+
data.tar.gz: a4f6e15bc340476b2773dcd8c9f7b3825e5a2b7860b2e064f427a9289993d5195594ad4e813d0ceccf888123de32bf80c534669526ff23e8858adce0c488271e
|
data/lib/mixlib/config.rb
CHANGED
@@ -18,11 +18,11 @@
|
|
18
18
|
# limitations under the License.
|
19
19
|
#
|
20
20
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
21
|
+
require_relative "config/version"
|
22
|
+
require_relative "config/configurable"
|
23
|
+
require_relative "config/unknown_config_option_error"
|
24
|
+
require_relative "config/reopened_config_context_with_configurable_error"
|
25
|
+
require_relative "config/reopened_configurable_with_config_context_error"
|
26
26
|
|
27
27
|
module Mixlib
|
28
28
|
module Config
|
@@ -33,11 +33,11 @@ module Mixlib
|
|
33
33
|
class << base; attr_accessor :config_context_lists; end
|
34
34
|
class << base; attr_accessor :config_context_hashes; end
|
35
35
|
class << base; attr_accessor :config_parent; end
|
36
|
-
base.configuration =
|
37
|
-
base.configurables =
|
38
|
-
base.config_contexts =
|
39
|
-
base.config_context_lists =
|
40
|
-
base.config_context_hashes =
|
36
|
+
base.configuration = ({})
|
37
|
+
base.configurables = ({})
|
38
|
+
base.config_contexts = ({})
|
39
|
+
base.config_context_lists = ({})
|
40
|
+
base.config_context_hashes = ({})
|
41
41
|
base.initialize_mixlib_config
|
42
42
|
end
|
43
43
|
|
@@ -69,7 +69,7 @@ module Mixlib
|
|
69
69
|
# === Parameters
|
70
70
|
# filename<String>:: A filename to read from
|
71
71
|
def from_yaml(filename)
|
72
|
-
require "yaml"
|
72
|
+
require "yaml" unless defined?(YAML)
|
73
73
|
from_hash(YAML.load(IO.read(filename)))
|
74
74
|
end
|
75
75
|
|
@@ -78,12 +78,12 @@ module Mixlib
|
|
78
78
|
# === Parameters
|
79
79
|
# filename<String>:: A filename to read from
|
80
80
|
def from_json(filename)
|
81
|
-
require "json"
|
81
|
+
require "json" unless defined?(JSON)
|
82
82
|
from_hash(JSON.parse(IO.read(filename)))
|
83
83
|
end
|
84
84
|
|
85
85
|
def from_toml(filename)
|
86
|
-
require "tomlrb"
|
86
|
+
require "tomlrb" unless defined?(Tomlrb)
|
87
87
|
from_hash(Tomlrb.parse(IO.read(filename), symbolize_keys: true))
|
88
88
|
end
|
89
89
|
|
@@ -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"
|
@@ -165,8 +165,8 @@ module Mixlib
|
|
165
165
|
|
166
166
|
# Resets all config options to their defaults.
|
167
167
|
def reset
|
168
|
-
self.configuration =
|
169
|
-
config_contexts.values.each
|
168
|
+
self.configuration = ({})
|
169
|
+
config_contexts.values.each(&:reset)
|
170
170
|
end
|
171
171
|
|
172
172
|
# Makes a copy of any non-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,9 +369,10 @@ 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
376
|
configurables[symbol] = Configurable.new(symbol)
|
376
377
|
define_attr_accessor_methods(symbol)
|
377
378
|
end
|
@@ -397,11 +398,11 @@ module Mixlib
|
|
397
398
|
# block<Block>: a block that will be run in the context of this new config
|
398
399
|
# class.
|
399
400
|
def config_context(symbol, &block)
|
400
|
-
if configurables.
|
401
|
+
if configurables.key?(symbol)
|
401
402
|
raise ReopenedConfigurableWithConfigContextError, "Cannot redefine config value #{symbol} with a config context"
|
402
403
|
end
|
403
404
|
|
404
|
-
if config_contexts.
|
405
|
+
if config_contexts.key?(symbol)
|
405
406
|
context = config_contexts[symbol]
|
406
407
|
else
|
407
408
|
context = Class.new
|
@@ -435,11 +436,11 @@ module Mixlib
|
|
435
436
|
# block<Block>: a block that will be run in the context of this new config
|
436
437
|
# class.
|
437
438
|
def config_context_list(plural_symbol, singular_symbol, &block)
|
438
|
-
if configurables.
|
439
|
+
if configurables.key?(plural_symbol)
|
439
440
|
raise ReopenedConfigurableWithConfigContextError, "Cannot redefine config value #{plural_symbol} with a config context"
|
440
441
|
end
|
441
442
|
|
442
|
-
unless config_context_lists.
|
443
|
+
unless config_context_lists.key?(plural_symbol)
|
443
444
|
config_context_lists[plural_symbol] = {
|
444
445
|
definition_blocks: [],
|
445
446
|
values: [],
|
@@ -467,11 +468,11 @@ module Mixlib
|
|
467
468
|
# block<Block>: a block that will be run in the context of this new config
|
468
469
|
# class.
|
469
470
|
def config_context_hash(plural_symbol, singular_symbol, &block)
|
470
|
-
if configurables.
|
471
|
+
if configurables.key?(plural_symbol)
|
471
472
|
raise ReopenedConfigurableWithConfigContextError, "Cannot redefine config value #{plural_symbol} with a config context"
|
472
473
|
end
|
473
474
|
|
474
|
-
unless config_context_hashes.
|
475
|
+
unless config_context_hashes.key?(plural_symbol)
|
475
476
|
config_context_hashes[plural_symbol] = {
|
476
477
|
definition_blocks: [],
|
477
478
|
values: {},
|
@@ -531,9 +532,10 @@ module Mixlib
|
|
531
532
|
# <ArgumentError>:: if value is set to something other than true, false, or :warn
|
532
533
|
#
|
533
534
|
def config_strict_mode=(value)
|
534
|
-
|
535
|
+
unless [ true, false, :warn, nil ].include?(value)
|
535
536
|
raise ArgumentError, "config_strict_mode must be true, false, nil or :warn"
|
536
537
|
end
|
538
|
+
|
537
539
|
@config_strict_mode = value
|
538
540
|
end
|
539
541
|
|
@@ -567,7 +569,10 @@ module Mixlib
|
|
567
569
|
def apply_nested_hash(hash)
|
568
570
|
hash.each do |k, v|
|
569
571
|
if v.is_a? Hash
|
570
|
-
|
572
|
+
# If loading from hash, and we reference a context that doesn't exist
|
573
|
+
# and warning/strict is off, we need to create the config context that we expected to be here.
|
574
|
+
context = internal_get(k.to_sym) || config_context(k.to_sym)
|
575
|
+
context.apply_nested_hash(v)
|
571
576
|
else
|
572
577
|
internal_set(k.to_sym, v)
|
573
578
|
end
|
@@ -604,9 +609,9 @@ module Mixlib
|
|
604
609
|
# value<Object>:: Value to be set in config hash
|
605
610
|
#
|
606
611
|
def internal_set(symbol, value)
|
607
|
-
if configurables.
|
612
|
+
if configurables.key?(symbol)
|
608
613
|
configurables[symbol].set(configuration, value)
|
609
|
-
elsif config_contexts.
|
614
|
+
elsif config_contexts.key?(symbol)
|
610
615
|
config_contexts[symbol].restore(value.to_hash)
|
611
616
|
else
|
612
617
|
if config_strict_mode == :warn
|
@@ -619,13 +624,13 @@ module Mixlib
|
|
619
624
|
end
|
620
625
|
|
621
626
|
def internal_get(symbol)
|
622
|
-
if configurables.
|
627
|
+
if configurables.key?(symbol)
|
623
628
|
configurables[symbol].get(configuration)
|
624
|
-
elsif config_contexts.
|
629
|
+
elsif config_contexts.key?(symbol)
|
625
630
|
config_contexts[symbol]
|
626
|
-
elsif config_context_lists.
|
631
|
+
elsif config_context_lists.key?(symbol)
|
627
632
|
config_context_lists[symbol]
|
628
|
-
elsif config_context_hashes.
|
633
|
+
elsif config_context_hashes.key?(symbol)
|
629
634
|
config_context_hashes[symbol]
|
630
635
|
else
|
631
636
|
if config_strict_mode == :warn
|
@@ -702,7 +707,7 @@ module Mixlib
|
|
702
707
|
# Adds a single new context to the list
|
703
708
|
meta.send :define_method, singular_symbol do |key, &block|
|
704
709
|
context_hash_details = internal_get(plural_symbol)
|
705
|
-
context = if context_hash_details[:values].
|
710
|
+
context = if context_hash_details[:values].key? key
|
706
711
|
context_hash_details[:values][key]
|
707
712
|
else
|
708
713
|
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:
|
4
|
+
version: 3.0.9
|
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:
|
11
|
+
date: 2020-08-13 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: {}
|
@@ -114,15 +50,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
114
50
|
requirements:
|
115
51
|
- - ">="
|
116
52
|
- !ruby/object:Gem::Version
|
117
|
-
version: '2.
|
53
|
+
version: '2.4'
|
118
54
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
119
55
|
requirements:
|
120
56
|
- - ">="
|
121
57
|
- !ruby/object:Gem::Version
|
122
58
|
version: '0'
|
123
59
|
requirements: []
|
124
|
-
|
125
|
-
rubygems_version: 2.7.6
|
60
|
+
rubygems_version: 3.0.3
|
126
61
|
signing_key:
|
127
62
|
specification_version: 4
|
128
63
|
summary: A class based configuration library
|
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.
|