Class: KubeDeployTools::Templater

Inherits:
Object
  • Object
show all
Defined in:
lib/kube_deploy_tools/templater.rb

Defined Under Namespace

Classes: Optparser

Instance Method Summary collapse

Instance Method Details

#render_erb_with_hash(template, values) ⇒ Object



16
17
18
19
20
21
22
23
24
# 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) ⇒ Object



10
11
12
13
14
# 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) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# 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