Ruby: lightweight templating
I had a TON of configuration files that I needed to deal with at work. After writing myself a handy tool to produce the “input” configuration details, I figured I'd just use some quick and dirty Ruby (and ERB
) to write the necessary files:
#!/usr/bin/env ruby
require 'json'
require 'erb'
template_file = "./template.toml.erb"
output_file_name = "./configuration/foo/conf-%{id_token}.toml"
# given a JSON array through STDIN of [ {conf}, {conf} ]
confs = JSON.parse($stdin.read, symbolize_names: true).map{|c| OpenStruct.new c }
# we can prepare the ERB template
template = ERB.new(File.read(template_file), 0, "%-"))
# finally, write out files for the conf objects
confs.each do |c|
result = template.result(c.instance_eval("binding"))
File.write(output_file_name % c, result)
end