Skip to content

Commit d2bc366

Browse files
committed
Refactor common functionality. Introduce support for definitions
1 parent 63e8eb9 commit d2bc366

File tree

2 files changed

+54
-11
lines changed

2 files changed

+54
-11
lines changed

lib/sfn-parameters/infrastructure.rb

+46-2
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,47 @@ def after_config_update(*_)
3232
# @param stack_name [String]
3333
# @return [Smash]
3434
def load_file_for(stack_name)
35-
root_path = config.fetch(:sfn_parameters, :directory, 'infrastructure')
3635
isolation_name = config.fetch(:sfn_parameters, :destination,
3736
ENV.fetch('SFN_PARAMETERS_DESTINATION', 'default')
3837
)
39-
paths = Dir.glob(File.join(root_path, "#{isolation_name}{#{VALID_EXTENSIONS.join(',')}}")).map(&:to_s)
38+
unpack_file(parameters_directory, isolation_name)
39+
end
40+
41+
# Detect and expand defined definitions
42+
#
43+
# @param config [Smash] current configuration
44+
# @return [Smash] expanded config
45+
def expand_config_file(config)
46+
new_config = config.to_smash
47+
definitions = config.fetch(:definitions, [])
48+
if(definitions.is_a?(Array))
49+
definitions.each do |def_name|
50+
definition = load_definition(def_name)
51+
new_config.deep_merge!(definition)
52+
end
53+
end
54+
new_config.keys.each do |key, value|
55+
if(value.is_a?(Hash))
56+
new_config[key] = expand_config_file(value)
57+
end
58+
end
59+
new_config
60+
end
61+
62+
# Define parameters directory for infrastructure based files
63+
#
64+
# @return [String]
65+
def parameters_directory
66+
config.fetch(:sfn_parameters, :directory, 'infrastructure')
67+
end
68+
69+
# Read and unpack file
70+
#
71+
# @param directory [String] directory path
72+
# @param name [String] file name without extension
73+
# @return [Smash]
74+
def unpack_file(directory, name)
75+
paths = Dir.glob(File.join(directory, "#{name}{#{VALID_EXTENSIONS.join(',')}}")).map(&:to_s)
4076
if(paths.size > 1)
4177
raise ArgumentError.new "Multiple parameter file matches encountered! (#{paths.join(', ')})"
4278
elsif(paths.empty?)
@@ -46,6 +82,14 @@ def load_file_for(stack_name)
4682
end
4783
end
4884

85+
# Load defintion file of given name
86+
#
87+
# @param name [String] name of definition
88+
# @return [Smash] definition contents
89+
def load_definition(name)
90+
unpack_file(File.join(parameters_directory, 'definitions'), name)
91+
end
92+
4993
# Process the given hash and set configuration values
5094
#
5195
# @param hash [Hash]

lib/sfn-parameters/stacks.rb

+8-9
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,14 @@ class ParametersStacks < ParametersInfrastructure
1010
# @param stack_name [String]
1111
# @return [Smash]
1212
def load_file_for(stack_name)
13-
root_path = config.fetch(:sfn_parameters, :directory, 'stacks')
14-
paths = Dir.glob(File.join(root_path, "#{stack_name}{#{VALID_EXTENSIONS.join(',')}}")).map(&:to_s)
15-
if(paths.size > 1)
16-
raise ArgumentError.new "Multiple parameter file matches encountered! (#{paths.join(', ')})"
17-
elsif(paths.empty?)
18-
Smash.new
19-
else
20-
unlock_content(Bogo::Config.new(paths.first).data)
21-
end
13+
unpack_file(parameters_directory, stack_name)
14+
end
15+
16+
# Define parameters directory for stacks based files
17+
#
18+
# @return [String]
19+
def parameters_directory
20+
config.fetch(:sfn_parameters, :directory, 'stacks')
2221
end
2322

2423
end

0 commit comments

Comments
 (0)