summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2025-02-15 09:25:57 +0200
committerPaul Buetow <paul@buetow.org>2025-02-15 09:25:57 +0200
commit6879b03b2735b082b913ab17e63857f464f53c93 (patch)
tree525b1b78983ba3b3d6f4e218daf0055edf7728a9
parent463f429166c5ace855998b433e865140f9b737ca (diff)
dsl keyword base class added
-rw-r--r--lib/dsl.rb7
-rw-r--r--lib/dslkeywords/file.rb18
-rw-r--r--lib/dslkeywords/keyword.rb15
-rw-r--r--lib/dslkeywords/only_when.rb14
-rw-r--r--playground/Rakefile15
5 files changed, 39 insertions, 30 deletions
diff --git a/lib/dsl.rb b/lib/dsl.rb
index 413a34c..f3ccfc3 100644
--- a/lib/dsl.rb
+++ b/lib/dsl.rb
@@ -2,7 +2,8 @@ require_relative 'config'
require_relative 'options'
require_relative 'log'
-Dir["#{Dir.pwd}/lib/dslkeywords/*.rb"].each { |m| require m }
+require_relative 'dslkeywords/file'
+require_relative 'dslkeywords/only_when'
# Ruby Configiration Management system
module RCM
@@ -23,13 +24,13 @@ module RCM
def initialize(reset)
DSL.reset! if reset
- @id = "#{self.class}(#{@@rcm_counter += 1})"
+ @id = "DSL[#{@@rcm_counter += 1}]"
@conds_met = true
@scheduled = []
yield self if block_given?
end
- def to_s = "RCM #{@number}"
+ def to_s = @id
def evaluate! = @scheduled.each(&:evaluate!)
def <<(obj)
diff --git a/lib/dslkeywords/file.rb b/lib/dslkeywords/file.rb
index 1b99a91..50f753e 100644
--- a/lib/dslkeywords/file.rb
+++ b/lib/dslkeywords/file.rb
@@ -1,24 +1,18 @@
require 'erb'
require 'fileutils'
-require_relative '../options'
-require_relative '../log'
+require_relative 'keyword'
module RCM
# Managing files
- class File
- attr_reader :id, :path
-
- include Options
- include Log
+ class File < Keyword
+ attr_reader :path
def initialize(path)
- @id = "#{self.class}(#{path})"
+ super(path)
@path = path
end
- def to_s = id
-
def content(text = nil)
return @content if text.nil?
@@ -40,9 +34,7 @@ module RCM
def evaluate_ensure_line!
return write_content!(@ensure_line) unless ::File.file?(@path)
-
- lines = ::File.readlines(@path, chomp: true)
- return if lines.include?(@ensure_line)
+ return if ::File.readlines(@path, chomp: true).include?(@ensure_line)
::File.open(@path, 'a') do |fd|
fd.puts(@ensure_line)
diff --git a/lib/dslkeywords/keyword.rb b/lib/dslkeywords/keyword.rb
new file mode 100644
index 0000000..33d03db
--- /dev/null
+++ b/lib/dslkeywords/keyword.rb
@@ -0,0 +1,15 @@
+require_relative '../options'
+require_relative '../log'
+
+module RCM
+ # The base class of all DSL key words
+ class Keyword
+ attr_reader :id
+
+ include Options
+ include Log
+
+ def initialize(name) = @id = "#{self.class.to_s.sub('RCM::', '')}[#{name}]"
+ def to_s = @id
+ end
+end
diff --git a/lib/dslkeywords/only_when.rb b/lib/dslkeywords/only_when.rb
index 485fd42..f23c33e 100644
--- a/lib/dslkeywords/only_when.rb
+++ b/lib/dslkeywords/only_when.rb
@@ -1,9 +1,15 @@
+require 'socket'
+
+require_relative 'keyword'
+
module RCM
# OnlyWhen (e.g. run on host foo)
- class OnlyWhen
- require 'socket'
+ class OnlyWhen < Keyword
+ def initialize(dsl_id)
+ super(dsl_id)
+ @conds = {}
+ end
- def initialize = @conds = {}
def is(arg) = arg
def method_missing(method_name, *args) = @conds[method_name] = args.first
def respond_to_missing? = true
@@ -18,7 +24,7 @@ module RCM
# Add 'only_when' to DSL
class DSL
def only_when(&block)
- conds = OnlyWhen.new
+ conds = OnlyWhen.new(id)
conds.instance_eval(&block)
@conds_met = conds.met?
end
diff --git a/playground/Rakefile b/playground/Rakefile
index e006960..aff881d 100644
--- a/playground/Rakefile
+++ b/playground/Rakefile
@@ -21,15 +21,6 @@ task :wireguard do
end
end
-desc 'foo task'
-task :foo do
- configure do
- file '/tmp/test.txt' do
- %w[foo bar baz].sort
- end
- end
-end
-
desc 'Set up the /etc/hosts file'
task :hosts do
configure do
@@ -45,7 +36,11 @@ desc 'foo'
task :foo do
configure do
file '/tmp/foo.txt' do
- ensure_line 'foo bar baz'
+ ensure_line 'foo'
+ end
+
+ file '/tmp/bar.txt' do
+ 'bar'
end
end
end