diff options
| author | Paul Buetow <paul@buetow.org> | 2024-12-06 23:04:47 +0200 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2024-12-06 23:04:47 +0200 |
| commit | 7686fe830946ae36957501f1656bb429c694b09e (patch) | |
| tree | a6ac996e4a7cbbdc054c2a62c7ae374b61ed49af | |
| parent | bfdcad7efca071374de0be3124dcc92deeab681e (diff) | |
add log module
| -rw-r--r-- | Rakefile | 5 | ||||
| -rw-r--r-- | lib/autorequire/file.rb | 20 | ||||
| -rw-r--r-- | lib/autorequire/log.rb | 19 | ||||
| -rw-r--r-- | lib/rcm.rb | 11 |
4 files changed, 51 insertions, 4 deletions
@@ -7,8 +7,9 @@ task :wireguard do dump_config only_when { hostname is :earth } - file '/etc/wg/wg0.conf' do - content 'the content' + file '/tmp/test/wg/wg0.conf' do + create_parent + content 'the content is here' end end end diff --git a/lib/autorequire/file.rb b/lib/autorequire/file.rb index 2a25a07..3233015 100644 --- a/lib/autorequire/file.rb +++ b/lib/autorequire/file.rb @@ -1,8 +1,13 @@ +require 'fileutils' +require_relative 'log' + module RCM # Managing files class File attr_reader :path + include Log + def initialize(path) @path = path end @@ -11,12 +16,25 @@ module RCM content.nil? ? @content : @content = content end + def create_parent + @create_parent = true + end + def to_s @path end def do! - puts "Evaluating #{self.class}:#{self}" + dirname = ::File.dirname(@path) + if !::File.directory?(dirname) && @create_parent + info "Creating parent directory #{parent}" + FileUtils.mkdir_p(dirname) + end + + info "Creating file #{@path}" + tmp_path = "#{@path}.tmp" + ::File.write(tmp_path, @content) + ::File.rename(tmp_path, @path) end end diff --git a/lib/autorequire/log.rb b/lib/autorequire/log.rb new file mode 100644 index 0000000..0fe9ea9 --- /dev/null +++ b/lib/autorequire/log.rb @@ -0,0 +1,19 @@ +require 'logger' + +module RCM + module Log + @@logger = Logger.new(STDOUT) + + def info(message) + @@logger.info("#{self.class}(#{self}): #{message}") + end + + def warn(message) + @@logger.warn("#{self.class}(#{self}): #{message}") + end + + def debug(message) + @@logger.debug("#{self.class}(#{self}): #{message}") + end + end +end @@ -2,15 +2,23 @@ Dir["#{Dir.pwd}/lib/autorequire/*.rb"].each { |m| require m } # Ruby Configiration Management system module RCM - # Here all starts class RCM + @@rcm_counter = 0 + include Config include Options + include Log def initialize @objs = [] @conds_met = true + @@rcm_counter += 1 + @number = @@rcm_counter + end + + def to_s + "RCM #{@number}" end def do! @@ -25,6 +33,7 @@ end def make_it_so(&block) rcm = RCM::RCM.new + rcm.info('Making it so...') rcm.instance_eval(&block) rcm.do! end |
