0

I'm unable to fix indentation for ruby template, I've spent hours on this and still couldn't get it to work as expected.

Here is the template:

<%= @log_path -%> {
  <%= @rotate_every %>
  rotate <%= @rotate_count %>
  compress
  <% if @delaycompress == true %>delaycompress
  <% end -%>missingok
  notifempty
  create <%= @create_mode -%> <%= @create_owner -%> <%= @create_group %>
  <% if @postrotate == true -%>postrotate
    <% @postrotate_cmd.each do |value| -%><%= value %>
  <% end -%>endscript<% end %>
}

When @postrotate is false the output looks like this. The } is after a empty line.

/var/log/auth.log {
  weekly
  rotate 4
  compress
  delaycompress
  missingok
  notifempty
  create 0644 root adm
  
  }

When @postrotate is true the output looks like this. The second invoke commnad should be a bit to the right.

/var/log/auth.log {
  weekly
  rotate 4
  compress
  delaycompress
  missingok
  notifempty
  create 0644 root adm
  postrotate
    invoke-rc.d rsyslog reload > /dev/null
  invoke-rc.d rsyslog reload > /dev/null
  endscript
  }

Expected output when @postrotate is false:

/var/log/auth.log {
  weekly
  rotate 4
  compress
  delaycompress
  missingok
  notifempty
  create 0644 root adm
}

Expected output when @postrotate is true:

/var/log/auth.log {
  weekly
  rotate 4
  compress
  delaycompress
  missingok
  notifempty
  create 0644 root adm
  postrotate
    invoke-rc.d rsyslog reload > /dev/null
    invoke-rc.d rsyslog reload > /dev/null
  endscript
}
Matthew Schuchard
  • 25,172
  • 3
  • 47
  • 67
user630702
  • 2,529
  • 5
  • 35
  • 98
  • The reason for the blank line in your `false` conditional is because your `end` for the `if` conditional is missing the whitespace/newline terminator. The lack of indentation for `true` I am not completely sure about. You may want to properly format the code in your template and see if that helps. – Matthew Schuchard Jul 07 '20 at 11:14

1 Answers1

1

You seem to have incorrect expectations about the meaning of the -%> end-of-tag token. If you close a tag with -%> and it is the last non-whitespace on that line of the template, then any trailing whitespace and the newline following it are consumed instead of being copied to the output. But that does nothing for you where the tag closed with -%> is not the last non-whitespace on the line, which is often the case in your template.

You also seem not to know about the <%- start-of-tag token, which has a similar effect on any leading indentation of a line where a tag starting with that token is the first non-whitespace. There are several places where using that could help you make your template easier to read.

Overall, I might write your template more like this:

<%= @log_path %> {
  <%= @rotate_every %>
  rotate <%= @rotate_count %>
  compress
  <%- if @delaycompress -%>
  delaycompress
  <%- end -%>
  missingok
  notifempty
  create <%= @create_mode %> <%= @create_owner %> <%= @create_group %>
  <%- if @postrotate -%>
  postrotate
    <%- @postrotate_cmd.each do |value| -%>
    <%= value %>
    <%- end -%>
  endscript
  <%- end -%>
}

In particular, note that lines that contain nothing but whitespace and a non-printing tag that starts with <%- and ends with -%> produce no output at all. Using that to separate flow-control statements from template text and output tags makes the template clearer and easier to read, and it will also ensure that your loop body is consistently indented. Furthermore, it may help you see and very likely will help you debug issues with leaving off the - where you wanted it, as your original template does in its last <% end %> tag.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157