Browse Source

Lots of refactoring. Actions now makes more sense

* Renamed :add to :append
* Added actions: :insert_if_no_match and :insert_after_match
* Renamed attribute ':before' to ':current_line'
* Reuse the attribute ':new_line' instead of using ':after' (removed this one)
* Set attribute :pattern as Regexp instead of String. Makes more sense
Juanje Ojeda 7 years ago
parent
commit
4723361145
2 changed files with 50 additions and 16 deletions
  1. 46 11
      providers/plain_file.rb
  2. 4 5
      resources/plain_file.rb

+ 46 - 11
providers/plain_file.rb

@@ -1,27 +1,62 @@
 
+def write_new_file(name, new_line, owner, group)
+  file name do
+    content "#{new_line}\n"
+    owner owner
+    group group
+    action :create
+  end
+end
+
 action :replace do
   if ::File.exists? new_resource.name
-    old_content = Chef::Util::FileEdit.new new_resource.name
-    old_content.search_file_replace(new_resource.before, new_resource.after)
-    old_content.write_file
+    current_content = Chef::Util::FileEdit.new new_resource.name
+    current_content.search_file_replace(new_resource.current_line, new_resource.new_line)
+    current_content.write_file
     new_resource.updated_by_last_action(true)
   else
     Chef::Log.debug("replace action couldn't be performed. #{new_resource.name} does not exist")
   end
 end
 
-action :add do
+action :append do
+  if ::File.exists? new_resource.name
+      open(new_resource.name, 'a') { |f| f.puts new_resource.new_line }
+  else
+    write_new_file(new_resource.name,
+                   new_resource.new_line,
+                   new_resource.owner,
+                   new_resource.group)
+  end
+  new_resource.updated_by_last_action(true)
+end
+
+action :insert_if_no_match do
+  if ::File.exists? new_resource.name
+    new_file = Chef::Util::FileEdit.new new_resource.name
+    new_file.insert_line_if_no_match(new_resource.pattern,
+                                     new_resource.new_line)
+    new_file.write_file
+  else
+    write_new_file(new_resource.name,
+                   new_resource.new_line,
+                   new_resource.owner,
+                   new_resource.group)
+  end
+  new_resource.updated_by_last_action(true)
+end
+
+action :insert_after_match do
   if ::File.exists? new_resource.name
     new_file = Chef::Util::FileEdit.new new_resource.name
-    new_file.insert_line_if_no_match(new_resource.pattern, new_resource.new_line)
+    new_file.insert_line_after_match(new_resource.pattern,
+                                     new_resource.new_line)
     new_file.write_file
   else
-    file new_resource.name do
-      content new_resource.new_line + "\n"
-      owner new_resource.owner
-      group new_resource.group
-      action :create
-    end
+    write_new_file(new_resource.name,
+                   new_resource.new_line,
+                   new_resource.owner,
+                   new_resource.group)
   end
   new_resource.updated_by_last_action(true)
 end

+ 4 - 5
resources/plain_file.rb

@@ -1,14 +1,13 @@
-actions :replace, :add, :remove
+actions :replace, :append, :insert_if_no_match, :insert_after_match, :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 :current_line, :kind_of => String
 attribute :new_line, :kind_of => String
-attribute :pattern, :kind_of => String
+attribute :pattern, :kind_of => Regexp
 
 def initialize(*args)
   super
-  @action = :add
+  @action = :append unless @new_line.nil?
 end