Browse Source

Added actions :add and :remove

Juanje Ojeda 12 years ago
parent
commit
14151f6def
2 changed files with 41 additions and 2 deletions
  1. 36 1
      providers/plain_file.rb
  2. 5 1
      resources/plain_file.rb

+ 36 - 1
providers/plain_file.rb

@@ -1,5 +1,40 @@
 action :replace do
   file new_resource.name do
-    replace(new_resource.before, new_resource.after) if include?(new_resource.before)
+    old_content = IO.read new_resource.name
+    if old_content =~ /#{new_resource.before}/
+      content old_content.gsub(new_resource.before, new_resource.after)
+    end
+    owner new_resource.owner
+    group new_resource.group
   end
 end
+
+action :add do
+  file new_resource.name do
+    if ::File.exists? new_resource.name
+      old_content = IO.read new_resource.name
+    else
+      old_content = ""
+    end
+    old_content << new_resource.new_line + "\n"
+    content old_content
+    owner new_resource.owner
+    group new_resource.group
+  end
+end
+
+action :remove do
+  file new_resource.name do
+    if ::File.exists? new_resource.name
+      old_content = IO.read new_resource.name
+      new_content = ""
+      old_content.each do |line|
+        new_content << line unless line =~ /#{new_resource.pattern}/
+      end
+      content new_content
+      owner new_resource.owner
+      group new_resource.group
+    end
+  end
+end
+

+ 5 - 1
resources/plain_file.rb

@@ -1,5 +1,9 @@
-actions :replace # and possibly more
+actions :replace, :add, :remove
 
 attribute :name, :kind_of => String, :name_attribute => true
+attribute :owner, :kind_of => String
+attribute :group, :kind_of => String
 attribute :before, :kind_of => String
 attribute :after, :kind_of => String
+attribute :new_line, :kind_of => String
+attribute :pattern, :kind_of => String