"...lib/puppet/stdlib/lib/puppet/functions/sprintf_hash.rb" did not exist on "1042df79ab6e547769d2f35b7f58142943690f3c"
sprintf_hash.rb 1.38 KB
Newer Older
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
# frozen_string_literal: true

# @summary
#  Uses sprintf with named references.
#
# The first parameter is format string describing how the rest of the parameters in the hash
# should be formatted. See the documentation for the `Kernel::sprintf` function in Ruby for
# all the details.
#
# In the given argument hash with parameters, all keys are converted to symbols so they work
# with the `sprintf` function.
#
# @example Format a string and number
#   $output = sprintf_hash('String: %<foo>s / number converted to binary: %<number>b',
#                          { 'foo' => 'a string', 'number' => 5 })
#   # $output = 'String: a string / number converted to binary: 101'
#
# Note that since Puppet 4.10.10, and 5.3.4 this functionality is supported by the
# `sprintf` function in puppet core.
#
Puppet::Functions.create_function(:sprintf_hash) do
  # @param format The format to use.
  # @param arguments Hash with parameters.
  # @return The formatted string.
  dispatch :sprintf_hash do
    param 'String', :format
    param 'Hash', :arguments
    # Disabled for now. This gives issues on puppet 4.7.1.
    # return_type 'String'
  end

  def sprintf_hash(format, arguments)
    call_function('deprecation', 'sprintf_hash', 'This method is deprecated. From Puppet 4.10.10/5.3.4 please use the built-in sprintf instead')

    Kernel.sprintf(format, Hash[arguments.map { |(k, v)| [k.to_sym, v] }])
  end
end