]> gitweb.fluxo.info Git - puppet-inifile.git/commitdiff
Allow overriding separator string between key/val pairs
authorChris Price <chris@puppetlabs.com>
Wed, 19 Sep 2012 22:42:16 +0000 (15:42 -0700)
committerChris Price <chris@puppetlabs.com>
Wed, 19 Sep 2012 22:42:16 +0000 (15:42 -0700)
This introduces a new parameter, 'key_val_separator', which
can be set in order to override the string that is used
as a separator between the key/value pair of a setting line.
The default is ' = ', but you could set the param to '=' if
you don't want to include whitespace in your settings file.

lib/puppet/provider/ini_setting/ruby.rb
lib/puppet/type/ini_setting.rb
lib/puppet/util/ini_file.rb
spec/unit/puppet/provider/ini_setting/ruby_spec.rb
tests/ini_setting.pp

index 9f13dff4c010ce3cac55b21068fe12229e661a53..ba8165cb7fcaa36c6d94c5b21aed2b3f3c068a93 100644 (file)
@@ -14,6 +14,7 @@ Puppet::Type.type(:ini_setting).provide(:ruby) do
 
   private
   def ini_file
-    @ini_file ||= Puppet::Util::IniFile.new(resource[:path])
+    @ini_file ||= Puppet::Util::IniFile.new(resource[:path],
+                                            resource[:key_val_separator])
   end
 end
index 9af47c1bdbaa8687a8dfd4a2e64751f25b69eb10..f1ab490a1157e54ac143f3786869f2db43525791 100644 (file)
@@ -30,4 +30,11 @@ Puppet::Type.newtype(:ini_setting) do
     end
   end
 
-end
\ No newline at end of file
+  newparam(:key_val_separator) do
+    desc 'The separator string to use between each setting name and value. ' +
+        'Defaults to " = ", but you could use this to override e.g. whether ' +
+        'or not the separator should include whitespace.'
+    defaultto(" = ")
+  end
+
+end
index 4fe41691dc94e746afb270c236a4b7ba49335247..36bc8a63310c586a3fd5f18e93ab7c30337608a8 100644 (file)
@@ -8,8 +8,9 @@ module Util
     SECTION_REGEX = /^\s*\[([\w\d\.\\\/\-\:]+)\]\s*$/
     SETTING_REGEX = /^\s*([\w\d\.\\\/\-]+)\s*=\s*([\S]+)\s*$/
 
-    def initialize(path)
+    def initialize(path, key_val_separator = ' = ')
       @path = path
+      @key_val_separator = key_val_separator
       @section_names = []
       @sections_hash = {}
       if File.file?(@path)
@@ -56,7 +57,7 @@ module Util
           end
 
           section.additional_settings.each_pair do |key, value|
-            fh.puts("#{key} = #{value}")
+            fh.puts("#{key}#{@key_val_separator}#{value}")
           end
         end
       end
@@ -106,7 +107,7 @@ module Util
       (section.start_line..section.end_line).each do |line_num|
         if (match = SETTING_REGEX.match(lines[line_num]))
           if (match[1] == setting)
-            lines[line_num] = "#{setting} = #{value}"
+            lines[line_num] = "#{setting}#{@key_val_separator}#{value}"
           end
         end
       end
index 2dbbf5569d65b8f5865ef2d31a1b100f4c65c263..ac9bb919fca9b979dd498f2b5c44d5ae302d2047 100644 (file)
@@ -392,4 +392,48 @@ bar = baz
     end
   end
 
+  context "when overriding the separator" do
+    let(:orig_content) {
+      <<-EOS
+[section2]
+foo=bar
+      EOS
+    }
+
+    it "should modify an existing setting" do
+      resource = Puppet::Type::Ini_setting.new(common_params.merge(
+                                                   :section           => 'section2',
+                                                   :setting           => 'foo',
+                                                   :value             => 'yippee',
+                                                   :key_val_separator => '='))
+      provider = described_class.new(resource)
+      provider.exists?.should == false
+      provider.create
+      validate_file(<<-EOS
+[section2]
+foo=yippee
+      EOS
+      )
+    end
+
+    it "should add a new setting" do
+      resource = Puppet::Type::Ini_setting.new(common_params.merge(
+                                                   :section           => 'section2',
+                                                   :setting           => 'bar',
+                                                   :value             => 'baz',
+                                                   :key_val_separator => '='))
+      provider = described_class.new(resource)
+      provider.exists?.should == false
+      provider.create
+      validate_file(<<-EOS
+[section2]
+foo=bar
+bar=baz
+      EOS
+      )
+    end
+
+
+  end
+
 end
index 598bedf3251e056a39a27b2377444103af7ec158..6fd7686118c90b103ef981ccd1d289c23256f609 100644 (file)
@@ -7,10 +7,11 @@ ini_setting { "sample setting":
 }
 
 ini_setting { "sample setting2":
-  path    => '/tmp/foo.ini',
-  section => 'bar',
-  setting => 'barsetting',
-  value   => 'BAR!',
-  ensure  => present,
-  require => Ini_setting["sample setting"],
+  path                => '/tmp/foo.ini',
+  section             => 'bar',
+  setting             => 'barsetting',
+  value               => 'BAR!',
+  key_value_separator => '=',
+  ensure              => present,
+  require             => Ini_setting["sample setting"],
 }