From 31d202e22ddb7c9b37e1b8f2557faba807d61c57 Mon Sep 17 00:00:00 2001 From: Thomas Bellman <bellman@nsc.liu.se> Date: Fri, 19 Jul 2024 03:16:25 +0200 Subject: [PATCH] ini_file: Align = signs in generated file. To make the file generated by cfgfile::ini_file prettier and easier to read, align the equal signs for settings within each section. I.e, generate a file looking like [section-1] foo = 10 gazonk = 20 [section-2] sarsaparilla = 4711 smurf = 90000 We limit how far out we align the equal signs, though, as otherwise a single very long setting name would just make things more difficult to read. Thus, we will generate e.g. [section] foo = 1 gazonk = 2 setting_name_that_is_way_too_long = 3 zarzaparilla = 4 The maximum alignment column is hardcoded to 24. --- manifests/ini_file.pp | 13 +++---------- templates/ini_file.erb | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+), 10 deletions(-) create mode 100644 templates/ini_file.erb diff --git a/manifests/ini_file.pp b/manifests/ini_file.pp index c2cdb3d..0b0415a 100644 --- a/manifests/ini_file.pp +++ b/manifests/ini_file.pp @@ -1,4 +1,4 @@ -# Copyright (C) 2014-2020 Thomas Bellman. +# Copyright (C) 2014-2024 Thomas Bellman. # Licensed under the GNU LGPL v3+; see the README file for more information. @@ -37,17 +37,10 @@ define cfgfile::ini_file($settings, $ensure='present', { if ($ensure == 'present') { - $content = inline_template( - '<%= @settings.sort.collect { |section_name,section_vars| - "[" + section_name + "]\n" + - section_vars.sort.collect { |k,v| - k + " = " + v.to_s - }.join("\n") - }.join("\n\n") + "\n" - %>') file { $name: - ensure => file, content => $content, + ensure => file, + content => template('cfgfile/ini_file.erb'), owner => $owner, group => $group, mode => $mode; } } diff --git a/templates/ini_file.erb b/templates/ini_file.erb new file mode 100644 index 0000000..59b9019 --- /dev/null +++ b/templates/ini_file.erb @@ -0,0 +1,19 @@ +<% + # Copyright (C) 2024 Thomas Bellman. + # Licensed under the GNU LGPL v3+; see the README file for more information. + # +-%> +<% @settings.sort().each do |section_name, section_vars| -%> +[<%= section_name %>] +<% # Want to align = signs, but only for "short" names + max_align_pos = 24 + shortnames = section_vars.keys().select { |name| + name.length() < max_align_pos - 1 + } + namewidth = ( shortnames.max_by { |name| name.length() } or "" ).length() + section_vars.sort.each do |key,value| +-%> +<%= sprintf('%-*s = %s', namewidth, key, value.to_s).rstrip() %> +<% end -%> + +<% end -%> -- GitLab