diff options
| author | Paul Buetow <paul@buetow.org> | 2025-02-18 10:46:44 +0200 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2025-02-18 10:46:44 +0200 |
| commit | 614a3b5062d3a37d7f1eb59ad47e4ec5d791edc6 (patch) | |
| tree | f9bac6d6b1b91bcc5675d880bbb31eff632dc792 | |
| parent | 9bb314cb170a651571b57767d33b3c01112aa5bc (diff) | |
backupping file when change
| -rw-r--r-- | TODO.md | 1 | ||||
| -rw-r--r-- | lib/dslkeywords/file.rb | 22 | ||||
| -rw-r--r-- | lib/dslkeywords/resource.rb | 2 |
3 files changed, 23 insertions, 2 deletions
@@ -1,7 +1,6 @@ # TODO * Recursively install a directory -* Backup of files when changed * Support for file deletion * Support for file modes (owner, chmod) * Support for symlinks diff --git a/lib/dslkeywords/file.rb b/lib/dslkeywords/file.rb index b42f223..e83906e 100644 --- a/lib/dslkeywords/file.rb +++ b/lib/dslkeywords/file.rb @@ -1,13 +1,32 @@ +require 'digest' require 'erb' require 'fileutils' require_relative 'resource' module RCM + # Backup the file on change + module FileBackup + def backup!(path) + return unless ::File.exist?(path) + + backup_dir = "#{::File.dirname(path)}/.rcm" + Dir.mkdir(backup_dir) unless ::File.directory?(backup_dir) + checksum = Digest::SHA256.file(path).hexdigest + backup_path = "#{backup_dir}/#{::File.basename(path)}.#{checksum}" + return if ::File.exist?(backup_path) + + info("Backing up #{path} -> #{backup_path}") + ::File.rename(path, backup_path) + end + end + # Managing files class File < Resource attr_reader :path + include FileBackup + def initialize(path) super(path) @path = path @@ -45,9 +64,10 @@ module RCM def write_content!(text) create_parent_directory! debug text if option :debug - info "Creating file #{@path}" + info "Managing file #{@path}" tmp_path = "#{@path}.tmp" ::File.write(tmp_path, text) + backup!(@path) ::File.rename(tmp_path, @path) end diff --git a/lib/dslkeywords/resource.rb b/lib/dslkeywords/resource.rb index f646030..f665a50 100644 --- a/lib/dslkeywords/resource.rb +++ b/lib/dslkeywords/resource.rb @@ -69,6 +69,8 @@ module RCM class NoSuchResourceObject < StandardError; end + # TODO: Detect duplicate resource definition + @@resource_find_cache = {} def self.find(id) |
