Skip to content

Commit

Permalink
Merge pull request #2 from tdupuy-inap/multiple_yaml
Browse files Browse the repository at this point in the history
Allow multiple yaml for config
  • Loading branch information
fbouliane authored Dec 23, 2021
2 parents e3fa5e7 + 152d673 commit f87ae4c
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 12 deletions.
17 changes: 15 additions & 2 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,20 @@ docker build --tag ruby-yaml-erb-tools-test .

(cd test && docker build --tag processor .)

[[ "This is the value" == "$(docker run -it --rm processor cat /processed/example.txt | tr -d '\r?\n')" ]]
[[ "value" == "$(docker run -it --rm processor cat /processed/key.txt | tr -d '\r?\n')" ]]
error=false

if [ "This is the value value_1 value_2 value_2_common" != "$(docker run -it --rm processor cat /processed/example.txt | tr -d '\r?\n')" ]; then
echo "Error in config.rb test"
error=true
fi

if [ "value" != "$(docker run -it --rm processor cat /processed/key.txt | tr -d '\r?\n')" ]; then
echo "Error in print_key.rb test"
error=true
fi

if [ "$error" = true ]; then
echo Fail!
exit 1
fi
echo Test Pass!
4 changes: 2 additions & 2 deletions test/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
FROM ruby-yaml-erb-tools-test AS builder

COPY config.yaml example.txt.erb ./
COPY ./ ./

RUN mkdir -p output/

RUN /bin/bash -c "./config.rb --yaml config.yaml example.txt.erb > output/example.txt";
RUN /bin/bash -c "./config.rb --yaml config_empty.yaml,config.yaml,otherConfigs example.txt.erb > output/example.txt";

RUN /bin/bash -c "./print_key.rb --yaml config.yaml parent.child > output/key.txt";

Expand Down
Empty file added test/config_empty.yaml
Empty file.
2 changes: 1 addition & 1 deletion test/example.txt.erb
Original file line number Diff line number Diff line change
@@ -1 +1 @@
This is the <%= @parent['child'] %>
This is the <%= @parent['child'] %> <%= @config_1_child %> <%= @config_2_child %> <%= @common_child %>
2 changes: 2 additions & 0 deletions test/otherConfigs/config1.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
config_1_child: value_1
common_child: value_1_common
2 changes: 2 additions & 0 deletions test/otherConfigs/config2.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
config_2_child: value_2
common_child: value_2_common
28 changes: 25 additions & 3 deletions tools/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,24 @@ def file_or_stdin(args, stdin = ::STDIN)

def main
options = OpenStruct.new
options.yamls = []

parser = OptionParser.new do |opts|
opts.banner = 'Usage: %s [options] file.erb' % $0
opts.on '-y', '--yaml=YAML-FILE', 'YAML file to populate local variables for the template' do |yaml_file|
File.open yaml_file, 'r' do |f|
options.yaml = YAML::load(f)
opts.on '-y', '--yaml=YAML-FILE(S)', Array, 'YAML file(s) to populate local variables for the template. separated by comma' do |configs|
configs.each do |config|
if File.directory?(config)
Dir.foreach(config) do |yaml|
next if yaml == '.' or yaml == '..'
File.open File.join(config, yaml), 'r' do |f|
options.yamls.push(YAML::load(f))
end
end
else
File.open config, 'r' do |f|
options.yamls.push(YAML::load(f))
end
end
end
end
end
Expand All @@ -53,6 +65,16 @@ def main
exit 1
end

options.yamls.each_with_index do |y, i|
if !y.nil? && y.inspect != 'false'
if options.yaml.nil?
options.yaml = y
else
options.yaml = options.yaml.merge y
end
end
end

file_or_stdin args do |input|
puts ERBContext.render(options.yaml || {}, input.read, nil, '-')
end
Expand Down
30 changes: 26 additions & 4 deletions tools/print_key.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,24 @@ def key_or_stdin(args, stdin = ::STDIN)

def main
options = OpenStruct.new
options.yamls = []

parser = OptionParser.new do |opts|
opts.banner = 'Usage: %s [options] key' % $0
opts.on '-y', '--yaml=YAML-FILE', 'YAML file to read the key from' do |yaml_file|
File.open yaml_file, 'r' do |f|
options.yaml = YAML::load(f)
opts.banner = 'Usage: %s [options] file.erb' % $0
opts.on '-y', '--yaml=YAML-FILE(S)', Array, 'YAML file(s) to populate local variables for the template. separated by comma' do |configs|
configs.each do |config|
if File.directory?(config)
Dir.foreach(config) do |yaml|
next if yaml == '.' or yaml == '..'
File.open File.join(config, yaml), 'r' do |f|
options.yamls.push(YAML::load(f))
end
end
else
File.open config, 'r' do |f|
options.yamls.push(YAML::load(f))
end
end
end
end
end
Expand All @@ -28,6 +40,16 @@ def main
exit 1
end

options.yamls.each_with_index do |y, i|
if !y.nil? && y.inspect != 'false'
if options.yaml.nil?
options.yaml = y
else
options.yaml = options.yaml.merge y
end
end
end

key_or_stdin args do |input|
content = options.yaml
input.split(".").each do |part|
Expand Down

0 comments on commit f87ae4c

Please sign in to comment.