diff options
| author | Paul Buetow <paul@buetow.org> | 2025-02-18 15:24:53 +0200 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2025-02-18 15:24:53 +0200 |
| commit | fdbd63c6798c6c92b482d03b1f197cbeb41a5448 (patch) | |
| tree | db78dc0d35d3a89bbff16a94f7e225d25154c490 /lib/dslkeywords/file.rb | |
| parent | cd1bdda5f963236dac849eb9257812f81c8af8ba (diff) | |
add ensure absent for file line
Diffstat (limited to 'lib/dslkeywords/file.rb')
| -rw-r--r-- | lib/dslkeywords/file.rb | 25 |
1 files changed, 21 insertions, 4 deletions
diff --git a/lib/dslkeywords/file.rb b/lib/dslkeywords/file.rb index dbcc612..020a97f 100644 --- a/lib/dslkeywords/file.rb +++ b/lib/dslkeywords/file.rb @@ -22,9 +22,12 @@ module RCM class File < Resource include FileBackup + class UnsupportedOperation < StandardError; end + def initialize(file_path) super(file_path) @file_path = file_path + @is = :installed end def content(text = nil) @@ -36,7 +39,7 @@ module RCM def create_parent_directory = @create_parent = true def from_sourcefile = @from_sourcefile = true def from_template = @from_template = true - def ensure_line(line) = @ensure_line = line + def line(line) = @ensure_line = line def path(file_path = nil) return @file_path if file_path.nil? @@ -44,17 +47,23 @@ module RCM @file_path = file_path end + def is(what) + @is = what.to_sym + raise UnsupportedOperation, "Unsupported operation #{@is}" unless %i[present absent].include?(@is) + end + def evaluate! return unless super return evaluate_ensure_line! unless @ensure_line.nil? - write_content!(real_content) + write!(real_content) end private def evaluate_ensure_line! - return write_content!(@ensure_line) unless ::File.file?(@file_path) + return evaluate_ensure_line_absent! if @is == :absent + return write!(@ensure_line) unless ::File.file?(@file_path) return if ::File.readlines(@file_path, chomp: true).include?(@ensure_line) ::File.open(@file_path, 'a') do |fd| @@ -62,7 +71,15 @@ module RCM end end - def write_content!(text) + def evaluate_ensure_line_absent! + return unless ::File.file?(@file_path) + + write!(::File.readlines(@file_path, chomp: true).reject do |line| + line == @ensure_line + end.join("\n")) + end + + def write!(text) info "Managing file #{@file_path}" create_parent_directory! |
