class KubeDeployTools::Templater

Public Instance Methods

render_erb_with_hash(template, values) click to toggle source
# File lib/kube_deploy_tools/templater.rb, line 16
def render_erb_with_hash(template, values)
  begin
    renderer = ERB.new(File.read(template), nil, '-')
    config = StrictHash.new(values)
    renderer.result(binding)
  rescue Exception => e
    raise "Error rendering template #{template} with #{config}: #{e}"
  end
end
template(template, values) click to toggle source
# File lib/kube_deploy_tools/templater.rb, line 10
def template(template, values)
  output = render_erb_with_hash template, values

  output
end
template_to_file(template, values, maybeOutputFilepath = nil) click to toggle source
# File lib/kube_deploy_tools/templater.rb, line 26
def template_to_file(template, values, maybeOutputFilepath = nil)
  raise "Expected template to be an existing file, received '#{template}'" unless File.file?(template)
  raise "Expected output filepath to be a new filepath, received '#{maybeOutputFilepath}'" if maybeOutputFilepath.present? && (File.file?(maybeOutputFilepath) || File.directory?(maybeOutputFilepath))

  output = template(template, values)

  # If an output filepath is not given, print to stdout
  if !maybeOutputFilepath
    $stdout.puts output
  elsif output.present?
    # Save file if output is not blank. This will suppress output file
    # generation when using ERB early returns at the top of an ERB template:
    # <% return if ... %>
    FileUtils.mkdir_p(File.dirname(maybeOutputFilepath))
    File.open(maybeOutputFilepath, "w") { |f| f << output }
  end
end