Skip to content

Commit

Permalink
refactor loading of env/providers so they can be in a dedicated file
Browse files Browse the repository at this point in the history
  • Loading branch information
wr0ngway committed Sep 10, 2018
1 parent 518e1b7 commit fedf915
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 21 deletions.
43 changes: 26 additions & 17 deletions lib/simplygenius/atmos/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ def config_merge(lhs, rhs)
result = nil

return rhs if lhs.nil?
return lhs if rhs.nil?

# Warn if user fat fingered config
unless lhs.is_a?(rhs.class) || rhs.is_a?(lhs.class)
Expand Down Expand Up @@ -155,14 +156,34 @@ def load_config_sources(relative_root, config, *patterns)
config
end

def load_submap(relative_root, group, name, config)
submap_dir = File.join(relative_root, 'atmos', group)
submap_file = File.join(submap_dir, "#{name}.yml")
if File.exist?(submap_file)
logger.debug("Loading atmos #{group} config file: #{submap_file}")
h = SettingsHash.new({group => {name => YAML.load_file(submap_file)}})
config = config_merge(config, h)
@included_configs << submap_file
end

begin
submap = config.deep_fetch(group, name)
config = config_merge(config, submap)
rescue
logger.debug("No #{group} config found for '#{name}'")
end

config
end

def load
@config ||= begin

logger.debug("Atmos env: #{atmos_env}")

@full_config = SettingsHash.new
if ! File.exist?(config_file)
logger.warn "Could not find an atmos config file at: #{config_file}"
@full_config = SettingsHash.new
else
logger.debug("Loading atmos config file #{config_file}")
@full_config = SettingsHash.new(YAML.load_file(config_file))
Expand All @@ -172,24 +193,12 @@ def load
@full_config = load_config_sources(File.dirname(config_file), @full_config, *Array(@full_config[:config_sources]))

@full_config['provider'] = provider_name = @full_config['provider'] || 'aws'
global = SettingsHash.new(@full_config.reject {|k, v| ['environments', 'providers'].include?(k) })
begin
prov = @full_config.deep_fetch(:providers, provider_name)
rescue
logger.debug("No provider config found for '#{provider_name}'")
prov = {}
end

begin
env = @full_config.deep_fetch(:environments, atmos_env)
rescue
logger.debug("No environment config found for '#{atmos_env}'")
env = {}
end
@full_config = load_submap(File.dirname(config_file), 'providers', provider_name, @full_config)
@full_config = load_submap(File.dirname(config_file), 'environments', atmos_env, @full_config)

conf = config_merge(global, prov)
conf = config_merge(conf, env)
conf = config_merge(conf, {
global = SettingsHash.new(@full_config.reject {|k, v| ['providers', 'environments'].include?(k) })
conf = config_merge(global, {
atmos_env: atmos_env,
atmos_version: VERSION
})
Expand Down
48 changes: 45 additions & 3 deletions spec/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,46 @@ module Atmos

end

describe "load_submap" do

it "does nothing if no submap" do
within_construct do |c|
conf = SettingsHash.new
result = config.send(:load_submap, "", "environments", "dev", conf)
expect(result).to be conf
end
end

it "loads from submap" do
within_construct do |c|
c.file('config/atmos/environments/dev.yml', YAML.dump(foo: "bar"))
conf = SettingsHash.new
result = config.send(:load_submap, "#{c}/config", "environments", "dev", conf)
expect(result).to_not be conf
expect(result["environments"]["dev"]["foo"]).to eq("bar")
expect(result["foo"]).to eq("bar")
expect(config.instance_variable_get(:@included_configs)).to eq(["#{c}/config/atmos/environments/dev.yml"])
end
end

it "merges config additively" do
within_construct do |c|
c.file('config/atmos/environments/dev.yml', YAML.dump(foo: [1], bar: {baz: "boo"}))
conf = SettingsHash.new(environments: {dev: {foo: [2], bar: {bum: "hum"}}})
result = config.send(:load_submap, "#{c}/config", "environments", "dev", conf)
expect(result).to_not be conf
expect(result["environments"]["dev"]["foo"]).to eq([2, 1])
expect(result["foo"]).to eq([2, 1])
expect(result["environments"]["dev"]["bar"]["baz"]).to eq("boo")
expect(result["bar"]["baz"]).to eq("boo")
expect(result["environments"]["dev"]["bar"]["bum"]).to eq("hum")
expect(result["bar"]["bum"]).to eq("hum")
expect(config.instance_variable_get(:@included_configs)).to eq(["#{c}/config/atmos/environments/dev.yml"])
end
end

end

describe "load" do

it "warns if main config file not present" do
Expand Down Expand Up @@ -427,9 +467,11 @@ module Atmos
end

it "performs disjoint deep merge additively (handles nils)" do
lhs = {x: {y: 9}}
rhs = {a: {b: 8}}
expect(config.send(:config_merge, lhs, rhs)).to eq({x: {y: 9}, a: {b: 8}})
lhs = {x: {y: 9}, b: [{c: 7}], l: ['foo'], n: nil}
rhs = {a: {b: 8}, y: [6], l: nil, o: nil}
expect(config.send(:config_merge, lhs, rhs)).to eq({x: {y: 9}, y: [6], a: {b: 8}, b: [{c: 7}], l: ['foo'], n: nil, o: nil})
expect(config.send(:config_merge, rhs, lhs)).to eq({x: {y: 9}, y: [6], a: {b: 8}, b: [{c: 7}], l: ['foo'], n: nil, o: nil})
expect(Logging.contents).to_not match(/Different types in deep merge/)
end

it "allows array override" do
Expand Down
2 changes: 1 addition & 1 deletion spec/integration/initial_setup_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def atmos(*args, output_on_fail: true, allow_fail: false, stdin_data: nil)
expect(File.exist?('config/atmos.yml')).to be true
output = atmos "generate", "--force", *recipes_sourcepath, "aws/scaffold",
stdin_data: "acme\n123456789012\n"
expect(File.exist?('config/atmos/aws.yml')).to be true
expect(File.exist?('config/atmos/atmos-aws.yml')).to be true
conf = SimplyGenius::Atmos::SettingsHash.new(YAML.load_file('config/atmos.yml'))
expect(conf['org']).to eq("acme")
expect(conf.notation_get('environments.ops.account_id')).to eq("123456789012")
Expand Down

0 comments on commit fedf915

Please sign in to comment.