summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2024-12-06 22:33:07 +0200
committerPaul Buetow <paul@buetow.org>2024-12-06 22:33:07 +0200
commitbfdcad7efca071374de0be3124dcc92deeab681e (patch)
tree6215f6fcf5287f69840d06ad8c009dab0945ef6d /lib
parent4a3629e42cb3e1fbca4356cf789a8e79043cdc51 (diff)
auto require
Diffstat (limited to 'lib')
-rw-r--r--lib/autorequire/config.rb18
-rw-r--r--lib/autorequire/file.rb33
-rw-r--r--lib/autorequire/only_when.rb37
-rw-r--r--lib/autorequire/options.rb23
-rw-r--r--lib/rcm.rb30
5 files changed, 141 insertions, 0 deletions
diff --git a/lib/autorequire/config.rb b/lib/autorequire/config.rb
new file mode 100644
index 0000000..b9dc5ac
--- /dev/null
+++ b/lib/autorequire/config.rb
@@ -0,0 +1,18 @@
+require 'toml'
+
+module RCM
+ # Configuration
+ module Config
+ @@config = File.exist?('config.toml') ? TOML.load_file('config.toml') : {}
+
+ def config(key)
+ raise "No such config key: #{key}" unless @@config.key?(key)
+
+ @@config[key]
+ end
+
+ def dump_config
+ p @@config
+ end
+ end
+end
diff --git a/lib/autorequire/file.rb b/lib/autorequire/file.rb
new file mode 100644
index 0000000..2a25a07
--- /dev/null
+++ b/lib/autorequire/file.rb
@@ -0,0 +1,33 @@
+module RCM
+ # Managing files
+ class File
+ attr_reader :path
+
+ def initialize(path)
+ @path = path
+ end
+
+ def content(content = nil)
+ content.nil? ? @content : @content = content
+ end
+
+ def to_s
+ @path
+ end
+
+ def do!
+ puts "Evaluating #{self.class}:#{self}"
+ end
+ end
+
+ # Add file keyword to the DSL
+ class RCM
+ def file(path, &block)
+ return unless @conds_met
+
+ f = File.new(path)
+ f.instance_eval(&block)
+ self << f
+ end
+ end
+end
diff --git a/lib/autorequire/only_when.rb b/lib/autorequire/only_when.rb
new file mode 100644
index 0000000..2700bee
--- /dev/null
+++ b/lib/autorequire/only_when.rb
@@ -0,0 +1,37 @@
+module RCM
+ # OnlyWhen (e.g. run on host foo)
+ class OnlyWhen
+ require 'socket'
+
+ def initialize
+ @conds = {}
+ end
+
+ def is(arg)
+ arg
+ end
+
+ def method_missing(method_name, *args, &block)
+ @conds[method_name] = args.first
+ end
+
+ def respond_to_missing?
+ true
+ end
+
+ def met?
+ return false if @conds.key?(:hostname) && Socket.gethostname != @conds[:hostname].to_s
+
+ true
+ end
+ end
+
+ # Add 'only_when' to DSL
+ class RCM
+ def only_when(&block)
+ conds = OnlyWhen.new
+ conds.instance_eval(&block)
+ @conds_met = conds.met?
+ end
+ end
+end
diff --git a/lib/autorequire/options.rb b/lib/autorequire/options.rb
new file mode 100644
index 0000000..de54888
--- /dev/null
+++ b/lib/autorequire/options.rb
@@ -0,0 +1,23 @@
+require 'optparse'
+
+module RCM
+ # Command line options
+ module Options
+ @@options = {
+ verbose: 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 }
+ end.parse!(after_double_dash)
+
+ def option(key)
+ raise "No such option: #{key}" unless @@options.key?(key)
+
+ @@options[key]
+ end
+ end
+end
diff --git a/lib/rcm.rb b/lib/rcm.rb
new file mode 100644
index 0000000..0c3d028
--- /dev/null
+++ b/lib/rcm.rb
@@ -0,0 +1,30 @@
+Dir["#{Dir.pwd}/lib/autorequire/*.rb"].each { |m| require m }
+
+# Ruby Configiration Management system
+module RCM
+
+ # Here all starts
+ class RCM
+ include Config
+ include Options
+
+ def initialize
+ @objs = []
+ @conds_met = true
+ end
+
+ def do!
+ @objs.each(&:do!)
+ end
+
+ def <<(obj)
+ @objs << obj
+ end
+ end
+end
+
+def make_it_so(&block)
+ rcm = RCM::RCM.new
+ rcm.instance_eval(&block)
+ rcm.do!
+end