From 9abea6c57e018f6033dfa8ffc6cdcdfc86ece969 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Fri, 6 Dec 2024 13:16:39 +0200 Subject: fix options --- Rakefile | 8 +++----- rcm/conditions.rb | 37 ------------------------------------- rcm/only_when.rb | 37 +++++++++++++++++++++++++++++++++++++ rcm/options.rb | 11 +++++------ rcm/rcm.rb | 4 ++-- 5 files changed, 47 insertions(+), 50 deletions(-) delete mode 100644 rcm/conditions.rb create mode 100644 rcm/only_when.rb diff --git a/Rakefile b/Rakefile index f41f084..5b2ec0e 100644 --- a/Rakefile +++ b/Rakefile @@ -1,12 +1,10 @@ require_relative 'rcm/rcm' desc 'Set up wireguard mesh' -task :wireguard do |t| - rcm do +task :wireguard do + make_it_so do p option :verbose - conditions do - hostname is :earth - end + only_when { hostname is :earth } file '/etc/wg/wg0.conf' do content 'the content' diff --git a/rcm/conditions.rb b/rcm/conditions.rb deleted file mode 100644 index f90c74c..0000000 --- a/rcm/conditions.rb +++ /dev/null @@ -1,37 +0,0 @@ -module RCM - # Conditions (e.g. run on host foo) - class Conditions - 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 conditions "keyword" to the DSL - class RCM - def conditions(&block) - conds = Conditions.new - conds.instance_eval(&block) - @conds_met = conds.met? - end - end -end diff --git a/rcm/only_when.rb b/rcm/only_when.rb new file mode 100644 index 0000000..2700bee --- /dev/null +++ b/rcm/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/rcm/options.rb b/rcm/options.rb index ab3e25d..de54888 100644 --- a/rcm/options.rb +++ b/rcm/options.rb @@ -7,13 +7,12 @@ module RCM verbose: false } - parser = OptionParser.new do |opts| - opts.banner = 'Usage: rake [target] -- [options]' - opts.on('-v', '--[no-]verbose', 'run verbosely') { |v| @@options[:verbose] = v } - end + after_double_dash = ARGV.slice_before('--').to_a.last.drop(1) - parser.order!(ARGV) {} - parser.parse! + 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) diff --git a/rcm/rcm.rb b/rcm/rcm.rb index 5c2e71e..3724a88 100644 --- a/rcm/rcm.rb +++ b/rcm/rcm.rb @@ -1,5 +1,5 @@ require_relative 'options' -require_relative 'conditions' +require_relative 'only_when' require_relative 'file' # Ruby Configiration Management system @@ -23,7 +23,7 @@ module RCM end end -def rcm(&block) +def make_it_so(&block) rcm = RCM::RCM.new rcm.instance_eval(&block) rcm.do! -- cgit v1.2.3