Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(FEAT) Add simple standardized header template #171

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,35 @@ The following sections/settings are included.
}
```

### EPP Templates
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer to see a native puppet function that takes all these parameters. Something like

extlib::puppet_managed(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 = {} ) >> String {

}

# process content here and break into multiple lines

Then in the template it would be

<%= extlib::puppet_managed() %>


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
Expand Down
20 changes: 20 additions & 0 deletions templates/puppet_managed.epp
Original file line number Diff line number Diff line change
@@ -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 = {}
| -%>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is complex and too many tags to understand. Would prefer to have the logic in the function.

<%- $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 %><% } %>