Class: Chef::Util::FileEdit

Inherits:
Object show all
Defined in:
lib/chef/util/file_edit.rb

Instance Method Summary collapse

Constructor Details

#initialize(filepath) ⇒ FileEdit

Returns a new instance of FileEdit.

Raises:

  • (ArgumentError)


31
32
33
34
35
36
37
# File 'lib/chef/util/file_edit.rb', line 31

def initialize(filepath)
  @original_pathname = filepath
  @file_edited = false

  raise ArgumentError, "File doesn't exist" unless File.exist? @original_pathname
  raise ArgumentError, "File is blank" unless (@contents = File.new(@original_pathname).readlines).length > 0
end

Instance Method Details

#insert_line_after_match(regex, newline) ⇒ Object

search the file line by line and match each line with the given regex if matched, insert newline after each matching line



65
66
67
# File 'lib/chef/util/file_edit.rb', line 65

def insert_line_after_match(regex, newline)
  search_match(regex, newline, 'i', 1)
end

#insert_line_if_no_match(regex, newline) ⇒ Object

search the file line by line and match each line with the given regex if not matched, insert newline at the end of the file



71
72
73
# File 'lib/chef/util/file_edit.rb', line 71

def insert_line_if_no_match(regex, newline)
  search_match(regex, newline, 'i', 2)
end

#search_file_delete(regex) ⇒ Object

search the file line by line and match each line with the given regex if matched, delete the match (all occurances) from the line



59
60
61
# File 'lib/chef/util/file_edit.rb', line 59

def search_file_delete(regex)
  search_match(regex, " ", 'd', 2)
end

#search_file_delete_line(regex) ⇒ Object

search the file line by line and match each line with the given regex if matched, delete the line



53
54
55
# File 'lib/chef/util/file_edit.rb', line 53

def search_file_delete_line(regex)
  search_match(regex, " ", 'd', 1)
end

#search_file_replace(regex, replace) ⇒ Object

search the file line by line and match each line with the given regex if matched, replace the match (all occurances) with the replace parameter



47
48
49
# File 'lib/chef/util/file_edit.rb', line 47

def search_file_replace(regex, replace)
  search_match(regex, replace, 'r', 2)
end

#search_file_replace_line(regex, newline) ⇒ Object

search the file line by line and match each line with the given regex if matched, replace the whole line with newline.



41
42
43
# File 'lib/chef/util/file_edit.rb', line 41

def search_file_replace_line(regex, newline)
  search_match(regex, newline, 'r', 1)
end

#write_fileObject

Make a copy of old_file and write new file out (only if file changed)



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/chef/util/file_edit.rb', line 76

def write_file

  # file_edited is false when there was no match in the whole file and thus no contents have changed.
  if file_edited
    backup_pathname = original_pathname + ".old"
    FileUtils.cp(original_pathname, backup_pathname, :preserve => true)
    File.open(original_pathname, "w") do |newfile|
      contents.each do |line|
        newfile.puts(line)
      end
      newfile.flush
    end
  end
  self.file_edited = false
end