precursor 0.5.0 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/argv_vault.rb +4 -1
- data/lib/config_key_builder.rb +1 -0
- data/lib/config_root.rb +16 -18
- data/lib/config_root_builder.rb +1 -0
- data/lib/env_vault.rb +5 -4
- data/lib/hash_vault.rb +3 -1
- data/lib/override_vault.rb +3 -1
- data/lib/precursor.rb +1 -0
- data/lib/thread_local_override_vault.rb +1 -0
- data/lib/vault.rb +11 -1
- data/lib/version.rb +3 -1
- data/lib/yaml_vault.rb +5 -3
- metadata +32 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 86985495bc3e48757e33837b70e20bd1a431f5e19ce750dd8a38cc6512ed0ffd
|
4
|
+
data.tar.gz: fecd720458ce41d39b8f000754500b0ebb9fc586a4cd7bf869657cc3b22391ec
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c582c2744224ddfb04b89eddfe0193bbb4b17ff789db6b3a1c56905806f3f769d1bbb3efe9f3f1e9211d1a0693cc262ca915991ea6ec3f5911ffa6df74ae5b41
|
7
|
+
data.tar.gz: 01e5f3bade8c2b1c16d7e630b299984a1abd5a752bf60a7ab0b2f2b0726e7fdb14709a5eeb23a5027587b663995fa1891afc4e3654aba3cea0b6dec8158a39c3
|
data/lib/argv_vault.rb
CHANGED
@@ -10,7 +10,9 @@ require_relative 'vault'
|
|
10
10
|
module Precursor
|
11
11
|
# Vault that parses command line arguments
|
12
12
|
class ArgvVault < Vault
|
13
|
-
def initialize(argv)
|
13
|
+
def initialize(argv, priority:)
|
14
|
+
super(priority: priority)
|
15
|
+
|
14
16
|
@vault_data = {}
|
15
17
|
@parser = OptionParser.new do |parser|
|
16
18
|
parser.on('-h', '--help', 'Prints this help') do
|
@@ -34,6 +36,7 @@ module Precursor
|
|
34
36
|
@vault_data
|
35
37
|
end
|
36
38
|
|
39
|
+
# A builder to configure ArgvVault key options
|
37
40
|
class ArgvKeyBuilder
|
38
41
|
def initialize(key_name)
|
39
42
|
@key_name = key_name
|
data/lib/config_key_builder.rb
CHANGED
data/lib/config_root.rb
CHANGED
@@ -1,33 +1,24 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Precursor
|
4
|
+
# Root class to access config vaults
|
4
5
|
class ConfigRoot
|
5
6
|
def initialize(vaults, key_options)
|
6
|
-
@vaults = vaults
|
7
|
+
@vaults = vaults.sort_by(&:priority).reverse
|
7
8
|
@key_options = key_options
|
8
9
|
end
|
9
10
|
|
10
11
|
def [](key)
|
11
|
-
|
12
|
-
value = nil
|
13
|
-
@vaults.each do |v|
|
14
|
-
if v.key? key
|
15
|
-
found = true
|
16
|
-
value = v.value key
|
17
|
-
break
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
if !found && @key_options.key?(key) && @key_options[key].key?(:default)
|
22
|
-
found = true
|
23
|
-
value = @key_options[key][:default]
|
24
|
-
end
|
12
|
+
key_vault = @vaults.find { |v| v.key? key }
|
25
13
|
|
26
|
-
|
14
|
+
value = key_vault.value key unless key_vault.nil?
|
15
|
+
value = get_default(key) if key_vault.nil?
|
27
16
|
|
28
|
-
value
|
17
|
+
value.is_a?(String) ? resolve_variables(value) : value
|
18
|
+
end
|
29
19
|
|
30
|
-
|
20
|
+
def add_vault(vault)
|
21
|
+
@vaults = @vaults.push(vault).sort_by(&:priority).reverse
|
31
22
|
end
|
32
23
|
|
33
24
|
private
|
@@ -35,6 +26,13 @@ module Precursor
|
|
35
26
|
VAR_PATTERN = /\$\{(?<var_name>[a-zA-Z]+[\w.]+)\}/
|
36
27
|
MAX_DEPTH = 16
|
37
28
|
|
29
|
+
def get_default(key)
|
30
|
+
has_default = @key_options.key?(key) && @key_options[key].key?(:default)
|
31
|
+
raise KeyError, "key #{key} not found" unless has_default
|
32
|
+
|
33
|
+
@key_options[key][:default]
|
34
|
+
end
|
35
|
+
|
38
36
|
def resolve_variables(str)
|
39
37
|
res = str
|
40
38
|
until (m = res.match(VAR_PATTERN)).nil?
|
data/lib/config_root_builder.rb
CHANGED
data/lib/env_vault.rb
CHANGED
@@ -8,13 +8,14 @@ module Precursor
|
|
8
8
|
# Initializes new instance of [EnvVault]
|
9
9
|
# @param separator [String] separator for hierarchical config entries
|
10
10
|
# @param allow_list [Array<String>] list of allowed env vars to prevent leaking of sensitive or unrelated vars
|
11
|
-
def initialize(separator: '__', allow_list: [])
|
12
|
-
|
11
|
+
def initialize(priority:, separator: '__', allow_list: [])
|
12
|
+
super(priority: priority)
|
13
|
+
@separator = separator
|
13
14
|
|
14
15
|
allow_set = Set.new(allow_list)
|
15
16
|
|
16
|
-
@env_vars = ENV.select { |n, _| allow_set.empty? || allow_set.include?(n) }.
|
17
|
-
|
17
|
+
@env_vars = ENV.select { |n, _| allow_set.empty? || allow_set.include?(n) }.transform_keys do |name|
|
18
|
+
name.sub(@separator, '.').to_sym
|
18
19
|
end
|
19
20
|
end
|
20
21
|
|
data/lib/hash_vault.rb
CHANGED
data/lib/override_vault.rb
CHANGED
data/lib/precursor.rb
CHANGED
data/lib/vault.rb
CHANGED
@@ -1,8 +1,17 @@
|
|
1
|
-
# frozen_string_literal
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Precursor
|
4
4
|
# Base class for config vaults
|
5
5
|
class Vault
|
6
|
+
# Priority of the vault. Vaults with higher priority are checked first when looking for config value
|
7
|
+
attr_reader :priority
|
8
|
+
|
9
|
+
# Initializes new instance of [Precursor::Vault]
|
10
|
+
# @param priority [Integer] the vault priority
|
11
|
+
def initialize(priority:)
|
12
|
+
@priority = priority
|
13
|
+
end
|
14
|
+
|
6
15
|
# Returns true if the vault has a value for the given key, otherwise false
|
7
16
|
# @return [Boolean] true if the vault has a value for the given key, otherwise false
|
8
17
|
def key?(key)
|
@@ -16,6 +25,7 @@ module Precursor
|
|
16
25
|
end
|
17
26
|
|
18
27
|
protected
|
28
|
+
|
19
29
|
# Replace this method in descendant classes to return Hash-like store
|
20
30
|
def store
|
21
31
|
{}
|
data/lib/version.rb
CHANGED
data/lib/yaml_vault.rb
CHANGED
@@ -5,9 +5,11 @@ require 'yaml'
|
|
5
5
|
require_relative 'vault'
|
6
6
|
|
7
7
|
module Precursor
|
8
|
+
# Vault that reads config data from a string with Yaml
|
8
9
|
class YamlVault < Vault
|
9
|
-
def initialize(src)
|
10
|
-
|
10
|
+
def initialize(src, priority:)
|
11
|
+
super(priority: priority)
|
12
|
+
@yaml = flat_hash(YAML.safe_load(src))
|
11
13
|
end
|
12
14
|
|
13
15
|
protected
|
@@ -27,7 +29,7 @@ module Precursor
|
|
27
29
|
{ k => v }
|
28
30
|
end
|
29
31
|
end
|
30
|
-
flat_hash.reduce(:merge).transform_keys
|
32
|
+
flat_hash.reduce(:merge).transform_keys(&:to_sym)
|
31
33
|
end
|
32
34
|
end
|
33
35
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: precursor
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrey Maraev
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-09-
|
11
|
+
date: 2022-09-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: optparse
|
@@ -24,6 +24,20 @@ dependencies:
|
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: 0.2.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: code-scanning-rubocop
|
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'
|
27
41
|
- !ruby/object:Gem::Dependency
|
28
42
|
name: debug
|
29
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -38,6 +52,20 @@ dependencies:
|
|
38
52
|
- - ">="
|
39
53
|
- !ruby/object:Gem::Version
|
40
54
|
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rubocop
|
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'
|
41
69
|
description:
|
42
70
|
email:
|
43
71
|
- the_vk@thevk.net
|
@@ -60,7 +88,8 @@ files:
|
|
60
88
|
homepage: https://github.com/the-vk/precursor
|
61
89
|
licenses:
|
62
90
|
- MIT
|
63
|
-
metadata:
|
91
|
+
metadata:
|
92
|
+
rubygems_mfa_required: 'true'
|
64
93
|
post_install_message:
|
65
94
|
rdoc_options: []
|
66
95
|
require_paths:
|