summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Gemfile1
-rw-r--r--Gemfile.lock4
-rw-r--r--Rakefile13
-rw-r--r--lib/autorequire/file.rb37
-rw-r--r--lib/autorequire/log.rb11
-rw-r--r--lib/autorequire/options.rb4
-rw-r--r--lib/rcm.rb11
7 files changed, 63 insertions, 18 deletions
diff --git a/Gemfile b/Gemfile
index f41482e..6bc7a34 100644
--- a/Gemfile
+++ b/Gemfile
@@ -1,3 +1,4 @@
source 'https://rubygems.org'
gem 'toml'
+gem 'erb'
diff --git a/Gemfile.lock b/Gemfile.lock
index 540a42c..fe13324 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -1,6 +1,9 @@
GEM
remote: https://rubygems.org/
specs:
+ cgi (0.4.1)
+ erb (4.0.4)
+ cgi (>= 0.3.3)
parslet (2.0.0)
toml (0.3.0)
parslet (>= 1.8.0, < 3.0.0)
@@ -10,6 +13,7 @@ PLATFORMS
x86_64-linux
DEPENDENCIES
+ erb
toml
BUNDLED WITH
diff --git a/Rakefile b/Rakefile
index 4e7f68b..8c35269 100644
--- a/Rakefile
+++ b/Rakefile
@@ -3,13 +3,18 @@ require_relative 'lib/rcm'
desc 'Set up wireguard mesh'
task :wireguard do
make_it_so do
- p option :verbose
- dump_config
+ # p option :verbose
+ # dump_config
only_when { hostname is :earth }
file '/tmp/test/wg/wg0.conf' do
- create_parent
- content 'the content is here'
+ create_parent_directory
+ from_template content 'the content is here and the result is <%= 1 + 2 %>'
+ end
+
+ file '/tmp/test/wg/wg1.conf' do
+ create_parent_directory
+ from_file content './Rakefile'
end
end
end
diff --git a/lib/autorequire/file.rb b/lib/autorequire/file.rb
index 3233015..35df33f 100644
--- a/lib/autorequire/file.rb
+++ b/lib/autorequire/file.rb
@@ -1,30 +1,48 @@
+require 'erb'
require 'fileutils'
+
+require_relative 'options'
require_relative 'log'
module RCM
# Managing files
class File
- attr_reader :path
+ attr_reader :id, :path
+ include Options
include Log
def initialize(path)
+ @id = "#{self.class}(#{path})"
@path = path
end
+ def to_s
+ id
+ end
+
def content(content = nil)
content.nil? ? @content : @content = content
end
- def create_parent
+ def create_parent_directory
@create_parent = true
+ self
end
- def to_s
- @path
+ def from_file(...)
+ @from_file = true
+ self
+ end
+
+ def from_template(...)
+ @from_template = true
+ self
end
def do!
+ content = file_content
+
dirname = ::File.dirname(@path)
if !::File.directory?(dirname) && @create_parent
info "Creating parent directory #{parent}"
@@ -32,10 +50,19 @@ module RCM
end
info "Creating file #{@path}"
+ debug content if option :debug
+
tmp_path = "#{@path}.tmp"
- ::File.write(tmp_path, @content)
+ ::File.write(tmp_path, content)
::File.rename(tmp_path, @path)
end
+
+ private
+
+ def file_content
+ content = @from_file ? ::File.read(@content) : @content
+ @from_template ? ERB.new(content).result : content
+ end
end
# Add file keyword to the DSL
diff --git a/lib/autorequire/log.rb b/lib/autorequire/log.rb
index 0fe9ea9..67ff321 100644
--- a/lib/autorequire/log.rb
+++ b/lib/autorequire/log.rb
@@ -5,15 +5,20 @@ module RCM
@@logger = Logger.new(STDOUT)
def info(message)
- @@logger.info("#{self.class}(#{self}): #{message}")
+ @@logger.info("#{id} => #{message}")
end
def warn(message)
- @@logger.warn("#{self.class}(#{self}): #{message}")
+ @@logger.warn("#{id} => #{message}")
+ end
+
+ def fatal_exit(message)
+ @@logger.fatal("#{id} => #{message}")
+ exit 2
end
def debug(message)
- @@logger.debug("#{self.class}(#{self}): #{message}")
+ @@logger.debug("#{id} => #{message}")
end
end
end
diff --git a/lib/autorequire/options.rb b/lib/autorequire/options.rb
index de54888..c3950ca 100644
--- a/lib/autorequire/options.rb
+++ b/lib/autorequire/options.rb
@@ -4,14 +4,14 @@ module RCM
# Command line options
module Options
@@options = {
- verbose: false
+ debug: false
}
after_double_dash = ARGV.slice_before('--').to_a.last.drop(1)
OptionParser.new do |opts|
opts.banner = 'Usage: rake [task] -- [options]'
- opts.on('-v', '--[no-]verbose', 'run verbosely') { |v| @@options[:verbose] = v }
+ opts.on('-v', '--[no-]debug', 'debug output') { |v| @@options[:debug] = v }
end.parse!(after_double_dash)
def option(key)
diff --git a/lib/rcm.rb b/lib/rcm.rb
index d9880eb..1f45c39 100644
--- a/lib/rcm.rb
+++ b/lib/rcm.rb
@@ -4,6 +4,8 @@ Dir["#{Dir.pwd}/lib/autorequire/*.rb"].each { |m| require m }
module RCM
# Here all starts
class RCM
+ attr_reader :id
+
@@rcm_counter = 0
include Config
@@ -11,10 +13,10 @@ module RCM
include Log
def initialize
- @objs = []
+ @id = "#{self.class}(#{@@rcm_counter})"
+ @objs = {}
@conds_met = true
@@rcm_counter += 1
- @number = @@rcm_counter
end
def to_s
@@ -22,11 +24,12 @@ module RCM
end
def do!
- @objs.each(&:do!)
+ @objs.each_value(&:do!)
end
def <<(obj)
- @objs << obj
+ fatal_exit "Object #{obj.id} already declared!" if @objs.key?(obj.id)
+ @objs[obj.id] = obj
end
end
end