From 845fa707be7132e753f291901dd7e4d4dc48c405 Mon Sep 17 00:00:00 2001 From: Chris Price Date: Sat, 20 Oct 2012 00:24:37 -0700 Subject: Better handling of whitespace lines at ends of sections This is another bit of cosmetic functionality; prior to this commit, when adding a new setting to a section, we'd write it on the very last line before the next section, even if there was a chunk of trailing whitespace lines at the end of the existing section. This was functional, but the output was not always particularly pleasant for human consumption. This commit tweaks things so that we insert new settings just before the final chunk of whitespace lines in an existing section. This keeps things a bit cleaner. --- lib/puppet/util/ini_file.rb | 47 +++++++++++++++++++++- spec/unit/puppet/provider/ini_setting/ruby_spec.rb | 3 +- 2 files changed, 47 insertions(+), 3 deletions(-) diff --git a/lib/puppet/util/ini_file.rb b/lib/puppet/util/ini_file.rb index af9bc0e..6038b6d 100644 --- a/lib/puppet/util/ini_file.rb +++ b/lib/puppet/util/ini_file.rb @@ -64,21 +64,57 @@ module Util def save File.open(@path, 'w') do |fh| - @section_names.each do |name| + @section_names.each_index do |index| + name = @section_names[index] section = @sections_hash[name] + # We need a buffer to cache lines that are only whitespace + whitespace_buffer = [] + if section.start_line.nil? fh.puts("\n[#{section.name}]") elsif ! section.end_line.nil? (section.start_line..section.end_line).each do |line_num| - fh.puts(lines[line_num]) + line = lines[line_num] + + # We buffer any lines that are only whitespace so that + # if they are at the end of a section, we can insert + # any new settings *before* the final chunk of whitespace + # lines. + if (line =~ /^\s*$/) + whitespace_buffer << line + else + # If we get here, we've found a non-whitespace line. + # We'll flush any cached whitespace lines before we + # write it. + flush_buffer_to_file(whitespace_buffer, fh) + fh.puts(line) + end end end section.additional_settings.each_pair do |key, value| fh.puts("#{' ' * (section.indentation || 0)}#{key}#{@key_val_separator}#{value}") end + + if (whitespace_buffer.length > 0) + flush_buffer_to_file(whitespace_buffer, fh) + else + # We get here if there were no blank lines at the end of the + # section. + # + # If we are adding a new section with a new setting, + # and if there are more sections that come after this one, + # we'll write one blank line just so that there is a little + # whitespace between the sections. + if (section.end_line.nil? && + (section.additional_settings.length > 0) && + (index < @section_names.length - 1)) + fh.puts("") + end + end + end end end @@ -176,6 +212,13 @@ module Util end end + def flush_buffer_to_file(buffer, fh) + if buffer.length > 0 + buffer.each { |l| fh.puts(l) } + buffer.clear + end + end + end end end diff --git a/spec/unit/puppet/provider/ini_setting/ruby_spec.rb b/spec/unit/puppet/provider/ini_setting/ruby_spec.rb index dc3b7cb..19db4c7 100644 --- a/spec/unit/puppet/provider/ini_setting/ruby_spec.rb +++ b/spec/unit/puppet/provider/ini_setting/ruby_spec.rb @@ -361,6 +361,7 @@ foo = http://192.168.1.1:8080 provider.create validate_file(<<-EOS foo = yippee + [section2] foo = http://192.168.1.1:8080 EOS @@ -571,8 +572,8 @@ subby=bar bar = barvalue master = true - yahoo = yippee + [section2] foo= foovalue2 baz=bazvalue -- cgit v1.2.3