summaryrefslogtreecommitdiff
path: root/lib/dslkeywords/agent.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/dslkeywords/agent.rb')
-rw-r--r--lib/dslkeywords/agent.rb45
1 files changed, 45 insertions, 0 deletions
diff --git a/lib/dslkeywords/agent.rb b/lib/dslkeywords/agent.rb
new file mode 100644
index 0000000..6632835
--- /dev/null
+++ b/lib/dslkeywords/agent.rb
@@ -0,0 +1,45 @@
+# frozen_string_literal: true
+
+require_relative 'keyword'
+
+module RCM
+ # Stores a named shell command template for agent-backed file processing.
+ class AgentDefinition < Keyword
+ attr_reader :name
+
+ class InvalidName < StandardError; end
+
+ def self.id_for(name) = super(normalize_name(name))
+
+ def self.normalize_name(name)
+ normalized = name.to_s.strip.gsub(/\s+/, ' ')
+ raise InvalidName, 'Agent name must not be empty' if normalized.empty?
+
+ normalized
+ end
+
+ def initialize(name)
+ @name = self.class.normalize_name(name)
+ super(@name)
+ end
+
+ def command(text = nil)
+ return @command if text.nil?
+
+ @command = text.to_s
+ end
+ end
+
+ # Adds the `agent` definition keyword to the top-level DSL.
+ class DSL
+ def agent(name = nil, &block)
+ return name if name.nil?
+ return unless @conds_met
+
+ definition = AgentDefinition.new(name)
+ definition.dsl = self
+ definition.command(definition.instance_eval(&block)) if block
+ register(definition, schedule: false, duplicate_error: DuplicateDefinition)
+ end
+ end
+end