]> gitweb.fluxo.info Git - puppet-inifile.git/commitdiff
Better handling of whitespace lines at ends of sections
authorChris Price <chris@puppetlabs.com>
Sat, 20 Oct 2012 07:24:37 +0000 (00:24 -0700)
committerChris Price <chris@puppetlabs.com>
Sat, 20 Oct 2012 08:43:19 +0000 (01:43 -0700)
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
spec/unit/puppet/provider/ini_setting/ruby_spec.rb

index af9bc0e57a7096649b7f7af89d934edbc23f1caa..6038b6d2b4cc71194c7becf4f2c08e1ccf54ac9d 100644 (file)
@@ -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
index dc3b7cb2b2964b268c979fe1af36b474804fb10a..19db4c7194dae116bf72d1b1429555d6aad0c8f4 100644 (file)
@@ -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