summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorChris Price <chris@pupppetlabs.com>2012-08-17 04:48:28 -0700
committerChris Price <chris@pupppetlabs.com>2012-08-17 04:48:28 -0700
commitc57dab4903a6a23fb14dc79c76efea989e694460 (patch)
tree6f8ef871f02465d21b231e931eba03739422d277 /lib
parent4f0e7264e3c3089e489d05bbb4371c449b0ed78d (diff)
downloadpuppet-inifile-c57dab4903a6a23fb14dc79c76efea989e694460.tar.gz
puppet-inifile-c57dab4903a6a23fb14dc79c76efea989e694460.tar.bz2
Add support for "global" section at beginning of file
This commit does the following: * Fixes a bug in ExternalIterator * Adds support for a "global" section before the first named section at the beginning of the INI file * Improves test coverage
Diffstat (limited to 'lib')
-rw-r--r--lib/puppet/util/external_iterator.rb8
-rw-r--r--lib/puppet/util/ini_file.rb21
2 files changed, 18 insertions, 11 deletions
diff --git a/lib/puppet/util/external_iterator.rb b/lib/puppet/util/external_iterator.rb
index 67b3375..45c0fa4 100644
--- a/lib/puppet/util/external_iterator.rb
+++ b/lib/puppet/util/external_iterator.rb
@@ -3,7 +3,7 @@ module Util
class ExternalIterator
def initialize(coll)
@coll = coll
- @cur_index = 0
+ @cur_index = -1
end
def next
@@ -17,7 +17,11 @@ module Util
private
def item_at(index)
- [@coll[index], index]
+ if @coll.length > index
+ [@coll[index], index]
+ else
+ [nil, nil]
+ end
end
end
end
diff --git a/lib/puppet/util/ini_file.rb b/lib/puppet/util/ini_file.rb
index 9fd08ad..8629db0 100644
--- a/lib/puppet/util/ini_file.rb
+++ b/lib/puppet/util/ini_file.rb
@@ -43,18 +43,13 @@ module Util
def save
File.open(@path, 'w') do |fh|
- first_section = @sections_hash[@section_names[0]]
- first_section.start_line == nil ? start_line = 0 : start_line = first_section.start_line
- (0..start_line - 1).each do |line_num|
- fh.puts(lines[line_num])
- end
@section_names.each do |name|
section = @sections_hash[name]
- if (section.start_line.nil?)
+ if section.start_line.nil?
fh.puts("\n[#{section.name}]")
- else
+ elsif ! section.end_line.nil?
(section.start_line..section.end_line).each do |line_num|
fh.puts(lines[line_num])
end
@@ -76,7 +71,13 @@ module Util
def parse_file
line_iter = create_line_iter
+
+ # We always create a "global" section at the beginning of the file, for
+ # anything that appears before the first named section.
+ section = read_section('', 0, line_iter)
+ add_section(section)
line, line_num = line_iter.next
+
while line
if (match = SECTION_REGEX.match(line))
section = read_section(match[1], line_num, line_iter)
@@ -88,13 +89,15 @@ module Util
def read_section(name, start_line, line_iter)
settings = {}
+ end_line_num = nil
while true
line, line_num = line_iter.peek
- if (line.nil? or match = SECTION_REGEX.match(line))
- return Section.new(name, start_line, line_num - 1, settings)
+ if (line_num.nil? or match = SECTION_REGEX.match(line))
+ return Section.new(name, start_line, end_line_num, settings)
elsif (match = SETTING_REGEX.match(line))
settings[match[1]] = match[2]
end
+ end_line_num = line_num
line_iter.next
end
end