summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2025-02-19 22:09:52 +0200
committerPaul Buetow <paul@buetow.org>2025-02-19 22:09:52 +0200
commit1afd39cccdb17e5f5c064b376db61d7c2b833ed3 (patch)
treef2c59dee57795dae643f9b14bd188382e8a07631
parent1f085e83f018048931079e9bf0a564b461fd713c (diff)
adding dry
-rw-r--r--TODO.md2
-rw-r--r--lib/dslkeywords/file.rb45
-rw-r--r--lib/dslkeywords/notify.rb4
-rw-r--r--lib/dslkeywords/resource.rb10
4 files changed, 43 insertions, 18 deletions
diff --git a/TODO.md b/TODO.md
index 1e61407..f0a86ba 100644
--- a/TODO.md
+++ b/TODO.md
@@ -3,7 +3,7 @@
* Implement dry run mode, e.g. use if option :dry ...
* Go through all keywords and evaluate methods!
* Print out what it would do but don't do it (helper method?)
-* Backup file when it gets deleted
+ * Action method with a block...! Is the way to go!
* Recursively install a directory
* Support for file deletion
* Support for file modes (owner, chmod)
diff --git a/lib/dslkeywords/file.rb b/lib/dslkeywords/file.rb
index a734042..f6c6998 100644
--- a/lib/dslkeywords/file.rb
+++ b/lib/dslkeywords/file.rb
@@ -61,15 +61,17 @@ module RCM
dirname = ::File.dirname(@file_path)
return if ::File.directory?(dirname)
- info "Creating parent directory #{dirname}"
- FileUtils.mkdir_p(dirname)
+ dry? "Creating parent directory #{dirname}" do
+ FileUtils.mkdir_p(dirname)
+ end
end
def evaluate_absent!
if ::File.exist?(@file_path)
- info("Deleting #{@file_path}")
- backup!(@file_path)
- ::File.delete(@file_path) if ::File.file?(@file_path)
+ dry? "Deleting #{@file_path}" do
+ backup!(@file_path)
+ ::File.delete(@file_path) if ::File.file?(@file_path)
+ end
end
cleanup_directory! if @manage_directory
end
@@ -77,8 +79,9 @@ module RCM
def cleanup_directory!
parent_dir = ::File.dirname(@file_path)
while Dir.empty?(parent_dir)
- info("Deleting empty parent directory #{parent_dir}")
- Dir.rmdir(parent_dir)
+ dry? "Deleting empty parent directory #{parent_dir}" do
+ Dir.rmdir(parent_dir)
+ end
parent_dir = ::File.dirname(parent_dir)
end
end
@@ -93,6 +96,7 @@ module RCM
def evaluate!
return unless super
+
return evaluate_ensure_line! unless @ensure_line.nil?
return evaluate_absent! if @is == :absent
@@ -108,22 +112,28 @@ module RCM
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|
- fd.puts(@ensure_line)
+ dry? "Appending line #{@ensure_line} to #{@file_path}" do
+ ::File.open(@file_path, 'a') do |fd|
+ fd.puts(@ensure_line)
+ end
end
end
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"))
+ dry? "Removing line #{@ensure_line} from #{@file_path}" do
+ write!(::File.readlines(@file_path, chomp: true).reject do |line|
+ line == @ensure_line
+ end.join("\n"))
+ end
end
def write!(text)
tmp_path = "#{@file_path}.rcmtmp"
- ::File.write(tmp_path, text)
+ dry? "Writing file #{@file_path}" do
+ ::File.write(tmp_path, text)
+ end
if ::File.file?(@file_path)
checksum = Digest::SHA256.file(@file_path).hexdigest
@@ -133,10 +143,11 @@ module RCM
::File.delete(tmp_path) # File has not changed, not doing anything
return
end
- backup!(@file_path, checksum) # File changed, backup!
+ # File changed, backup!
+ backup!(@file_path, checksum) unless option :dry
end
- ::File.rename(tmp_path, @file_path)
+ ::File.rename(tmp_path, @file_path) unless option :dry
end
end
@@ -147,7 +158,9 @@ module RCM
return evaluate_absent! if @is == :absent
create_parent_directory! if @manage_directory
- FileUtils.ln_sf(content, @file_path)
+ dry? "Creating symlink #{@file_path}" do
+ FileUtils.ln_sf(content, @file_path)
+ end
end
end
diff --git a/lib/dslkeywords/notify.rb b/lib/dslkeywords/notify.rb
index 6616cb0..8f0577e 100644
--- a/lib/dslkeywords/notify.rb
+++ b/lib/dslkeywords/notify.rb
@@ -21,7 +21,9 @@ module RCM
def evaluate!
return unless super
- puts "#{id} => #{@message}"
+ dry? "Notifying #{@message}" do
+ puts "#{id} => #{@message}"
+ end
end
end
diff --git a/lib/dslkeywords/resource.rb b/lib/dslkeywords/resource.rb
index aea47cc..8bf62e1 100644
--- a/lib/dslkeywords/resource.rb
+++ b/lib/dslkeywords/resource.rb
@@ -38,6 +38,16 @@ module RCM
end
def requires?(*others) = others.flatten.none? { |other| !@requires&.include?(other) }
+
+ # Only run the block when not in dry mode
+ def dry?(message)
+ if option :dry
+ info("#{message} - dry run!")
+ return
+ end
+ info(message)
+ yield
+ end
end
# To resolve dependencies