Skip to content

Commit f87ae4c

Browse files
authored
Merge pull request #2 from tdupuy-inap/multiple_yaml
Allow multiple yaml for config
2 parents e3fa5e7 + 152d673 commit f87ae4c

File tree

8 files changed

+73
-12
lines changed

8 files changed

+73
-12
lines changed

test.sh

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,20 @@ docker build --tag ruby-yaml-erb-tools-test .
44

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

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

9+
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
10+
echo "Error in config.rb test"
11+
error=true
12+
fi
13+
14+
if [ "value" != "$(docker run -it --rm processor cat /processed/key.txt | tr -d '\r?\n')" ]; then
15+
echo "Error in print_key.rb test"
16+
error=true
17+
fi
18+
19+
if [ "$error" = true ]; then
20+
echo Fail!
21+
exit 1
22+
fi
1023
echo Test Pass!

test/Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
FROM ruby-yaml-erb-tools-test AS builder
22

3-
COPY config.yaml example.txt.erb ./
3+
COPY ./ ./
44

55
RUN mkdir -p output/
66

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

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

test/config_empty.yaml

Whitespace-only changes.

test/example.txt.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
This is the <%= @parent['child'] %>
1+
This is the <%= @parent['child'] %> <%= @config_1_child %> <%= @config_2_child %> <%= @common_child %>

test/otherConfigs/config1.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
config_1_child: value_1
2+
common_child: value_1_common

test/otherConfigs/config2.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
config_2_child: value_2
2+
common_child: value_2_common

tools/config.rb

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,24 @@ def file_or_stdin(args, stdin = ::STDIN)
3838

3939
def main
4040
options = OpenStruct.new
41+
options.yamls = []
4142

4243
parser = OptionParser.new do |opts|
4344
opts.banner = 'Usage: %s [options] file.erb' % $0
44-
opts.on '-y', '--yaml=YAML-FILE', 'YAML file to populate local variables for the template' do |yaml_file|
45-
File.open yaml_file, 'r' do |f|
46-
options.yaml = YAML::load(f)
45+
opts.on '-y', '--yaml=YAML-FILE(S)', Array, 'YAML file(s) to populate local variables for the template. separated by comma' do |configs|
46+
configs.each do |config|
47+
if File.directory?(config)
48+
Dir.foreach(config) do |yaml|
49+
next if yaml == '.' or yaml == '..'
50+
File.open File.join(config, yaml), 'r' do |f|
51+
options.yamls.push(YAML::load(f))
52+
end
53+
end
54+
else
55+
File.open config, 'r' do |f|
56+
options.yamls.push(YAML::load(f))
57+
end
58+
end
4759
end
4860
end
4961
end
@@ -53,6 +65,16 @@ def main
5365
exit 1
5466
end
5567

68+
options.yamls.each_with_index do |y, i|
69+
if !y.nil? && y.inspect != 'false'
70+
if options.yaml.nil?
71+
options.yaml = y
72+
else
73+
options.yaml = options.yaml.merge y
74+
end
75+
end
76+
end
77+
5678
file_or_stdin args do |input|
5779
puts ERBContext.render(options.yaml || {}, input.read, nil, '-')
5880
end

tools/print_key.rb

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,24 @@ def key_or_stdin(args, stdin = ::STDIN)
1313

1414
def main
1515
options = OpenStruct.new
16+
options.yamls = []
1617

1718
parser = OptionParser.new do |opts|
18-
opts.banner = 'Usage: %s [options] key' % $0
19-
opts.on '-y', '--yaml=YAML-FILE', 'YAML file to read the key from' do |yaml_file|
20-
File.open yaml_file, 'r' do |f|
21-
options.yaml = YAML::load(f)
19+
opts.banner = 'Usage: %s [options] file.erb' % $0
20+
opts.on '-y', '--yaml=YAML-FILE(S)', Array, 'YAML file(s) to populate local variables for the template. separated by comma' do |configs|
21+
configs.each do |config|
22+
if File.directory?(config)
23+
Dir.foreach(config) do |yaml|
24+
next if yaml == '.' or yaml == '..'
25+
File.open File.join(config, yaml), 'r' do |f|
26+
options.yamls.push(YAML::load(f))
27+
end
28+
end
29+
else
30+
File.open config, 'r' do |f|
31+
options.yamls.push(YAML::load(f))
32+
end
33+
end
2234
end
2335
end
2436
end
@@ -28,6 +40,16 @@ def main
2840
exit 1
2941
end
3042

43+
options.yamls.each_with_index do |y, i|
44+
if !y.nil? && y.inspect != 'false'
45+
if options.yaml.nil?
46+
options.yaml = y
47+
else
48+
options.yaml = options.yaml.merge y
49+
end
50+
end
51+
end
52+
3153
key_or_stdin args do |input|
3254
content = options.yaml
3355
input.split(".").each do |part|

0 commit comments

Comments
 (0)