From 37eb90aebd8b26508d60f4ec623ae6192df5c09d Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Tue, 25 Feb 2025 23:16:54 +0200 Subject: initial mode support --- TODO.md | 3 ++- lib/dslkeywords/file.rb | 25 +++++++++++++++++++++++++ test/lib/dslkeywords/file_test.rb | 9 +++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/TODO.md b/TODO.md index 8e5118a..0c65e59 100644 --- a/TODO.md +++ b/TODO.md @@ -1,7 +1,8 @@ # TODO -* Recursively install a directory * Support for file modes (owner, chmod) + Test the mode +* Recursively install a directory (rsync?) * User and group management * Fedora * FreeBSD diff --git a/lib/dslkeywords/file.rb b/lib/dslkeywords/file.rb index 39e0499..c1f55d3 100644 --- a/lib/dslkeywords/file.rb +++ b/lib/dslkeywords/file.rb @@ -58,6 +58,15 @@ module RCM def manage(what) = @manage_directory = validate(__method__, what.to_sym, :directory) == :directory def path(file_path = nil) = file_path.nil? ? @file_path : @file_path = file_path def without(what) = @without_backup = validate(__method__, what.to_sym, :backup) == :backup + def mode(what) = @mode = what + + def evaluate! + unless super + @mode = nil + return false + end + true + end def content(text = nil) if text.nil? @@ -69,6 +78,14 @@ module RCM protected + def mode!(file_path = path) + return if !::File.exist?(file_path) || @mode.nil? + + dry? "Setting mode of #{file_path} to #{@mode}" do + FileUtils.chmod(@mode, file_path) + end + end + # Validate whether we can use this up in this context or not def validate(method, what, *valids) return what if valids.include?(what) @@ -129,6 +146,8 @@ module RCM create_parent_directory! if @manage_directory write!(content) + ensure + mode! end private @@ -187,6 +206,8 @@ module RCM dry? "Creating symlink #{@file_path}" do FileUtils.ln_sf(content, @file_path) end + ensure + mode! end end @@ -204,6 +225,8 @@ module RCM FileUtils.touch(@file_path) end + ensure + mode! end end @@ -217,6 +240,8 @@ module RCM when :absent, :purged evaluate_absent! end + ensure + mode! end def evaluate_present! diff --git a/test/lib/dslkeywords/file_test.rb b/test/lib/dslkeywords/file_test.rb index 35a0f21..0a95e7e 100644 --- a/test/lib/dslkeywords/file_test.rb +++ b/test/lib/dslkeywords/file_test.rb @@ -22,6 +22,15 @@ class RCMFileTest < Minitest::Test assert_equal text, File.read(FILE_PATH) end + # def test_file_mode + # configure_from_scratch do + # file FILE_PATH do + # mode 661 + # 'content' + # end + # end + # end + def test_create_file_from_array arr = %w[Hello World and Hello Universe] configure_from_scratch do -- cgit v1.2.3