Skip to content

Commit

Permalink
refactoring metadata params and adding config reader
Browse files Browse the repository at this point in the history
  • Loading branch information
pghalliday committed Oct 28, 2015
1 parent 5141f38 commit 1e5394c
Show file tree
Hide file tree
Showing 8 changed files with 238 additions and 29 deletions.
22 changes: 19 additions & 3 deletions lib/formatron/configuration/config.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,32 @@
require_relative 'config/reader'
require 'deep_merge'

class Formatron
class Configuration
# Processes the config directory
module Config
CONFIG_DIR = 'config'
DEFAULT_CONFIG = '_default'
DEFAULT_JSON = '_default.json'

def self.targets(directory)
config = File.join directory, 'config'
config = File.join directory, CONFIG_DIR
Dir.entries(config).select do |entry|
path = File.join config, entry
File.directory?(path) && !%w(_default . ..).include?(entry)
File.directory?(path) && !%W(#{DEFAULT_CONFIG} . ..).include?(entry)
end
end

def self.target(_directory, _target)
def self.target(directory, target)
Reader.read(
File.join(directory, CONFIG_DIR, DEFAULT_CONFIG),
DEFAULT_JSON
).deep_merge!(
Reader.read(
File.join(directory, CONFIG_DIR, target),
DEFAULT_JSON
)
)
end
end
end
Expand Down
31 changes: 31 additions & 0 deletions lib/formatron/configuration/config/reader.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
require 'json'
require 'deep_merge'

class Formatron
class Configuration
module Config
# reads config directories into config hash
module Reader
def self.read(dir, default_file)
default = File.join(dir, default_file)
config = File.file?(default) ? JSON.parse(File.read(default)) : {}
entries = Dir.glob(File.join(dir, '*'), File::FNM_DOTMATCH)
do_entries entries, default_file, config
config
end

def self.do_entries(entries, default_file, config)
entries.each do |entry|
basename = File.basename(entry)
next if ['.', '..', default_file].include?(basename)
config[basename] = {} unless config[basename].is_a? Hash
config[basename].deep_merge!(
read(entry, default_file)
) if File.directory?(entry)
config[basename] = File.read(entry) if File.file?(entry)
end
end
end
end
end
end
19 changes: 8 additions & 11 deletions lib/formatron/configuration/formatronfile/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,21 @@ class Configuration
class Formatronfile
# DSL for the Formatronfile
class DSL
attr_reader(
:name,
:bucket
)

def initialize(_target, _config, file)
instance_eval File.read(file), file
end

def bootstrap(&block)
def bootstrap(name = nil, bucket = nil, &block)
@name = name unless name.nil?
@bucket = bucket unless bucket.nil?
@bootstrap = block if block_given?
@bootstrap
end

def name(value = nil)
@name = value unless value.nil?
@name
end

def bucket(value = nil)
@bucket = value unless value.nil?
@bucket
end
end
end
end
Expand Down
5 changes: 1 addition & 4 deletions lib/formatron/generators/bootstrap/Formatronfile.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
name '<%= params[:name] %>'
s3_bucket '<%= params[:s3_bucket] %>'

bootstrap do
bootstrap '<%= params[:name] %>', '<%= params[:s3_bucket] %>' do
protect config['protected']
kms_key '<%= params[:kms_key] %>'
hosted_zone_id '<%= params[:hosted_zone_id] %>'
Expand Down
144 changes: 144 additions & 0 deletions spec/formatron/configuration/config/reader_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
require 'spec_helper'
require 'formatron/configuration/config/reader'

describe Formatron::Configuration::Config::Reader do
include FakeFS::SpecHelpers

before(:each) do
Dir.mkdir('test')
end

context 'with just a default json file' do
before(:each) do
File.write(
'test/default.json', <<-EOH.gsub(/\s{10}/, '')
{
"test": "value"
}
EOH
)
@config = Formatron::Configuration::Config::Reader.read(
'test',
'default.json'
)
end

it 'should read the json into a hash' do
expect(@config).to eql(
'test' => 'value'
)
end
end

context 'with files but no default json file' do
before(:each) do
File.write(
'test/test-key', <<-EOH.gsub(/\s{10}/, '')
another value
EOH
)
@config = Formatron::Configuration::Config::Reader.read(
'test',
'default.json'
)
end

it 'should add the file content as a key to the hash' do
expect(@config).to eql(
'test-key' => "another value\n"
)
end
end

context 'with files and a default json file' do
before(:each) do
File.write(
'test/test-key', <<-EOH.gsub(/\s{10}/, '')
another value
EOH
)
File.write(
'test/default.json', <<-EOH.gsub(/\s{10}/, '')
{
"test": "value",
"test-key": "overriden value"
}
EOH
)
@config = Formatron::Configuration::Config::Reader.read(
'test',
'default.json'
)
end

it 'should merge the keys using the files to override the json' do
expect(@config).to eql(
'test' => 'value',
'test-key' => "another value\n"
)
end
end

context 'with sub directories' do
before(:each) do
File.write(
'test/test-key', <<-EOH.gsub(/\s{10}/, '')
another value
EOH
)
File.write(
'test/default.json', <<-EOH.gsub(/\s{10}/, '')
{
"test": "value",
"test-key": "overriden value",
"hello": "something",
"banana": {
"fruit": true
}
}
EOH
)
Dir.mkdir('test/hello')
File.write(
'test/hello/default.json', <<-EOH.gsub(/\s{10}/, '')
{
"more-test": "hello",
"and-again": "override me"
}
EOH
)
File.write(
'test/hello/and-again', <<-EOH.gsub(/\s{10}/, '')
new value
EOH
)
Dir.mkdir('test/banana')
File.write(
'test/banana/default.json', <<-EOH.gsub(/\s{10}/, '')
{
"color": "yellow"
}
EOH
)
@config = Formatron::Configuration::Config::Reader.read(
'test',
'default.json'
)
end

it 'should recurse and merge everything' do
expect(@config).to eql(
'test' => 'value',
'test-key' => "another value\n",
'hello' => {
'more-test' => 'hello',
'and-again' => "new value\n"
},
'banana' => {
'fruit' => true,
'color' => 'yellow'
}
)
end
end
end
37 changes: 33 additions & 4 deletions spec/formatron/configuration/config_spec.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,40 @@
require 'spec_helper'
require 'json'
require 'formatron/configuration/config'

describe Formatron::Configuration::Config do
include FakeFS::SpecHelpers

directory = 'test/configuration'
targets = %w(target1 target2 target3)
default_config = {
'param1' => 'param1 default',
'param2' => 'param2 default'
}
target_config = {}
merged_config = {}
targets.each do |target|
target_config[target] = {
'param2' => "param2 #{target}",
'param3' => "param3 #{target}"
}
merged_config[target] = {
'param1' => 'param1 default',
'param2' => "param2 #{target}",
'param3' => "param3 #{target}"
}
end

before(:each) do
FileUtils.mkdir_p File.join(directory, 'config', '_default')
default_dir = File.join directory, 'config', '_default'
FileUtils.mkdir_p default_dir
File.write File.join(default_dir, '_default.json'), default_config.to_json
targets.each do |target|
FileUtils.mkdir_p File.join(directory, 'config', target)
target_dir = File.join directory, 'config', target
FileUtils.mkdir_p target_dir
File.write File.join(
target_dir, '_default.json'
), target_config[target].to_json
end
end

Expand All @@ -23,8 +47,13 @@
end

describe '::target' do
skip 'should return the merged target configuration ' \
'in the config directory' do
it 'should return the merged target configuration ' \
'from the config directory' do
targets.each do |target|
expect(
Formatron::Configuration::Config.target(directory, target)
).to eql merged_config[target]
end
end
end
end
4 changes: 1 addition & 3 deletions spec/formatron/configuration/formatronfile/dsl_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@
File.write(
file,
<<-EOH.gsub(/^ {8}/, '')
name 'name'
bucket 'bucket'
bootstrap do
bootstrap 'name', 'bucket' do
'bootstrap'
end
EOH
Expand Down
5 changes: 1 addition & 4 deletions spec/formatron/generators/bootstrap_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,7 @@
it 'should generate a Formatronfile' do
actual = File.read File.join(directory, 'Formatronfile')
expect(actual).to eql <<-EOH.gsub(/^ {8}/, '')
name '#{params[:name]}'
s3_bucket '#{params[:s3_bucket]}'
bootstrap do
bootstrap '#{params[:name]}', '#{params[:s3_bucket]}' do
protect config['protected']
kms_key '#{params[:kms_key]}'
hosted_zone_id '#{params[:hosted_zone_id]}'
Expand Down

0 comments on commit 1e5394c

Please sign in to comment.