stacked_config 0.3.1 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +40 -1
- data/lib/stacked_config/layers/env_layer.rb +41 -0
- data/lib/stacked_config/orchestrator.rb +9 -1
- data/lib/stacked_config/source_helper.rb +1 -6
- data/lib/stacked_config/version.rb +1 -1
- data/lib/stacked_config.rb +1 -0
- data/spec/env_layer_spec.rb +55 -0
- data/spec/orchestrator_spec.rb +14 -0
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: da24388d057ab98233e3002bb7adcc961b2b5bb9
|
4
|
+
data.tar.gz: cb324ce96a7a17fdd7ffdbd89d9e94eaefc08a1f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3a4b2fcf9749d4a2002f6a2004ab14caaf522b04bd622f71112673654b934c37c1fd55ed1fd8974cef1413599508b4fb2e6b85984ab3f9ce3a136a9ac080a7f2
|
7
|
+
data.tar.gz: ce7df61eaeb80659d44b0b3e4b7aacaef1454fa9c1391b74242fb32ee5c27cc1460368ffc91be5ae132b9d0913aae05eb794ce2f5468849b822f42259d2c4a8e
|
data/README.md
CHANGED
@@ -15,6 +15,8 @@ script. By default, it will handle already few config layers:
|
|
15
15
|
* The __global layer__, which is the layer to declare options for all users that use the ruby script using this gem.
|
16
16
|
* The __user layer__, which is the layer, where a user can set options for the ruby script using this gem.
|
17
17
|
* The __extra layer__, which provides the possibility to specify another config file from the config itself.
|
18
|
+
* The __enviroment variables layer__, which provides the possibility to include in the config variables coming from
|
19
|
+
the shell variables. See [below](#environment-variables) for more info.
|
18
20
|
* The __command-line layer__, which provides the ability to specify options from the command line.
|
19
21
|
* The __override layer__, which will contain all modifications done to the config at run time.
|
20
22
|
|
@@ -81,6 +83,7 @@ Every layer is accessible through the following orchestrator properties:
|
|
81
83
|
* `global_layer`
|
82
84
|
* `user_layer`
|
83
85
|
* `provided_config_file_layer`
|
86
|
+
* `env_layer`
|
84
87
|
* `command_line_layer`
|
85
88
|
* `write_layer`
|
86
89
|
|
@@ -103,7 +106,8 @@ As you can see in the sources, paths are expressed using kind of 'templates', wh
|
|
103
106
|
currently executing script.
|
104
107
|
* `##PROGRAM_NAME##` is by default the name of the script you are running (with no extension). You can if you want
|
105
108
|
change this name at runtime. __Changing it (using the `config_file_base_name` orchestrator property ) will trigger a
|
106
|
-
re-search and reload of all the config files__.
|
109
|
+
re-search and reload of all the config files__. `config_file_base_name` defines the base name to search for config
|
110
|
+
files accross the system. by default the name of the gem will be used.
|
107
111
|
* `##EXTENSION##` is one of the following extensions : `conf CONF cfg CFG yml YML yaml YAML`.
|
108
112
|
|
109
113
|
The search of the config files for a layer is done according to the order defined in sources just above and then
|
@@ -118,6 +122,41 @@ user config level, only the first is taken in account:
|
|
118
122
|
* `~/.config/my_script.conf`
|
119
123
|
|
120
124
|
|
125
|
+
### Environment variables
|
126
|
+
|
127
|
+
`stacked_config` can read information from your ENV variables thanks to the dedicated `env_layer`. __This layer is not
|
128
|
+
created by default__, it is up to you to add it to your application, due to the fact that you will use it mostly
|
129
|
+
with an optional filter and that you may want to add it with different priorities regarding your use case:
|
130
|
+
|
131
|
+
```ruby
|
132
|
+
require 'stacked_config'
|
133
|
+
|
134
|
+
config = StackedConfig::Orchestrator.new
|
135
|
+
env_layer = StackedConfig::Layers::EnvLayer.new(optional_filter)
|
136
|
+
config.add_layer(envLayer)
|
137
|
+
env_layer.name = 'Environment variables level'
|
138
|
+
env_layer.priority = 60
|
139
|
+
```
|
140
|
+
|
141
|
+
Or the exactly same using the convenience method of the orchestrator:
|
142
|
+
|
143
|
+
```ruby
|
144
|
+
require 'stacked_config'
|
145
|
+
|
146
|
+
config = StackedConfig::Orchestrator.new
|
147
|
+
config.include_env_layer(optional_filter, optional_priority)
|
148
|
+
```
|
149
|
+
|
150
|
+
Which does not prevent you changing afterwards its priority using the config's `env_layer` accessor.
|
151
|
+
|
152
|
+
The constructor parameter `optional_filter` parameter aims at filtering the ENV variables:
|
153
|
+
|
154
|
+
* if nil, all the ENV[] content will be added to the `env_layer`
|
155
|
+
* it can be a single string if you want only one variable
|
156
|
+
* it can be an array of accepted names of variables. ex: `StackedConfig::Layers::EnvLayer.new 'VAR_NAME_1'`
|
157
|
+
`StackedConfig::Layers::EnvLayer.new ['VAR_NAME_1', 'VAR_NAME_2']`
|
158
|
+
* it can be a regexp that variables names have to match. ex: `StackedConfig::Layers::EnvLayer.new /somePattern/`
|
159
|
+
|
121
160
|
### Script command line options
|
122
161
|
|
123
162
|
`stacked_config` uses internally the fantastic [Slop] gem to manage options coming from the command line within the
|
@@ -0,0 +1,41 @@
|
|
1
|
+
module StackedConfig
|
2
|
+
module Layers
|
3
|
+
|
4
|
+
class EnvLayer < SuperStack::Layer
|
5
|
+
|
6
|
+
attr_reader :filter
|
7
|
+
|
8
|
+
def initialize(filter=nil)
|
9
|
+
self.filter = filter
|
10
|
+
end
|
11
|
+
|
12
|
+
def filter=(filter)
|
13
|
+
raise 'Invalid filter' unless [NilClass, String, Array, Regexp].include? filter.class
|
14
|
+
@filter = filter
|
15
|
+
reload
|
16
|
+
end
|
17
|
+
|
18
|
+
def load(*args)
|
19
|
+
self.replace read_filtered_environment
|
20
|
+
@file_name = :none
|
21
|
+
self
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def read_filtered_environment
|
27
|
+
return ENV.to_hash if filter.nil?
|
28
|
+
|
29
|
+
if filter.is_a? Array
|
30
|
+
ENV.to_hash.select {|k, v| filter.include?(k) }
|
31
|
+
elsif filter.is_a? Regexp
|
32
|
+
ENV.to_hash.select {|k, v| k =~ filter }
|
33
|
+
elsif filter.is_a? String
|
34
|
+
ENV.to_hash.select {|k, v| k == filter }
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
end
|
@@ -3,7 +3,7 @@ module StackedConfig
|
|
3
3
|
|
4
4
|
include StackedConfig::ProgramDescriptionHelper
|
5
5
|
|
6
|
-
attr_reader :system_layer, :global_layer, :gem_layer, :user_layer,
|
6
|
+
attr_reader :system_layer, :global_layer, :gem_layer, :user_layer, :env_layer,
|
7
7
|
:command_line_layer, :provided_config_file_layer
|
8
8
|
|
9
9
|
def initialize
|
@@ -19,6 +19,14 @@ module StackedConfig
|
|
19
19
|
File.basename($PROGRAM_NAME).gsub /\.[^\.]+$/, ''
|
20
20
|
end
|
21
21
|
|
22
|
+
def include_env_layer(filter = nil, priority = 60)
|
23
|
+
@env_layer = StackedConfig::Layers::EnvLayer.new filter
|
24
|
+
env_layer.name = 'Environment variables level'
|
25
|
+
env_layer.priority = priority
|
26
|
+
env_layer.load
|
27
|
+
self << env_layer
|
28
|
+
end
|
29
|
+
|
22
30
|
private
|
23
31
|
|
24
32
|
def setup_layers
|
@@ -12,11 +12,6 @@ module StackedConfig
|
|
12
12
|
unix: '/etc'
|
13
13
|
}
|
14
14
|
|
15
|
-
USER_CONFIG_ROOT = {
|
16
|
-
windows: ENV['APPDATA'],
|
17
|
-
unix: ENV['HOME']
|
18
|
-
}
|
19
|
-
|
20
15
|
EXTENSIONS = %w(conf CONF cfg CFG yml YML yaml YAML)
|
21
16
|
|
22
17
|
def self.os_flavour
|
@@ -32,7 +27,7 @@ module StackedConfig
|
|
32
27
|
end
|
33
28
|
|
34
29
|
def self.user_config_root
|
35
|
-
|
30
|
+
Dir.home
|
36
31
|
end
|
37
32
|
|
38
33
|
def self.gem_config_root
|
data/lib/stacked_config.rb
CHANGED
@@ -9,6 +9,7 @@ require 'stacked_config/layers/system_layer'
|
|
9
9
|
require 'stacked_config/layers/gem_layer'
|
10
10
|
require 'stacked_config/layers/global_layer'
|
11
11
|
require 'stacked_config/layers/user_layer'
|
12
|
+
require 'stacked_config/layers/env_layer'
|
12
13
|
require 'stacked_config/layers/command_line_layer'
|
13
14
|
require 'stacked_config/layers/provided_config_file_layer'
|
14
15
|
require 'stacked_config/orchestrator'
|
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe StackedConfig::Layers::EnvLayer do
|
4
|
+
|
5
|
+
NIL_FILTER = nil
|
6
|
+
STRING_FILTER ='StackedConfig::Layers::EnvLayer::VAR1'
|
7
|
+
ARRAY_FILTER = %w(StackedConfig::Layers::EnvLayer::VAR1 StackedConfig::Layers::EnvLayer::VAR3)
|
8
|
+
REGEXP_FILTER = /StackedConfig::Layers::EnvLayer::VAR[13]/
|
9
|
+
|
10
|
+
before(:all) do
|
11
|
+
(1..3).each { |i| ENV["StackedConfig::Layers::EnvLayer::VAR#{i}"] = "VALUE#{i}" }
|
12
|
+
end
|
13
|
+
|
14
|
+
after(:all) do
|
15
|
+
(1..3).each { |i| ENV.delete("StackedConfig::Layers::EnvLayer::VAR#{i}") }
|
16
|
+
end
|
17
|
+
|
18
|
+
context 'when no filter is provided' do
|
19
|
+
|
20
|
+
it 'should contain all ENV values' do
|
21
|
+
layer = StackedConfig::Layers::EnvLayer.new
|
22
|
+
layer.load
|
23
|
+
expect(layer.to_a).to eq(ENV.to_hash.to_a)
|
24
|
+
layer = StackedConfig::Layers::EnvLayer.new NIL_FILTER
|
25
|
+
layer.load
|
26
|
+
expect(layer.to_a).to eq(ENV.to_hash.to_a)
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
|
31
|
+
context 'when a filter is provided' do
|
32
|
+
|
33
|
+
[ARRAY_FILTER, REGEXP_FILTER].each do |filter|
|
34
|
+
|
35
|
+
it "should contain filtered ENV variables according to the #{filter.class} filter" do
|
36
|
+
layer = StackedConfig::Layers::EnvLayer.new(filter)
|
37
|
+
layer.load
|
38
|
+
[1,3].each { |i|
|
39
|
+
expect(layer["StackedConfig::Layers::EnvLayer::VAR#{i}"]).to eql("VALUE#{i}")
|
40
|
+
}
|
41
|
+
expect(layer.keys.length == 2).to be_truthy
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'should filter according a String if only value is requested' do
|
47
|
+
layer = StackedConfig::Layers::EnvLayer.new(STRING_FILTER)
|
48
|
+
layer.load
|
49
|
+
expect(layer['StackedConfig::Layers::EnvLayer::VAR1']).to eql 'VALUE1'
|
50
|
+
expect(layer.keys.length == 1).to be_truthy
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
data/spec/orchestrator_spec.rb
CHANGED
@@ -116,5 +116,19 @@ describe StackedConfig::Orchestrator do
|
|
116
116
|
|
117
117
|
end
|
118
118
|
|
119
|
+
it 'should not have environment variables by defaut' do
|
120
|
+
expect(subject.env_layer).to be_nil
|
121
|
+
end
|
122
|
+
|
123
|
+
context 'when including environment variables in the config' do
|
124
|
+
|
125
|
+
it 'should be accessible through the #env_layer accessor' do
|
126
|
+
expect {subject.include_env_layer}.not_to raise_error
|
127
|
+
expect(subject.env_layer).not_to be_nil
|
128
|
+
expect(subject.env_layer).to be_a StackedConfig::Layers::EnvLayer
|
129
|
+
end
|
130
|
+
|
131
|
+
end
|
132
|
+
|
119
133
|
|
120
134
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: stacked_config
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Laurent B.
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-01-
|
11
|
+
date: 2015-01-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -116,6 +116,7 @@ files:
|
|
116
116
|
- Rakefile
|
117
117
|
- lib/stacked_config.rb
|
118
118
|
- lib/stacked_config/layers/command_line_layer.rb
|
119
|
+
- lib/stacked_config/layers/env_layer.rb
|
119
120
|
- lib/stacked_config/layers/gem_layer.rb
|
120
121
|
- lib/stacked_config/layers/generic_layer.rb
|
121
122
|
- lib/stacked_config/layers/global_layer.rb
|
@@ -127,6 +128,7 @@ files:
|
|
127
128
|
- lib/stacked_config/source_helper.rb
|
128
129
|
- lib/stacked_config/version.rb
|
129
130
|
- spec/command_line_layer_spec.rb
|
131
|
+
- spec/env_layer_spec.rb
|
130
132
|
- spec/gem_layer_spec.rb
|
131
133
|
- spec/global_layer_spec.rb
|
132
134
|
- spec/orchestrator_spec.rb
|
@@ -178,6 +180,7 @@ specification_version: 4
|
|
178
180
|
summary: Manages config files according to standard policy.
|
179
181
|
test_files:
|
180
182
|
- spec/command_line_layer_spec.rb
|
183
|
+
- spec/env_layer_spec.rb
|
181
184
|
- spec/gem_layer_spec.rb
|
182
185
|
- spec/global_layer_spec.rb
|
183
186
|
- spec/orchestrator_spec.rb
|