consul_application_settings 0.1.3 → 2.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/main.yml +7 -5
- data/.simplecov +6 -1
- data/Gemfile.lock +15 -8
- data/README.md +22 -27
- data/consul_application_settings.gemspec +3 -2
- data/lib/consul_application_settings.rb +9 -17
- data/lib/consul_application_settings/configuration.rb +6 -6
- data/lib/consul_application_settings/consul_provider.rb +33 -0
- data/lib/consul_application_settings/file_provider.rb +54 -0
- data/lib/consul_application_settings/settings_provider.rb +16 -0
- data/lib/consul_application_settings/version.rb +1 -1
- metadata +23 -9
- data/.codeclimate.yml +0 -15
- data/lib/consul_application_settings/defaults.rb +0 -43
- data/lib/consul_application_settings/options.rb +0 -53
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8e6c1820af05af3bcc9adca21879296cbcfc8898e69ffa7136bac1d785ae1bb1
|
4
|
+
data.tar.gz: 0e8b5c29bf2992a6d36e7a064b8887922b69ec5f820073157b53a21dc5905089
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 976fec73196a99d0a642d9f4d9243beb0a0925f44f140ff6e77091dac1919f36472b1467344e190da01b071803412d6ec612ccb31f8751a9d06d3f471a49fd1f
|
7
|
+
data.tar.gz: e0d2e02025e94d2d5fc6ac4dd24a4073f9ac228969e67afbd9afb5cafa297c6a347dd68a53fac9a4ad6b4c2ce875b036372894b175e1bd36e6db138c1b31e9d9
|
data/.github/workflows/main.yml
CHANGED
@@ -39,13 +39,15 @@ jobs:
|
|
39
39
|
- name: Set up Dependencies
|
40
40
|
run: bundle install --path vendor/bundle
|
41
41
|
|
42
|
-
- name: Run
|
43
|
-
uses: paambaati/codeclimate-action@v2.3.0
|
42
|
+
- name: Run specs
|
44
43
|
env:
|
45
44
|
COVERAGE: true
|
46
|
-
|
47
|
-
|
48
|
-
|
45
|
+
run: bundle exec rspec
|
46
|
+
|
47
|
+
- name: Upload coverage
|
48
|
+
env:
|
49
|
+
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
|
50
|
+
run: bash <(curl -s https://codecov.io/bash)
|
49
51
|
|
50
52
|
release:
|
51
53
|
runs-on: ubuntu-latest
|
data/.simplecov
CHANGED
data/Gemfile.lock
CHANGED
@@ -2,29 +2,34 @@ PATH
|
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
4
|
consul_application_settings (0.0.0)
|
5
|
-
diplomat (~> 2.1
|
5
|
+
diplomat (~> 2.5.1)
|
6
6
|
|
7
7
|
GEM
|
8
8
|
remote: https://rubygems.org/
|
9
9
|
specs:
|
10
10
|
ast (2.4.0)
|
11
|
+
codecov (0.4.3)
|
12
|
+
simplecov (>= 0.15, < 0.22)
|
11
13
|
deep_merge (1.2.1)
|
12
14
|
diff-lcs (1.3)
|
13
|
-
diplomat (2.1
|
14
|
-
deep_merge (~> 1.
|
15
|
-
faraday (
|
15
|
+
diplomat (2.5.1)
|
16
|
+
deep_merge (~> 1.2)
|
17
|
+
faraday (>= 0.9)
|
16
18
|
docile (1.3.1)
|
17
|
-
faraday (
|
19
|
+
faraday (1.3.0)
|
20
|
+
faraday-net_http (~> 1.0)
|
18
21
|
multipart-post (>= 1.2, < 3)
|
22
|
+
ruby2_keywords
|
23
|
+
faraday-net_http (1.0.1)
|
19
24
|
jaro_winkler (1.5.2)
|
20
|
-
json (2.
|
25
|
+
json (2.3.1)
|
21
26
|
multipart-post (2.1.1)
|
22
27
|
parallel (1.16.2)
|
23
28
|
parser (2.6.2.0)
|
24
29
|
ast (~> 2.4.0)
|
25
30
|
psych (3.1.0)
|
26
31
|
rainbow (3.0.0)
|
27
|
-
rake (
|
32
|
+
rake (13.0.1)
|
28
33
|
rspec (3.8.0)
|
29
34
|
rspec-core (~> 3.8.0)
|
30
35
|
rspec-expectations (~> 3.8.0)
|
@@ -49,6 +54,7 @@ GEM
|
|
49
54
|
rubocop-rspec (1.32.0)
|
50
55
|
rubocop (>= 0.60.0)
|
51
56
|
ruby-progressbar (1.10.0)
|
57
|
+
ruby2_keywords (0.0.4)
|
52
58
|
simplecov (0.16.1)
|
53
59
|
docile (~> 1.1)
|
54
60
|
json (>= 1.8, < 3)
|
@@ -61,8 +67,9 @@ PLATFORMS
|
|
61
67
|
|
62
68
|
DEPENDENCIES
|
63
69
|
bundler (~> 2.0)
|
70
|
+
codecov (~> 0.4)
|
64
71
|
consul_application_settings!
|
65
|
-
rake (~>
|
72
|
+
rake (~> 13.0)
|
66
73
|
rspec (~> 3.0)
|
67
74
|
rubocop (~> 0.66)
|
68
75
|
rubocop-rspec (~> 1.32.0)
|
data/README.md
CHANGED
@@ -1,8 +1,7 @@
|
|
1
1
|
# ConsulApplicationSettings
|
2
2
|
|
3
3
|
![Build Status](https://github.com/matic-insurance/consul_application_settings/workflows/ci/badge.svg?branch=master)
|
4
|
-
[![Test Coverage](https://
|
5
|
-
[![Maintainability](https://api.codeclimate.com/v1/badges/b0eaebcf83898535ea4e/maintainability)](https://codeclimate.com/github/matic-insurance/consul_application_settings/maintainability)
|
4
|
+
[![Test Coverage](https://codecov.io/gh/matic-insurance/consul_application_settings/branch/master/graph/badge.svg?token=5E8NA8EE8L)](https://codecov.io/gh/matic-insurance/consul_application_settings)
|
6
5
|
|
7
6
|
Gem that simplifies usage of Consul (via [Diplomat gem](https://github.com/WeAreFarmGeek/diplomat))
|
8
7
|
to host application settings. Gem provides defaults via yaml files and other utilities
|
@@ -23,7 +22,7 @@ Example use cases:
|
|
23
22
|
|
24
23
|
Gem reads any particular setting from consul and if it is missing tries to find value in YAML defaults file
|
25
24
|
|
26
|
-
**NOTE** Consul is requested every time you query the settings. Defaults YAML file loaded in memory and not changing.
|
25
|
+
**NOTE** Consul is requested every time you query the settings. Defaults YAML file is loaded in memory and is not changing.
|
27
26
|
|
28
27
|
## Installation
|
29
28
|
|
@@ -40,13 +39,17 @@ gem 'consul_application_settings'
|
|
40
39
|
At the load of application:
|
41
40
|
```ruby
|
42
41
|
ConsulApplicationSettings.configure do |config|
|
43
|
-
# Specify path to
|
44
|
-
config.
|
45
|
-
# Specify
|
46
|
-
config.
|
42
|
+
# Specify path to the base settings YML. Default: 'config/application_settings.yml'
|
43
|
+
config.base_file_path = Rails.root.join('config/my_settings.yml')
|
44
|
+
# Specify path to the local settings YML, which overrides the base file. Default: 'config/application_settings.local.yml'
|
45
|
+
config.local_file_path = Rails.root.join('config/my_settings.local.yml')
|
46
|
+
# Specify whether exceprion should be thrown on Consul connection errors. Default: false
|
47
|
+
config.disable_consul_connection_errors = true
|
47
48
|
end
|
48
49
|
|
49
50
|
APP_SETTINGS = ConsulApplicationSettings.load
|
51
|
+
# Specify path to settings both in YML files and Consul
|
52
|
+
AUTH_SETTIGNS = ConsulApplicationSettings.load('authentication')
|
50
53
|
```
|
51
54
|
|
52
55
|
**NOTE** For rails you can add this code to custom initializer `console_application_settings.rb` in `app/config/initializers`
|
@@ -55,7 +58,7 @@ APP_SETTINGS = ConsulApplicationSettings.load
|
|
55
58
|
|
56
59
|
### Settings structure
|
57
60
|
|
58
|
-
Assuming your defaults file in repository `config/
|
61
|
+
Assuming your defaults file in repository `config/application_settings.yml` looks like:
|
59
62
|
```yaml
|
60
63
|
staging:
|
61
64
|
my_cool_app:
|
@@ -98,15 +101,13 @@ Anywhere in your code base, after initialization, you can use
|
|
98
101
|
previously loaded settings to query any key by full path
|
99
102
|
|
100
103
|
```ruby
|
101
|
-
APP_SETTINGS
|
104
|
+
APP_SETTINGS['app_name'] # "MyCoolApp"
|
102
105
|
APP_SETTINGS.get(:hostname) # "https://mycoolapp.com"
|
103
106
|
|
104
107
|
APP_SETTINGS.get('integrations/database/user') # "app"
|
105
108
|
APP_SETTINGS['integrations/slack/enabled'] # true
|
106
109
|
```
|
107
110
|
|
108
|
-
**NOTE** Gem is pulling settings from consul with namespace but ignores namespace for defaults
|
109
|
-
|
110
111
|
### Nested settings
|
111
112
|
|
112
113
|
Assuming some part of your code needs to work with smaller part of settings -
|
@@ -114,36 +115,30 @@ gem provides interface to avoid duplicating absolute path
|
|
114
115
|
|
115
116
|
```ruby
|
116
117
|
# You can load subsettings from root object
|
117
|
-
db_settings = APP_SETTINGS.
|
118
|
-
db_settings.domain # "194.78.92.19"
|
119
|
-
db_settings['user']
|
120
|
-
|
121
|
-
# You can load subsettings from subsettings
|
122
|
-
integrations_settings = APP_SETTINGS.load_from('integrations')
|
123
|
-
slack_settings = integrations_settings.load_from('slack')
|
124
|
-
slack_settings.enabled # true
|
125
|
-
slack_settings.get('webhook_url') # "https://hooks.slack.com/services/XXXXXX/XXXXX/XXXXXXX"
|
118
|
+
db_settings = APP_SETTINGS.load('integrations/database')
|
119
|
+
db_settings.get(:domain) # "194.78.92.19"
|
120
|
+
db_settings['user'] # "app"
|
126
121
|
```
|
127
122
|
|
128
123
|
### Gem Configuration
|
129
124
|
You can configure gem with block:
|
130
125
|
```ruby
|
131
126
|
ConsulApplicationSettings.configure do |config|
|
132
|
-
config.
|
127
|
+
config.local_file_path = 'config/config.yml'
|
133
128
|
end
|
134
129
|
```
|
135
130
|
or one option at a time
|
136
131
|
```ruby
|
137
|
-
ConsulApplicationSettings.config.
|
132
|
+
ConsulApplicationSettings.config.local_file_path = 'config/config.yml'
|
138
133
|
```
|
139
134
|
|
140
135
|
All Gem configurations
|
141
136
|
|
142
|
-
| Configuration | Required | Default | Type | Description
|
143
|
-
|
144
|
-
|
|
145
|
-
|
|
146
|
-
| disable_consul_connection_errors | no |
|
137
|
+
| Configuration | Required | Default | Type | Description |
|
138
|
+
|----------------------------------|----------|-----------------------------------------|---------|------------------------------------------------------------------------------|
|
139
|
+
| base_file_path | no | 'config/application_settings.yml' | String | Path to the file with base settings |
|
140
|
+
| local_file_path | no | 'config/application_settings.local.yml' | String | Path to the file with local settings overriding the base settings |
|
141
|
+
| disable_consul_connection_errors | no | true | Boolean | Do not raise exception when consul is not available (useful for development) |
|
147
142
|
|
148
143
|
## Development
|
149
144
|
|
@@ -29,12 +29,13 @@ Gem::Specification.new do |spec|
|
|
29
29
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
30
30
|
spec.require_paths = ['lib']
|
31
31
|
|
32
|
-
spec.add_dependency 'diplomat', '~> 2.1
|
32
|
+
spec.add_dependency 'diplomat', '~> 2.5.1'
|
33
33
|
|
34
34
|
spec.add_development_dependency 'bundler', '~> 2.0'
|
35
|
-
spec.add_development_dependency 'rake', '~>
|
35
|
+
spec.add_development_dependency 'rake', '~> 13.0'
|
36
36
|
spec.add_development_dependency 'rspec', '~> 3.0'
|
37
37
|
spec.add_development_dependency 'rubocop', '~> 0.66'
|
38
38
|
spec.add_development_dependency 'rubocop-rspec', '~> 1.32.0'
|
39
39
|
spec.add_development_dependency 'simplecov', '~> 0.16'
|
40
|
+
spec.add_development_dependency 'codecov', '~> 0.4'
|
40
41
|
end
|
@@ -1,11 +1,11 @@
|
|
1
1
|
require 'consul_application_settings/version'
|
2
2
|
require 'consul_application_settings/configuration'
|
3
|
-
require 'consul_application_settings/
|
4
|
-
require 'consul_application_settings/
|
3
|
+
require 'consul_application_settings/consul_provider'
|
4
|
+
require 'consul_application_settings/file_provider'
|
5
|
+
require 'consul_application_settings/settings_provider'
|
5
6
|
require 'consul_application_settings/utils'
|
6
|
-
require 'diplomat'
|
7
7
|
|
8
|
-
#
|
8
|
+
# The gem provides possibility to load settings from Consul and automatically fall back to data stored in file system
|
9
9
|
module ConsulApplicationSettings
|
10
10
|
class Error < StandardError; end
|
11
11
|
|
@@ -16,19 +16,11 @@ module ConsulApplicationSettings
|
|
16
16
|
|
17
17
|
self.config ||= ConsulApplicationSettings::Configuration.new
|
18
18
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
self.defaults = ConsulApplicationSettings::Defaults.read(config.defaults_path)
|
23
|
-
end
|
24
|
-
|
25
|
-
def load_from(path)
|
26
|
-
settings_path = ConsulApplicationSettings::Utils.generate_path(config.namespace, path)
|
27
|
-
ConsulApplicationSettings::Options.new(settings_path, defaults)
|
28
|
-
end
|
19
|
+
def self.configure
|
20
|
+
yield(config)
|
21
|
+
end
|
29
22
|
|
30
|
-
|
31
|
-
|
32
|
-
end
|
23
|
+
def self.load(path = '')
|
24
|
+
SettingsProvider.new(path, config)
|
33
25
|
end
|
34
26
|
end
|
@@ -1,14 +1,14 @@
|
|
1
1
|
module ConsulApplicationSettings
|
2
2
|
# All gem configuration settings
|
3
3
|
class Configuration
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
attr_accessor :namespace
|
8
|
-
attr_accessor :disable_consul_connection_errors
|
4
|
+
DEFAULT_BASE_FILE_PATH = 'config/app_settings.yml'.freeze
|
5
|
+
DEFAULT_LOCAL_FILE_PATH = 'config/app_settings.local.yml'.freeze
|
6
|
+
attr_accessor :base_file_path, :local_file_path, :disable_consul_connection_errors
|
9
7
|
|
10
8
|
def initialize
|
11
|
-
|
9
|
+
@base_file_path = DEFAULT_BASE_FILE_PATH
|
10
|
+
@local_file_path = DEFAULT_LOCAL_FILE_PATH
|
11
|
+
@disable_consul_connection_errors = true
|
12
12
|
end
|
13
13
|
end
|
14
14
|
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'diplomat'
|
2
|
+
|
3
|
+
module ConsulApplicationSettings
|
4
|
+
# Provides access to settings stored in Consul
|
5
|
+
class ConsulProvider
|
6
|
+
def initialize(base_path, config)
|
7
|
+
@base_path = base_path
|
8
|
+
@config = config
|
9
|
+
end
|
10
|
+
|
11
|
+
def get(path)
|
12
|
+
value = fetch_value(path)
|
13
|
+
ConsulApplicationSettings::Utils.cast_consul_value(value)
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def fetch_value(path)
|
19
|
+
full_path = generate_full_path(path)
|
20
|
+
Diplomat::Kv.get(full_path, {}, :return)
|
21
|
+
rescue SystemCallError, Faraday::ConnectionFailed, Diplomat::PathNotFound => e
|
22
|
+
raise e unless disable_consul_connection_errors?
|
23
|
+
end
|
24
|
+
|
25
|
+
def generate_full_path(path)
|
26
|
+
ConsulApplicationSettings::Utils.generate_path(@base_path, path)
|
27
|
+
end
|
28
|
+
|
29
|
+
def disable_consul_connection_errors?
|
30
|
+
@config.disable_consul_connection_errors
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
|
3
|
+
module ConsulApplicationSettings
|
4
|
+
# Provides access to settings stored in file system with support of base and local files
|
5
|
+
class FileProvider
|
6
|
+
def initialize(base_path, config)
|
7
|
+
@base_path = base_path
|
8
|
+
@config = config
|
9
|
+
load
|
10
|
+
end
|
11
|
+
|
12
|
+
def get(path)
|
13
|
+
read_path(path).clone
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def load
|
19
|
+
base_yml = read_yml(base_file_path)
|
20
|
+
local_yml = read_yml(local_file_path)
|
21
|
+
@data = DeepMerge.deep_merge!(local_yml, base_yml, preserve_unmergeables: false, overwrite_arrays: true,
|
22
|
+
merge_nil_values: true)
|
23
|
+
end
|
24
|
+
|
25
|
+
def base_file_path
|
26
|
+
@config.base_file_path
|
27
|
+
end
|
28
|
+
|
29
|
+
def local_file_path
|
30
|
+
@config.local_file_path
|
31
|
+
end
|
32
|
+
|
33
|
+
def read_yml(path)
|
34
|
+
return {} unless File.exist?(path)
|
35
|
+
|
36
|
+
YAML.safe_load(IO.read(path))
|
37
|
+
rescue Psych::SyntaxError, Errno::ENOENT => e
|
38
|
+
raise ConsulApplicationSettings::Error, "Cannot read settings file at #{path}: #{e.message}"
|
39
|
+
end
|
40
|
+
|
41
|
+
def read_path(path)
|
42
|
+
full_path = ConsulApplicationSettings::Utils.generate_path(@base_path, path)
|
43
|
+
parts = ConsulApplicationSettings::Utils.decompose_path(full_path)
|
44
|
+
parts.reduce(@data, &method(:read_value))
|
45
|
+
end
|
46
|
+
|
47
|
+
def read_value(hash, key)
|
48
|
+
raise ConsulApplicationSettings::Error, 'reading arrays not implemented' if hash.is_a? Array
|
49
|
+
return {} if hash.nil?
|
50
|
+
|
51
|
+
hash.fetch(key.to_s, nil)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module ConsulApplicationSettings
|
2
|
+
# Provides access to settings stored in Consul or in file system
|
3
|
+
class SettingsProvider
|
4
|
+
def initialize(base_path, config)
|
5
|
+
@consul_provider = ConsulProvider.new(base_path, config)
|
6
|
+
@file_provider = FileProvider.new(base_path, config)
|
7
|
+
end
|
8
|
+
|
9
|
+
def get(path)
|
10
|
+
consul_value = @consul_provider.get(path)
|
11
|
+
!consul_value.nil? && consul_value != '' ? consul_value : @file_provider.get(path)
|
12
|
+
end
|
13
|
+
|
14
|
+
alias [] get
|
15
|
+
end
|
16
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: consul_application_settings
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Volodymyr Mykhailyk
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-02-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: diplomat
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: 2.1
|
19
|
+
version: 2.5.1
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: 2.1
|
26
|
+
version: 2.5.1
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: bundler
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -44,14 +44,14 @@ dependencies:
|
|
44
44
|
requirements:
|
45
45
|
- - "~>"
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version: '
|
47
|
+
version: '13.0'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
52
|
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version: '
|
54
|
+
version: '13.0'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: rspec
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
@@ -108,6 +108,20 @@ dependencies:
|
|
108
108
|
- - "~>"
|
109
109
|
- !ruby/object:Gem::Version
|
110
110
|
version: '0.16'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: codecov
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - "~>"
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0.4'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - "~>"
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0.4'
|
111
125
|
description: |-
|
112
126
|
Gem that simplifies usage of Consul (via Diplomat gem) to host application settings.
|
113
127
|
Gem provides defaults and utilities
|
@@ -117,7 +131,6 @@ executables: []
|
|
117
131
|
extensions: []
|
118
132
|
extra_rdoc_files: []
|
119
133
|
files:
|
120
|
-
- ".codeclimate.yml"
|
121
134
|
- ".github/workflows/main.yml"
|
122
135
|
- ".gitignore"
|
123
136
|
- ".rspec"
|
@@ -136,8 +149,9 @@ files:
|
|
136
149
|
- consul_application_settings.gemspec
|
137
150
|
- lib/consul_application_settings.rb
|
138
151
|
- lib/consul_application_settings/configuration.rb
|
139
|
-
- lib/consul_application_settings/
|
140
|
-
- lib/consul_application_settings/
|
152
|
+
- lib/consul_application_settings/consul_provider.rb
|
153
|
+
- lib/consul_application_settings/file_provider.rb
|
154
|
+
- lib/consul_application_settings/settings_provider.rb
|
141
155
|
- lib/consul_application_settings/utils.rb
|
142
156
|
- lib/consul_application_settings/version.rb
|
143
157
|
homepage: https://github.com/matic-insurance/consul_application_settings
|
data/.codeclimate.yml
DELETED
@@ -1,15 +0,0 @@
|
|
1
|
-
version: 2
|
2
|
-
plugins:
|
3
|
-
# disabled before git-legal supports bundler 2 https://github.com/kmewhort/git.legal-codeclimate/issues/6
|
4
|
-
# git-legal:
|
5
|
-
# enabled: true
|
6
|
-
rubocop:
|
7
|
-
enabled: true
|
8
|
-
channel: rubocop-0-73
|
9
|
-
config:
|
10
|
-
file: ".rubocop.yml"
|
11
|
-
bundler-audit:
|
12
|
-
enabled: true
|
13
|
-
exclude_patterns:
|
14
|
-
- "spec/"
|
15
|
-
- "*.gemspec"
|
@@ -1,43 +0,0 @@
|
|
1
|
-
require 'yaml'
|
2
|
-
|
3
|
-
module ConsulApplicationSettings
|
4
|
-
# Reading default file from YAML file and providing interface to query them
|
5
|
-
class Defaults
|
6
|
-
attr_reader :contents
|
7
|
-
|
8
|
-
def initialize(hash)
|
9
|
-
@contents = hash
|
10
|
-
end
|
11
|
-
|
12
|
-
def get(name)
|
13
|
-
read_path(name, contents)
|
14
|
-
end
|
15
|
-
|
16
|
-
def load_from(path)
|
17
|
-
keys = ConsulApplicationSettings::Utils.decompose_path(path)
|
18
|
-
new_defaults = keys.reduce(contents) { |hash, key| read_path(key, hash, {}) }
|
19
|
-
self.class.new(new_defaults)
|
20
|
-
end
|
21
|
-
|
22
|
-
def self.read(path)
|
23
|
-
new YAML.safe_load(IO.read(path))
|
24
|
-
rescue Psych::SyntaxError, Errno::ENOENT => e
|
25
|
-
raise ConsulApplicationSettings::Error, "Cannot read defaults file at #{path}: #{e.message}"
|
26
|
-
end
|
27
|
-
|
28
|
-
private
|
29
|
-
|
30
|
-
def read_path(path, hash, default = nil)
|
31
|
-
parts = ConsulApplicationSettings::Utils.decompose_path(path)
|
32
|
-
result = parts.reduce(hash, &method(:read_value))
|
33
|
-
result || default
|
34
|
-
end
|
35
|
-
|
36
|
-
def read_value(hash, key)
|
37
|
-
raise ConsulApplicationSettings::Error, 'reading arrays not implemented' if hash.is_a? Array
|
38
|
-
return {} if hash.nil?
|
39
|
-
|
40
|
-
hash.fetch(key.to_s)
|
41
|
-
end
|
42
|
-
end
|
43
|
-
end
|
@@ -1,53 +0,0 @@
|
|
1
|
-
require 'json'
|
2
|
-
|
3
|
-
module ConsulApplicationSettings
|
4
|
-
# Reads settings from consul or ask defaults for value
|
5
|
-
class Options
|
6
|
-
attr_reader :path, :defaults
|
7
|
-
|
8
|
-
def initialize(path, defaults)
|
9
|
-
@path = path
|
10
|
-
@defaults = defaults.load_from(path)
|
11
|
-
end
|
12
|
-
|
13
|
-
def load_from(new_path)
|
14
|
-
full_path = ConsulApplicationSettings::Utils.generate_path(path, new_path)
|
15
|
-
self.class.new(full_path, defaults)
|
16
|
-
end
|
17
|
-
|
18
|
-
def get(name)
|
19
|
-
consul_value = key_value(name)
|
20
|
-
if consul_value.nil? || consul_value.empty?
|
21
|
-
defaults.get(name)
|
22
|
-
else
|
23
|
-
ConsulApplicationSettings::Utils.cast_consul_value(consul_value)
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
def [](name)
|
28
|
-
get(name)
|
29
|
-
end
|
30
|
-
|
31
|
-
# rubocop:disable Style/MethodMissingSuper
|
32
|
-
def method_missing(name, *_args)
|
33
|
-
get(name)
|
34
|
-
end
|
35
|
-
# rubocop:enable Style/MethodMissingSuper
|
36
|
-
|
37
|
-
def respond_to_missing?(_name)
|
38
|
-
true
|
39
|
-
end
|
40
|
-
|
41
|
-
private
|
42
|
-
|
43
|
-
def key_value(name)
|
44
|
-
Diplomat::Kv.get(key_path(name), {}, :return)
|
45
|
-
rescue SystemCallError, Faraday::ConnectionFailed, Diplomat::PathNotFound => e
|
46
|
-
raise e unless ConsulApplicationSettings.config.disable_consul_connection_errors
|
47
|
-
end
|
48
|
-
|
49
|
-
def key_path(name)
|
50
|
-
ConsulApplicationSettings::Utils.generate_path(path, name)
|
51
|
-
end
|
52
|
-
end
|
53
|
-
end
|