From 7686fe830946ae36957501f1656bb429c694b09e Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Fri, 6 Dec 2024 23:04:47 +0200 Subject: add log module --- Rakefile | 5 +++-- lib/autorequire/file.rb | 20 +++++++++++++++++++- lib/autorequire/log.rb | 19 +++++++++++++++++++ lib/rcm.rb | 11 ++++++++++- 4 files changed, 51 insertions(+), 4 deletions(-) create mode 100644 lib/autorequire/log.rb diff --git a/Rakefile b/Rakefile index 5122919..4e7f68b 100644 --- a/Rakefile +++ b/Rakefile @@ -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 diff --git a/lib/rcm.rb b/lib/rcm.rb index 0c3d028..d9880eb 100644 --- a/lib/rcm.rb +++ b/lib/rcm.rb @@ -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 -- cgit v1.2.3