conf_file_edit.rb 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. module Conf
  2. class ConfFileEdit < Chef::Util::FileEdit
  3. attr_accessor :file_edited
  4. private
  5. def search_match(regex, replace, command, method)
  6. #convert regex to a Regexp object (if not already is one) and store it in exp.
  7. exp = Regexp.new(regex)
  8. #loop through contents and do the appropriate operation depending on 'command' and 'method'
  9. new_contents = []
  10. matched = false
  11. contents.each do |line|
  12. if line.match(exp)
  13. matched = true
  14. case command
  15. when 'r'
  16. new_contents << ((method == 1) ? replace : line.gsub(exp, replace))
  17. self.file_edited = true
  18. when 'd'
  19. new_contents << line.gsub(exp, "") if method == 2
  20. self.file_edited = true
  21. when 'i'
  22. new_contents << line
  23. unless method == 2
  24. new_contents << replace
  25. self.file_edited = true
  26. end
  27. end
  28. else
  29. new_contents << line
  30. end
  31. end
  32. if command == 'i' && method == 2 && ! matched
  33. new_contents << replace
  34. self.file_edited = true
  35. end
  36. self.contents = new_contents
  37. end
  38. end
  39. end