summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2025-02-14 21:08:34 +0200
committerPaul Buetow <paul@buetow.org>2025-02-14 21:08:34 +0200
commit07e5f8d4402346ccb336cc0680cad98d0d5ba920 (patch)
tree06927c1e10d41c75e822762ab43606e45c833778
parentccc0fab74a0367c8236679333a40774a3f810512 (diff)
add ensure_line
-rw-r--r--Gemfile3
-rw-r--r--Gemfile.lock4
-rw-r--r--Rakefile13
-rw-r--r--lib/dsl.rb1
-rw-r--r--lib/dslkeywords/file.rb27
5 files changed, 39 insertions, 9 deletions
diff --git a/Gemfile b/Gemfile
index 6bc7a34..728602d 100644
--- a/Gemfile
+++ b/Gemfile
@@ -1,4 +1,5 @@
source 'https://rubygems.org'
-gem 'toml'
gem 'erb'
+gem 'rbs'
+gem 'toml'
diff --git a/Gemfile.lock b/Gemfile.lock
index fe13324..0fcb5fb 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -4,7 +4,10 @@ GEM
cgi (0.4.1)
erb (4.0.4)
cgi (>= 0.3.3)
+ logger (1.6.4)
parslet (2.0.0)
+ rbs (3.7.0)
+ logger
toml (0.3.0)
parslet (>= 1.8.0, < 3.0.0)
@@ -14,6 +17,7 @@ PLATFORMS
DEPENDENCIES
erb
+ rbs
toml
BUNDLED WITH
diff --git a/Rakefile b/Rakefile
index d883dac..99a599f 100644
--- a/Rakefile
+++ b/Rakefile
@@ -1,4 +1,4 @@
-require_relative 'lib/dsl'.frozen
+require_relative 'lib/dsl'
desc 'Set up wireguard mesh'
task :wireguard do
@@ -36,7 +36,16 @@ task :hosts do
only_when { hostname is :earth }
file '/etc/hosts.test' do
- add_line '192.168.1.101 foo'
+ ensure_line '192.168.1.101 foo'
+ end
+ end
+end
+
+desc 'foo'
+task :foo do
+ configure do
+ file '/tmp/foo.txt' do
+ ensure_line 'foo bar baz'
end
end
end
diff --git a/lib/dsl.rb b/lib/dsl.rb
index f6f8e8e..1629afc 100644
--- a/lib/dsl.rb
+++ b/lib/dsl.rb
@@ -10,6 +10,7 @@ module RCM
class DSL
attr_reader :id
+ # TODO: Replace @@ with @ class variables
@@rcm_counter = -1
@@objs = {}
diff --git a/lib/dslkeywords/file.rb b/lib/dslkeywords/file.rb
index 7e24801..403f01e 100644
--- a/lib/dslkeywords/file.rb
+++ b/lib/dslkeywords/file.rb
@@ -28,21 +28,36 @@ module RCM
def create_parent_directory = @create_parent = true
def from_sourcefile = @from_sourcefile = true
def from_template = @from_template = true
+ def ensure_line(line) = @ensure_line = line
def evaluate!
- create_parent_directory!
- content = real_content
+ return evaluate_ensure_line! unless @ensure_line.nil?
- info "Creating file #{@path}"
- debug content if option :debug
+ write_content!(real_content)
+ end
+
+ private
+
+ 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)
+ ::File.open(@path, 'a') do |fd|
+ fd.puts(@ensure_line)
+ end
+ end
+
+ def write_content!(content)
+ create_parent_directory!
+ debug content if option :debug
+ info "Creating file #{@path}"
tmp_path = "#{@path}.tmp"
::File.write(tmp_path, content)
::File.rename(tmp_path, @path)
end
- private
-
def create_parent_directory!
dirname = ::File.dirname(@path)
return unless !::File.directory?(dirname) && @create_parent