Skip to content

Commit

Permalink
Better handling of whitespace lines at ends of sections
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
cprice404 committed Oct 20, 2012
1 parent c2c26de commit 845fa70
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
47 changes: 45 additions & 2 deletions lib/puppet/util/ini_file.rb
Expand Up @@ -64,21 +64,57 @@ def remove_setting(section_name, setting)
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
Expand Down Expand Up @@ -176,6 +212,13 @@ def decrement_section_line_numbers(section_index)
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
3 changes: 2 additions & 1 deletion spec/unit/puppet/provider/ini_setting/ruby_spec.rb
Expand Up @@ -361,6 +361,7 @@ def validate_file(expected_content,tmpfile = tmpfile)
provider.create
validate_file(<<-EOS
foo = yippee
[section2]
foo = http://192.168.1.1:8080
EOS
Expand Down Expand Up @@ -571,8 +572,8 @@ def validate_file(expected_content,tmpfile = tmpfile)
bar = barvalue
master = true
yahoo = yippee
[section2]
foo= foovalue2
baz=bazvalue
Expand Down

0 comments on commit 845fa70

Please sign in to comment.