precursor 0.6.0 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 86985495bc3e48757e33837b70e20bd1a431f5e19ce750dd8a38cc6512ed0ffd
4
- data.tar.gz: fecd720458ce41d39b8f000754500b0ebb9fc586a4cd7bf869657cc3b22391ec
3
+ metadata.gz: f390a1a3b8749ba095c88074ad75d0d999cee718a6d394abea80e269f50327dd
4
+ data.tar.gz: d6a2b52eb7cd36d0b5144a96fed24f76f708558d53782a69426bc80ca9e506df
5
5
  SHA512:
6
- metadata.gz: c582c2744224ddfb04b89eddfe0193bbb4b17ff789db6b3a1c56905806f3f769d1bbb3efe9f3f1e9211d1a0693cc262ca915991ea6ec3f5911ffa6df74ae5b41
7
- data.tar.gz: 01e5f3bade8c2b1c16d7e630b299984a1abd5a752bf60a7ab0b2f2b0726e7fdb14709a5eeb23a5027587b663995fa1891afc4e3654aba3cea0b6dec8158a39c3
6
+ metadata.gz: a414e2d13365a2bea45566673d6b79e301f2ec736a93cff32304574ecf4b48a05267db80288c0128af4182df19a84d5c5be4fd9851503c6a4002aea210fce43c
7
+ data.tar.gz: 9177ba4950d23d8f02fb4d8bea81b5806de34c5674a93600467d9235af9f09c514a79e560c958da4f4bd162198afbb56f6d4823d831c3b084b69355e1f4946f2
data/lib/argv_vault.rb CHANGED
@@ -10,9 +10,10 @@ require_relative 'vault'
10
10
  module Precursor
11
11
  # Vault that parses command line arguments
12
12
  class ArgvVault < Vault
13
- def initialize(argv, priority:)
14
- super(priority: priority)
13
+ def initialize(argv)
14
+ super()
15
15
 
16
+ @argv = argv
16
17
  @vault_data = {}
17
18
  @parser = OptionParser.new do |parser|
18
19
  parser.on('-h', '--help', 'Prints this help') do
@@ -21,7 +22,6 @@ module Precursor
21
22
  end
22
23
  end
23
24
  yield self
24
- @parser.parse(argv)
25
25
  end
26
26
 
27
27
  def key(key_name)
@@ -32,7 +32,8 @@ module Precursor
32
32
 
33
33
  protected
34
34
 
35
- def store
35
+ def load_store(_config_root)
36
+ @parser.parse(@argv)
36
37
  @vault_data
37
38
  end
38
39
 
data/lib/config_root.rb CHANGED
@@ -4,8 +4,12 @@ module Precursor
4
4
  # Root class to access config vaults
5
5
  class ConfigRoot
6
6
  def initialize(vaults, key_options)
7
- @vaults = vaults.sort_by(&:priority).reverse
7
+ @vaults = vaults
8
8
  @key_options = key_options
9
+
10
+ @vaults.each do |v|
11
+ v.load(self)
12
+ end
9
13
  end
10
14
 
11
15
  def [](key)
@@ -14,11 +18,17 @@ module Precursor
14
18
  value = key_vault.value key unless key_vault.nil?
15
19
  value = get_default(key) if key_vault.nil?
16
20
 
17
- value.is_a?(String) ? resolve_variables(value) : value
21
+ value.is_a?(String) ? resolve(value) : value
18
22
  end
19
23
 
20
- def add_vault(vault)
21
- @vaults = @vaults.push(vault).sort_by(&:priority).reverse
24
+ def resolve(str)
25
+ res = str
26
+ until (m = res.match(VAR_PATTERN)).nil?
27
+ k = m[1]
28
+ v = self[k.to_sym]
29
+ res = res.sub("${#{k}}", v.to_s)
30
+ end
31
+ res
22
32
  end
23
33
 
24
34
  private
@@ -32,15 +42,5 @@ module Precursor
32
42
 
33
43
  @key_options[key][:default]
34
44
  end
35
-
36
- def resolve_variables(str)
37
- res = str
38
- until (m = res.match(VAR_PATTERN)).nil?
39
- k = m[1]
40
- v = self[k.to_sym]
41
- res = res.sub("${#{k}}", v.to_s)
42
- end
43
- res
44
- end
45
45
  end
46
46
  end
data/lib/env_vault.rb CHANGED
@@ -8,21 +8,19 @@ 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(priority:, separator: '__', allow_list: [])
12
- super(priority: priority)
11
+ def initialize(separator: '__', allow_list: [])
12
+ super()
13
13
  @separator = separator
14
14
 
15
- allow_set = Set.new(allow_list)
16
-
17
- @env_vars = ENV.select { |n, _| allow_set.empty? || allow_set.include?(n) }.transform_keys do |name|
18
- name.sub(@separator, '.').to_sym
19
- end
15
+ @allow_set = Set.new(allow_list)
20
16
  end
21
17
 
22
18
  protected
23
19
 
24
- def store
25
- @env_vars
20
+ def load_store(_config_root)
21
+ ENV.select { |n, _| @allow_set.empty? || @allow_set.include?(n) }.transform_keys do |name|
22
+ name.sub(@separator, '.').to_sym
23
+ end
26
24
  end
27
25
  end
28
26
  end
data/lib/hash_vault.rb CHANGED
@@ -5,13 +5,15 @@ require_relative 'vault'
5
5
  module Precursor
6
6
  # Vault that reads config data from passed hash
7
7
  class HashVault < Vault
8
- def initialize(hash, priority:)
9
- super(priority: priority)
8
+ def initialize(hash)
9
+ super()
10
10
  @store = hash
11
11
  end
12
12
 
13
13
  protected
14
14
 
15
- attr_reader :store
15
+ def load_store(_config_root)
16
+ @store
17
+ end
16
18
  end
17
19
  end
@@ -1,13 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative 'vault'
3
+ require_relative 'hash_vault'
4
4
 
5
5
  module Precursor
6
6
  # Vault that stores data in hash and allows to override config on the fly
7
7
  class OverrideVault < Vault
8
- def initialize(priority:)
9
- super(priority: priority)
10
- @vault_data = {}
8
+ def initialize(hash = {})
9
+ super(hash)
11
10
  end
12
11
 
13
12
  def override(key, value)
@@ -17,11 +16,5 @@ module Precursor
17
16
  def clear(key)
18
17
  store.delete(key)
19
18
  end
20
-
21
- protected
22
-
23
- def store
24
- @vault_data
25
- end
26
19
  end
27
20
  end
@@ -8,7 +8,7 @@ module Precursor
8
8
  protected
9
9
 
10
10
  def store
11
- Thread.current[:precursor_override_vault] ||= {}
11
+ Thread.current[:precursor_override_vault] ||= @store
12
12
  end
13
13
  end
14
14
  end
data/lib/vault.rb CHANGED
@@ -3,13 +3,12 @@
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
6
+ attr_reader :store
8
7
 
9
- # Initializes new instance of [Precursor::Vault]
10
- # @param priority [Integer] the vault priority
11
- def initialize(priority:)
12
- @priority = priority
8
+ # Loads vault data from the source
9
+ # @param config_root [Precursor::ConfigRoot] A reference to config root to read values or resolve vars
10
+ def load(config_root)
11
+ @store = load_store(config_root)
13
12
  end
14
13
 
15
14
  # Returns true if the vault has a value for the given key, otherwise false
@@ -26,8 +25,7 @@ module Precursor
26
25
 
27
26
  protected
28
27
 
29
- # Replace this method in descendant classes to return Hash-like store
30
- def store
28
+ def load_store(_config_root)
31
29
  {}
32
30
  end
33
31
  end
data/lib/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Precursor
4
- VERSION = '0.6.0'
4
+ VERSION = '0.8.0'
5
5
  end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'yaml_vault'
4
+
5
+ module Precursor
6
+ # A vault that reads yaml data from a file
7
+ class YamlFileVault < YamlVault
8
+ def initialize(file_path)
9
+ super('')
10
+ @file_path = file_path
11
+ end
12
+
13
+ protected
14
+
15
+ def load_store(config_root)
16
+ file_path = config_root.resolve(@file_path)
17
+ @yaml = File.new(file_path).read if File.exist?(file_path)
18
+
19
+ super(config_root)
20
+ end
21
+ end
22
+ end
data/lib/yaml_vault.rb CHANGED
@@ -7,15 +7,15 @@ require_relative 'vault'
7
7
  module Precursor
8
8
  # Vault that reads config data from a string with Yaml
9
9
  class YamlVault < Vault
10
- def initialize(src, priority:)
11
- super(priority: priority)
12
- @yaml = flat_hash(YAML.safe_load(src))
10
+ def initialize(yaml)
11
+ super()
12
+ @yaml = yaml
13
13
  end
14
14
 
15
15
  protected
16
16
 
17
- def store
18
- @yaml
17
+ def load_store(_config_root)
18
+ flat_hash(YAML.safe_load(@yaml))
19
19
  end
20
20
 
21
21
  private
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.6.0
4
+ version: 0.8.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-16 00:00:00.000000000 Z
11
+ date: 2022-09-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: optparse
@@ -84,6 +84,7 @@ files:
84
84
  - lib/thread_local_override_vault.rb
85
85
  - lib/vault.rb
86
86
  - lib/version.rb
87
+ - lib/yaml_file_vault.rb
87
88
  - lib/yaml_vault.rb
88
89
  homepage: https://github.com/the-vk/precursor
89
90
  licenses: