From bfdcad7efca071374de0be3124dcc92deeab681e Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Fri, 6 Dec 2024 22:33:07 +0200 Subject: auto require --- Rakefile | 2 +- lib/autorequire/config.rb | 18 ++++++++++++++++++ lib/autorequire/file.rb | 33 +++++++++++++++++++++++++++++++++ lib/autorequire/only_when.rb | 37 +++++++++++++++++++++++++++++++++++++ lib/autorequire/options.rb | 23 +++++++++++++++++++++++ lib/rcm.rb | 30 ++++++++++++++++++++++++++++++ rcm/config.rb | 18 ------------------ rcm/file.rb | 33 --------------------------------- rcm/only_when.rb | 37 ------------------------------------- rcm/options.rb | 23 ----------------------- rcm/rcm.rb | 32 -------------------------------- 11 files changed, 142 insertions(+), 144 deletions(-) create mode 100644 lib/autorequire/config.rb create mode 100644 lib/autorequire/file.rb create mode 100644 lib/autorequire/only_when.rb create mode 100644 lib/autorequire/options.rb create mode 100644 lib/rcm.rb delete mode 100644 rcm/config.rb delete mode 100644 rcm/file.rb delete mode 100644 rcm/only_when.rb delete mode 100644 rcm/options.rb delete mode 100644 rcm/rcm.rb diff --git a/Rakefile b/Rakefile index 2935920..5122919 100644 --- a/Rakefile +++ b/Rakefile @@ -1,4 +1,4 @@ -require_relative 'rcm/rcm' +require_relative 'lib/rcm' desc 'Set up wireguard mesh' task :wireguard do 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 diff --git a/rcm/config.rb b/rcm/config.rb deleted file mode 100644 index b9dc5ac..0000000 --- a/rcm/config.rb +++ /dev/null @@ -1,18 +0,0 @@ -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/rcm/file.rb b/rcm/file.rb deleted file mode 100644 index 2a25a07..0000000 --- a/rcm/file.rb +++ /dev/null @@ -1,33 +0,0 @@ -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/rcm/only_when.rb b/rcm/only_when.rb deleted file mode 100644 index 2700bee..0000000 --- a/rcm/only_when.rb +++ /dev/null @@ -1,37 +0,0 @@ -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/rcm/options.rb b/rcm/options.rb deleted file mode 100644 index de54888..0000000 --- a/rcm/options.rb +++ /dev/null @@ -1,23 +0,0 @@ -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/rcm/rcm.rb b/rcm/rcm.rb deleted file mode 100644 index 43c016b..0000000 --- a/rcm/rcm.rb +++ /dev/null @@ -1,32 +0,0 @@ -require_relative 'config' -require_relative 'options' -require_relative 'only_when' -require_relative 'file' - -# 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 -- cgit v1.2.3