Search code examples
rubypuppeterberuby

indentation ERB file


I have the following code:

if (!EAP-Message) {
    # Now check against the authorized_macs file
    <%- @filename.each do |key,value| -%>
    <%= key %>
    if (!ok) {
    <%- end -%>
                        eap
                    }
                    else {
                        # accept
                        update control {
                            Auth-Type := Accept
                        }
                    }
                }
                else {
                    # accept
                    update control {
                        Auth-Type := Accept
                    }   
                }
            }
            else {
                # accept
                update control {
                    Auth-Type := Accept
                }
            }
        }
        else {
            # accept
            update control {
                Auth-Type := Accept
            }
        }
    }
    else {
        # accept
        update control {
            Auth-Type := Accept
        }
    }
}
else {
    eap
}

I would like to indent the file properly by adding indentation or spaces or tabs after each loop, so that it looks like this:

if (!EAP-Message) {
    # Now check against the authorized_macs file
    authorized_macs_10
    if (!ok) {
        authorized_macs_11
        if (!ok) {
            authorized_macs_12
            if (!ok) {
                authorized_macs_13
                if (!ok) {
                    authorized_macs_14
                    if (!ok) {
                        eap
                    }
                    else {
                        # accept
                        update control {
                            Auth-Type := Accept
                        }
                    }
                }
                else {
                    # accept
                    update control {
                        Auth-Type := Accept
                    }
                }
            }
            else {
                # accept
                update control {
                    Auth-Type := Accept
                }
            }
        }
        else {
            # accept
            update control {
                Auth-Type := Accept
            }
        }
    }
    else {
        # accept
        update control {
            Auth-Type := Accept
        }
    }
}
else {
    eap
}

This is a part of the /etc/freeradius/sites-available/default of my freeradius puppet module, which is finally done. The @filename variable is a hiera_hash, which I get from a yaml file. Here is the relevant part:

test_freeradius::files:
'authorized_macs_10':
    macs: 
        - 'aaaaa'
        - 'bbbbb'
    vlan: '10' 
'authorized_macs_11':
    macs: 
        - 'ccccc' 
        - 'ddddd'  
    vlan: '11'
'authorized_macs_12':
    macs: 
        - 'eeeee'
        - 'fffff'
    vlan: '12'
'authorized_macs_13':
    macs:
        - 'ggggg'
        - 'hhhhh'
    vlan: '13'
'authorized_macs_14':
    macs:
        - 'iiiii'
        - 'jjjjj'
    vlan: '14'

And below is the define I use to create among other things the content of the default file:

define test_freeradius::files (

  $macs,
  $vlan,
  $macfile  = $title,

) {

  include test_freeradius::service

  ensure_resource('file', '/etc/freeradius/modules/files', {
      ensure  => 'file',
      owner   => 'root',
      group   => 'root',
      mode    => '0644',
      content => template("${module_name}/files.erb"),
      require => Class['test_freeradius::install'],
      notify  => Service['freeradius'],
    }
  )

  file { "/etc/freeradius/${macfile}" :
    ensure  => 'file',
    owner   => 'root',
    group   => 'root',
    mode    => '0644',
    content => template("${module_name}/macfiles.erb"),
    require => Class['test_freeradius::install'],
    notify  => Service['freeradius'],
  }

  ensure_resource('file', '/etc/freeradius/sites-available/default', {
      ensure  => 'file',
      owner   => 'root',
      group   => 'freerad',
      mode    => '0644',
      content => template("${module_name}/default.erb"),
      require => Class['test_freeradius::install'],
      notify  => Service['freeradius'],
    }
  )

  ensure_resource('file', '/etc/freeradius/sites-enabled/default', {
      ensure => 'link',
      target => '/etc/freeradius/sites-available/default',
    }
  )
}

The loop does what it should, but without indentation, I tried different things like adding <%= " "%> at several positions and creating a variable for the if (!ok) { part, unfortunately without success, maybe I should restructure the code? any ideas?


Solution

  • You ought to be able to generate appropriate indentation via Ruby scriptlet code by building up an indentation string as you iterate. For example,

    if (!EAP-Message) {
        # Now check against the authorized_macs file
        <%- indent = '' -%>
        <%- @filename.each do |key,value| -%>
        <%= indent %><%= key %>
        <%= indent %>if (!ok) {
        <%- indent += '    ' -%>
        <%- end -%>
                            eap
        ...
    

    It seems to me, though, that you might have another problem. Can you be confident that the @filenames hash will always contain exactly five entries? If it contains some other number of entries then your template will produce malformed output, because there will be either too many or too few block closures.

    You could address that by using an approach similar to the one I already described to build up the block closures in a string as you iterate, and then outputting the string at the appropriate point (instead of using template text to close the blocks).