Browse Source

It will now actually work correctly.

Douglas Thrift 10 years ago
parent
commit
aacb33e4b8
2 changed files with 50 additions and 4 deletions
  1. 46 0
      libraries/conf_file_edit.rb
  2. 4 4
      providers/plain_file.rb

+ 46 - 0
libraries/conf_file_edit.rb

@@ -0,0 +1,46 @@
+module Conf
+  class ConfFileEdit < Chef::Util::FileEdit
+    attr_accessor :file_edited
+
+    private
+
+    def search_match(regex, replace, command, method)
+
+      #convert regex to a Regexp object (if not already is one) and store it in exp.
+      exp = Regexp.new(regex)
+
+      #loop through contents and do the appropriate operation depending on 'command' and 'method'
+      new_contents = []
+
+      matched = false
+
+      contents.each do |line|
+        if line.match(exp)
+          matched = true
+          case command
+          when 'r'
+            new_contents << ((method == 1) ? replace : line.gsub(exp, replace))
+            self.file_edited = true
+          when 'd'
+            new_contents << line.gsub(exp, "") if method == 2
+            self.file_edited = true
+          when 'i'
+            new_contents << line
+            unless method == 2
+              new_contents << replace
+              self.file_edited = true
+            end
+          end
+        else
+          new_contents << line
+        end
+      end
+      if command == 'i' && method == 2 && ! matched
+        new_contents << replace
+        self.file_edited = true
+      end
+
+      self.contents = new_contents
+    end
+  end
+end

+ 4 - 4
providers/plain_file.rb

@@ -10,7 +10,7 @@ end
 
 action :replace do
   if ::File.exists? new_resource.name
-    current_content = Chef::Util::FileEdit.new new_resource.name
+    current_content = Conf::ConfFileEdit.new new_resource.name
     current_content.search_file_replace(new_resource.current_line, new_resource.new_line)
     edited = current_content.file_edited
     current_content.write_file
@@ -34,7 +34,7 @@ end
 
 action :insert_if_no_match do
   if ::File.exists? new_resource.name
-    new_file = Chef::Util::FileEdit.new new_resource.name
+    new_file = Conf::ConfFileEdit.new new_resource.name
     new_file.insert_line_if_no_match(new_resource.pattern,
                                      new_resource.new_line)
     edited = new_file.file_edited
@@ -51,7 +51,7 @@ end
 
 action :insert_after_match do
   if ::File.exists? new_resource.name
-    new_file = Chef::Util::FileEdit.new new_resource.name
+    new_file = Conf::ConfFileEdit.new new_resource.name
     new_file.insert_line_after_match(new_resource.pattern,
                                      new_resource.new_line)
     edited = new_file.file_edited
@@ -68,7 +68,7 @@ end
 
 action :remove do
   if ::File.exists? new_resource.name
-    new_file = Chef::Util::FileEdit.new new_resource.name
+    new_file = Conf::ConfFileEdit.new new_resource.name
     new_file.search_file_delete_line(new_resource.pattern)
     edited = new_file.file_edited
     new_file.write_file