Puppet Function: bind::gencfg
- Defined in:
- functions/gencfg.pp
- Function type:
- Puppet Language
Summary
Generate a configuration snippet from a hashOverview
Generate a Bind configuration snippet from a hash. Each key of the hash is used as a config option. The can be a string, a numeric value, a boolean or an array or hash. the config option is terminated with a ‘;’ char.
Strings and numeric values are included normally as parameter value. A boolean value is written as ‘yes’ or ‘no’. Arrays are enclosed in braces and array values are returned as a single line if the number of elements is zero or one. Otherwise a new line is used for each value. Hashes are also enclosed in braces and the keys and values are processed recursively.
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'functions/gencfg.pp', line 75
function bind::gencfg(Hash[String,Data] $config, Integer $indent = 0) >> String {
# base indentation
$space = sprintf('%*s', $indent, '')
# use string length of longest hash key for alignment
$align = $config.reduce(0) |$memo, $item| { max($memo, $item[0].length) }
$config.reduce('') |$memo, $item| {
case $item[1] {
Boolean: {
# Convert boolean values to 'yes' or 'no'
sprintf("%s%s%*s %s;\n", $memo, $space, -$align, $item[0], bool2str($item[1], 'yes', 'no'))
}
Array: {
# If the array has zero or one element, then the result is written on
# the same line as the option. Otherwise a new line is used for every
# array element and the array elements are indented by two additional
# spaces.
if ($item[1].length <= 1) {
$param = $item[1].reduce(' ') |$memo, $elt| {
"${memo}${elt}; "
}
}
else {
$param = $item[1].reduce("\n${space}") |$memo, $elt| {
"${memo} ${elt};\n${space}"
}
}
sprintf("%s%s%*s {%s};\n", $memo, $space, -$align, $item[0], $param)
}
Hash: {
# Hash parameters are generated recursively using more indentation.
$param = bind::gencfg($item[1], $indent + 2)
sprintf("%s%s%*s {\n%s%s};\n", $memo, $space, -$align, $item[0], $param, $space)
}
default: {
sprintf("%s%s%*s %s;\n", $memo, $space, -$align, $item[0], String($item[1]))
}
}
}
}
|