Skip to content

Commit 0a6ac2a

Browse files
author
Martin Roy
committed
Initial commit
1 parent ca5a61e commit 0a6ac2a

File tree

9 files changed

+157
-0
lines changed

9 files changed

+157
-0
lines changed

.travis.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
language: bash
2+
3+
script:
4+
- bash test.sh

Dockerfile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
FROM ruby:latest AS config-builder
2+
3+
RUN mkdir /workspace/
4+
WORKDIR /workspace/
5+
6+
COPY ./tools/* ./

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,21 @@
11
# ruby-yaml-erb-tools
2+
23
Tool set for parsing YAML and ERB files using ruby
4+
5+
## config.rb
6+
7+
`Usage: config.rb --yaml config.yaml example.txt.erb`
8+
9+
will output the parsed template using the yaml file
10+
11+
## print_key.rb
12+
13+
`Usage: print_key.rb --yaml config.yaml parent.child`
14+
15+
will output the parsed value at the given path
16+
17+
# Usage in dockerfiles
18+
19+
You might need to build a container using templates... try multi stage build.
20+
21+
See `test/Dockerfile` for an example

test.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/bin/env bash -eux
2+
3+
docker build --tag ruby-yaml-erb-tools-test .
4+
5+
(cd test && docker build --tag processor .)
6+
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')" ]]
9+
10+
echo Test Pass!

test/Dockerfile

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
FROM ruby-yaml-erb-tools-test AS builder
2+
3+
COPY config.yaml example.txt.erb ./
4+
5+
RUN mkdir -p output/
6+
7+
RUN /bin/bash -c "./config.rb --yaml config.yaml example.txt.erb > output/example.txt";
8+
9+
RUN /bin/bash -c "./print_key.rb --yaml config.yaml parent.child > output/key.txt";
10+
11+
12+
FROM alpine
13+
14+
COPY --from=builder /workspace/output/* /processed/

test/config.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
parent:
2+
child: value

test/example.txt.erb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is the <%= @parent['child'] %>

tools/config.rb

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#!/usr/bin/env ruby
2+
require 'erb'
3+
require 'json'
4+
require 'yaml'
5+
require 'optparse'
6+
require 'ostruct'
7+
8+
class ERBContext
9+
def initialize(hash)
10+
raise ArgumentError, 'hash must be a Hash object' unless hash.is_a?(::Hash)
11+
hash.each do |key, value|
12+
instance_variable_set :"@#{key}", value
13+
end
14+
end
15+
16+
def render(template)
17+
template.result binding
18+
end
19+
20+
class << self
21+
def render(hash, template, safe_level = nil, trim_mode = nil, eoutvar = '_erbout')
22+
tmpl = ::ERB.new(template, safe_level, trim_mode, eoutvar)
23+
context = new(hash)
24+
context.render tmpl
25+
end
26+
end
27+
end
28+
29+
def file_or_stdin(args, stdin = ::STDIN)
30+
if args.empty? || args.first == '-'
31+
yield stdin
32+
else
33+
File.open args.first, 'r' do |f|
34+
yield f
35+
end
36+
end
37+
end
38+
39+
def main
40+
options = OpenStruct.new
41+
42+
parser = OptionParser.new do |opts|
43+
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)
47+
end
48+
end
49+
end
50+
51+
if (args = parser.parse(ARGV)).length > 1
52+
STDERR.puts '%s: cannot render more than 1 file at a time!' % $0
53+
exit 1
54+
end
55+
56+
file_or_stdin args do |input|
57+
puts ERBContext.render(options.yaml || {}, input.read, nil, '-')
58+
end
59+
end
60+
61+
main if __FILE__ == $0

tools/print_key.rb

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/usr/bin/env ruby
2+
require 'yaml'
3+
require 'optparse'
4+
require 'ostruct'
5+
6+
def key_or_stdin(args, stdin = ::STDIN)
7+
if args.empty? || args.first == '-'
8+
yield stdin
9+
else
10+
yield args.first
11+
end
12+
end
13+
14+
def main
15+
options = OpenStruct.new
16+
17+
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)
22+
end
23+
end
24+
end
25+
26+
if (args = parser.parse(ARGV)).length > 1
27+
STDERR.puts '%s: cannot render more than 1 key at a time!' % $0
28+
exit 1
29+
end
30+
31+
key_or_stdin args do |input|
32+
content = options.yaml
33+
input.split(".").each do |part|
34+
content = content[part]
35+
end
36+
puts content
37+
end
38+
end
39+
40+
main if __FILE__ == $0

0 commit comments

Comments
 (0)