Module: EnvironmentConfigTransmogrifier
- Defined in:
- lib/environment_config_transmogrifier.rb
Overview
EnvironmentConfigTransmogrifier uses environment variables to generate config values for specific keys.
Defined Under Namespace
Classes: NoEscapeMustache
Constant Summary
collapse
- @@memoize_mustache =
{}
Class Method Summary
collapse
Class Method Details
.extendReplace(hash, path) ⇒ Object
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
# File 'lib/environment_config_transmogrifier.rb', line 84
def self.extendReplace(hash, path)
Dir.glob(File.join(path, '*')).each do |file|
key = File.basename(file)
value = File.read(file)
hash[key.upcase.tr('-', '_')] = value
end
end
|
.inject_value(hash, key_grams, value, original_key) ⇒ Object
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
# File 'lib/environment_config_transmogrifier.rb', line 102
def self.inject_value(hash, key_grams, value, original_key)
if key_grams.size == 1
unless value.nil? && hash[key_grams[0]].is_a?(Hash)
hash[key_grams[0]] = value
end
return
end
hash[key_grams[0]] = {} if hash[key_grams[0]].nil?
unless hash[key_grams[0]].is_a?(Hash)
raise NonHashValueOverride, \
"Refusing to override non-hash value #{hash[key_grams[0]].inspect} " \
"with #{value.inspect}: '#{key_grams.join('.')}'" \
" - Complete key: '#{original_key}'"
end
inject_value(hash[key_grams[0]], key_grams.drop(1), value, original_key)
end
|
.mustacheMessageFromError(e) ⇒ Object
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
# File 'lib/environment_config_transmogrifier.rb', line 69
def self.mustacheMessageFromError(e)
lines = e.message.split(/\n/)
return 'No reason for failure given by mustache library' if lines.empty?
return lines.first if lines.size < 4
caret_line = lines[3]
caret_pos = caret_line.index('^')
return lines[0] + '.' unless caret_pos
delimiter = '{{=(( ))=}}'
actual_line = lines[2]
leading_junk = actual_line.lstrip.start_with?(delimiter) ? actual_line.index(delimiter) : 0
lines[0] + ": Error at or near position #{caret_pos - leading_junk} of template value."
end
|
.processMustacheTemplate(value, input_hash, key) ⇒ Object
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
# File 'lib/environment_config_transmogrifier.rb', line 52
def self.processMustacheTemplate(value, input_hash, key)
val = @@memoize_mustache.fetch(value, {})[input_hash]
return val if val
delimiter = '{{=(( ))=}}'
begin
mustache_value = NoEscapeMustache.render("#{delimiter}#{value}", input_hash)
mustache_value = mustache_value.to_s.gsub("\n", "\n\n")
@@memoize_mustache[value] ||= {}
@@memoize_mustache[value][input_hash] = YAML.safe_load(mustache_value)
rescue => e
msg = mustacheMessageFromError(e)
raise LoadYamlFromMustacheError, "Could not load config key '#{key}': #{msg}"
end
end
|
.transmogrify(base_config, environment_templates, secrets: nil) ⇒ Hash
Processes the mustache templates and injects new keys into the configuration
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
# File 'lib/environment_config_transmogrifier.rb', line 22
def self.transmogrify(base_config, environment_templates, secrets: nil)
input_hash = ENV.to_hash
extendReplace(input_hash, secrets) if secrets && File.directory?(secrets)
input_hash.reject! { |_, v| v.nil? || v.empty? }
environment_templates.each do |key, value|
loop do
value = processMustacheTemplate(value, input_hash, key)
break unless value.respond_to?(:include?) && value.include?('((')
end
inject_value(base_config, key.split('.'), value, key)
end
base_config
end
|