From df7ab8d5277331e2973604f1fd70f0308709b937 Mon Sep 17 00:00:00 2001 From: Pat Riehecky Date: Mon, 23 Nov 2020 10:54:18 -0600 Subject: [PATCH] (FEAT) Add simple standardized header template --- README.md | 29 +++++++++++++++++++++++++++++ templates/puppet_managed.epp | 20 ++++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 templates/puppet_managed.epp diff --git a/README.md b/README.md index f5582df..8669f6e 100644 --- a/README.md +++ b/README.md @@ -63,6 +63,35 @@ The following sections/settings are included. } ``` +### EPP Templates + +You can include a standardized comment header in your `.epp` or `.erb` templates with the following: +```puppet +<%= scope.call_function('epp', ["extlib/puppet_managed.epp"]) %> +``` +The EPP template takes the following optional parameters: +- String $message = 'WARNING This file is managed by puppet. Do not edit!', +- String $begin_line = '#', +- String $end_line = '', +- Int $line_length = 70, +- String $metadata_title = 'Metadata:', +- Hash $metadata = {} +Example: +```puppet +<%= scope.call_function('epp', ["extlib/puppet_managed.epp"], {'metadata' => {'fqdn' => $::fqdn}}) %> +``` +Should produce +```shell +# +# +# WARNING This file is managed by puppet. Do not edit! +# +# Metadata: +# fqdn=hostname.example.com +# +# +``` + ## Limitations Some functions require puppetlabs-stdlib (>= 4.6.0) and all functions are only diff --git a/templates/puppet_managed.epp b/templates/puppet_managed.epp new file mode 100644 index 0000000..1eb15a4 --- /dev/null +++ b/templates/puppet_managed.epp @@ -0,0 +1,20 @@ +<%- | String $message = 'WARNING This file is managed by puppet. Do not edit!', + String $begin_line = '#', + String $end_line = '', + Int $line_length = 70, + String $metadata_title = 'Metadata:', + Hash $metadata = {} +| -%> +<%- $comment_length = $begin_line.length() + $end_line.length() -%> +<%= $begin_line %><% if $end_line != '' { %><% $tmp = $comment_length %><% loop do %><% if $tmp >= $line_length { break } %> <% $tmp = $tmp + 1 %><% end %><%= $end_line %><% } %> +<%= $begin_line %><% if $end_line != '' { %><% $tmp = $comment_length %><% loop do %><% if $tmp >= $line_length { break } %> <% $tmp = $tmp + 1 %><% end %><%= $end_line %><% } %> +<%= $begin_line %> <% $message %><% if $end_line != '' { %><% $tmp = $comment_length + message.length() + 1%><% loop do %><% if $tmp >= $line_length { break } %> <% $tmp = $tmp + 1 %><% end %><%= $end_line %><% } %> +<% if $metadata { -%> +<%= $begin_line %><% if $end_line != '' { %><% $tmp = $comment_length %><% loop do %><% if $tmp >= $line_length { break } %> <% $tmp = $tmp + 1 %><% end %><%= $end_line %><% } %> +<%= $begin_line %> <%= $metadata_title %><% if $end_line != '' { %><% $tmp = $comment_length + $metadata_title.length() + 1 %><% loop do %><% if $tmp >= $line_length { break } %> <% $tmp = $tmp + 1 %><% end %><%= $end_line %><% } %> +<% $metadata.keys.each |$key| { -%> +<%= $begin_line %> <% $key %>=<%= $metadata[$key] %><% if $end_line != '' { %><% $tmp = $comment_length + $key.length() $metadata[$key].length() + 3 %><% loop do %><% if $tmp >= $line_length { break } %> <% $tmp = $tmp + 1 %><% end %><%= $end_line %><% } %> +<% } -%> +<% } -%> +<%= $begin_line %><% if $end_line != '' { %><% $tmp = $comment_length %><% loop do %><% if $tmp >= $line_length { break } %> <% $tmp = $tmp + 1 %><% end %><%= $end_line %><% } %> +<%= $begin_line %><% if $end_line != '' { %><% $tmp = $comment_length %><% loop do %><% if $tmp >= $line_length { break } %> <% $tmp = $tmp + 1 %><% end %><%= $end_line %><% } %>