precursor 0.2.0 → 0.5.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 +73 -0
- data/lib/config_root.rb +18 -0
- data/lib/env_vault.rb +3 -5
- data/lib/hash_vault.rb +15 -0
- data/lib/override_vault.rb +25 -0
- data/lib/precursor.rb +5 -0
- data/lib/thread_local_override_vault.rb +13 -0
- data/lib/vault.rb +9 -3
- data/lib/version.rb +1 -1
- data/lib/yaml_vault.rb +33 -0
- metadata +21 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bba5bce81c5c7e1d955c9f5c0acb4cf3898a5512f8fd66c1378469e6afe38081
|
4
|
+
data.tar.gz: 41f71f5e7a582aa678c1a62c5e453c31416de9d10706270b69cbae031ba98299
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 14d9ed609d3b9d5322fa42da86698b5e6e7eab865c8c190aafc69a10d76f2c47f6be3667281dd9847a1549ec7cb5c41dbaebd3ede2de5e095d3c47e69394c997
|
7
|
+
data.tar.gz: 4b6ec72589f67c6f701e49ec026a7dfc14208ee0632c5d23d25adff565502e3decd51b98ff37db6cd12544bd039441e532b776f42795ef9f19e04330feafe23e
|
data/lib/argv_vault.rb
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'optparse'
|
4
|
+
require 'optparse/date'
|
5
|
+
require 'optparse/time'
|
6
|
+
require 'optparse/uri'
|
7
|
+
|
8
|
+
require_relative 'vault'
|
9
|
+
|
10
|
+
module Precursor
|
11
|
+
# Vault that parses command line arguments
|
12
|
+
class ArgvVault < Vault
|
13
|
+
def initialize(argv)
|
14
|
+
@vault_data = {}
|
15
|
+
@parser = OptionParser.new do |parser|
|
16
|
+
parser.on('-h', '--help', 'Prints this help') do
|
17
|
+
puts parser
|
18
|
+
exit
|
19
|
+
end
|
20
|
+
end
|
21
|
+
yield self
|
22
|
+
@parser.parse(argv)
|
23
|
+
end
|
24
|
+
|
25
|
+
def key(key_name)
|
26
|
+
builder = ArgvKeyBuilder.new(key_name)
|
27
|
+
yield builder
|
28
|
+
builder.build(@parser, @vault_data)
|
29
|
+
end
|
30
|
+
|
31
|
+
protected
|
32
|
+
|
33
|
+
def store
|
34
|
+
@vault_data
|
35
|
+
end
|
36
|
+
|
37
|
+
class ArgvKeyBuilder
|
38
|
+
def initialize(key_name)
|
39
|
+
@key_name = key_name
|
40
|
+
@short = []
|
41
|
+
@long = []
|
42
|
+
@type = nil
|
43
|
+
@description = nil
|
44
|
+
end
|
45
|
+
|
46
|
+
def short(short_key)
|
47
|
+
@short.push(short_key)
|
48
|
+
end
|
49
|
+
|
50
|
+
def long(long_key)
|
51
|
+
@long.push(long_key)
|
52
|
+
end
|
53
|
+
|
54
|
+
def type(type)
|
55
|
+
@type = type
|
56
|
+
end
|
57
|
+
|
58
|
+
def description(desc)
|
59
|
+
@description = desc
|
60
|
+
end
|
61
|
+
|
62
|
+
def build(parser, vault_data)
|
63
|
+
args = @short + @long
|
64
|
+
args.push(@type) unless @type.nil?
|
65
|
+
args.push(@description) unless @description.nil?
|
66
|
+
|
67
|
+
parser.on(*args) do |v|
|
68
|
+
vault_data[@key_name] = v
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
data/lib/config_root.rb
CHANGED
@@ -24,7 +24,25 @@ module Precursor
|
|
24
24
|
end
|
25
25
|
|
26
26
|
raise KeyError, "key #{key} not found" unless found
|
27
|
+
|
28
|
+
value = resolve_variables(value) if value.is_a? String
|
29
|
+
|
27
30
|
value
|
28
31
|
end
|
32
|
+
|
33
|
+
private
|
34
|
+
|
35
|
+
VAR_PATTERN = /\$\{(?<var_name>[a-zA-Z]+[\w.]+)\}/
|
36
|
+
MAX_DEPTH = 16
|
37
|
+
|
38
|
+
def resolve_variables(str)
|
39
|
+
res = str
|
40
|
+
until (m = res.match(VAR_PATTERN)).nil?
|
41
|
+
k = m[1]
|
42
|
+
v = self[k.to_sym]
|
43
|
+
res = res.sub("${#{k}}", v.to_s)
|
44
|
+
end
|
45
|
+
res
|
46
|
+
end
|
29
47
|
end
|
30
48
|
end
|
data/lib/env_vault.rb
CHANGED
data/lib/hash_vault.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'vault'
|
4
|
+
|
5
|
+
module Precursor
|
6
|
+
class OverrideVault < Vault
|
7
|
+
def initialize
|
8
|
+
@vault_data = {}
|
9
|
+
end
|
10
|
+
|
11
|
+
def override(key, value)
|
12
|
+
store[key] = value
|
13
|
+
end
|
14
|
+
|
15
|
+
def clear(key)
|
16
|
+
store.delete(key)
|
17
|
+
end
|
18
|
+
|
19
|
+
protected
|
20
|
+
|
21
|
+
def store
|
22
|
+
@vault_data
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
data/lib/precursor.rb
CHANGED
@@ -2,7 +2,12 @@
|
|
2
2
|
|
3
3
|
require_relative 'config_root_builder'
|
4
4
|
|
5
|
+
require_relative 'argv_vault'
|
5
6
|
require_relative 'env_vault'
|
7
|
+
require_relative 'hash_vault'
|
8
|
+
require_relative 'override_vault'
|
9
|
+
require_relative 'thread_local_override_vault'
|
10
|
+
require_relative 'yaml_vault'
|
6
11
|
|
7
12
|
module Precursor
|
8
13
|
class << self
|
data/lib/vault.rb
CHANGED
@@ -5,14 +5,20 @@ module Precursor
|
|
5
5
|
class Vault
|
6
6
|
# Returns true if the vault has a value for the given key, otherwise false
|
7
7
|
# @return [Boolean] true if the vault has a value for the given key, otherwise false
|
8
|
-
def key?
|
9
|
-
|
8
|
+
def key?(key)
|
9
|
+
store.key?(key)
|
10
10
|
end
|
11
11
|
|
12
12
|
# Returns a value fot the given key, nil if key is not available
|
13
13
|
# @return [Object|Nil] a value for the given key
|
14
14
|
def value(key)
|
15
|
-
|
15
|
+
store[key]
|
16
|
+
end
|
17
|
+
|
18
|
+
protected
|
19
|
+
# Replace this method in descendant classes to return Hash-like store
|
20
|
+
def store
|
21
|
+
{}
|
16
22
|
end
|
17
23
|
end
|
18
24
|
end
|
data/lib/version.rb
CHANGED
data/lib/yaml_vault.rb
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'yaml'
|
4
|
+
|
5
|
+
require_relative 'vault'
|
6
|
+
|
7
|
+
module Precursor
|
8
|
+
class YamlVault < Vault
|
9
|
+
def initialize(src)
|
10
|
+
@yaml = flat_hash(YAML.load(src))
|
11
|
+
end
|
12
|
+
|
13
|
+
protected
|
14
|
+
|
15
|
+
def store
|
16
|
+
@yaml
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def flat_hash(hash, prefix = nil)
|
22
|
+
flat_hash = hash.flat_map do |k, v|
|
23
|
+
k = "#{prefix}.#{k}" unless prefix.nil?
|
24
|
+
if v.is_a? Hash
|
25
|
+
flat_hash(v, k)
|
26
|
+
else
|
27
|
+
{ k => v }
|
28
|
+
end
|
29
|
+
end
|
30
|
+
flat_hash.reduce(:merge).transform_keys { |k| k.to_sym }
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
metadata
CHANGED
@@ -1,15 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: precursor
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.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-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: optparse
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.2.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.2.0
|
13
27
|
- !ruby/object:Gem::Dependency
|
14
28
|
name: debug
|
15
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -31,13 +45,18 @@ executables: []
|
|
31
45
|
extensions: []
|
32
46
|
extra_rdoc_files: []
|
33
47
|
files:
|
48
|
+
- lib/argv_vault.rb
|
34
49
|
- lib/config_key_builder.rb
|
35
50
|
- lib/config_root.rb
|
36
51
|
- lib/config_root_builder.rb
|
37
52
|
- lib/env_vault.rb
|
53
|
+
- lib/hash_vault.rb
|
54
|
+
- lib/override_vault.rb
|
38
55
|
- lib/precursor.rb
|
56
|
+
- lib/thread_local_override_vault.rb
|
39
57
|
- lib/vault.rb
|
40
58
|
- lib/version.rb
|
59
|
+
- lib/yaml_vault.rb
|
41
60
|
homepage: https://github.com/the-vk/precursor
|
42
61
|
licenses:
|
43
62
|
- MIT
|