File: default_content.rb

package info (click to toggle)
puppet-module-extlib 7.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 420 kB
  • sloc: ruby: 1,035; sh: 15; makefile: 10
file content (40 lines) | stat: -rw-r--r-- 1,312 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# frozen_string_literal: true

# Takes an optional content and an optional template name and returns the contents of a file.
Puppet::Functions.create_function(:'extlib::default_content') do
  # @param content
  # @param template_name
  #   The path to an .erb or .epp template file or `undef`.
  # @return
  #   Returns the value of the content parameter if it's a non empty string.
  #   Otherwise returns the rendered output from `template_name`.
  #   Returns `undef` if both `content` and `template_name` are `undef`.
  #
  # @example Using the function with a file resource.
  #   $config_file_content = default_content($file_content, $template_location)
  #   file { '/tmp/x':
  #     ensure  => 'file',
  #     content => $config_file_content,
  #   }
  dispatch :default_content do
    param 'Optional[String]', :content
    param 'Optional[String]', :template_name
    return_type 'Optional[String]'
  end

  def emptyish(param)
    param.nil? || param.empty? || param == :undef
  end

  def default_content(content = :undef, template_name = :undef)
    return content unless emptyish(content)

    unless emptyish(template_name)
      return call_function('template', template_name) unless template_name.end_with?('.epp')

      return call_function('epp', template_name)
    end

    :undef
  end
end