mirror of
https://github.com/elastic/logstash.git
synced 2025-04-24 14:47:19 -04:00
- Make configs inheritable and other goodies in this prototype
Example: % RUBYLIB=lib ruby -rubygems -e 'require "logstash/config"; require "logstash/inputs/file"; require "logstash/inputs/amqp"; LogStash::Inputs::Amqp.dsl_gen; LogStash::Inputs::File.dsl_gen' input { #parent amqp { #node "somename": tag => string, path => string, pantscon => string, } #node } #parent input { #parent file { #node "somename": tag => string, path => string, } #node } #parent
This commit is contained in:
parent
b025425e73
commit
9c9932bebe
4 changed files with 61 additions and 13 deletions
|
@ -23,20 +23,59 @@ module LogStash::Config
|
|||
# This method is called when someone does 'include LogStash::Config'
|
||||
def self.included(base)
|
||||
# Add ClassMethods module methods to the 'base' given.
|
||||
base.extend(ClassMethods)
|
||||
base.extend(LogStash::Config::DSL)
|
||||
end
|
||||
|
||||
module ClassMethods
|
||||
def section(name)
|
||||
@section = name
|
||||
end # def self.section
|
||||
module DSL
|
||||
attr_accessor :dsl_name
|
||||
attr_accessor :dsl_parent
|
||||
|
||||
def config(cfg)
|
||||
# Set the parent config for this class.
|
||||
def dsl_parent(*args)
|
||||
@dsl_parent = args[0] if args.length > 0
|
||||
return @dsl_parent
|
||||
end
|
||||
|
||||
# Set the config name for this class.
|
||||
def dsl_name(*args)
|
||||
@dsl_name = args[0] if args.length > 0
|
||||
return @dsl_name
|
||||
end
|
||||
|
||||
def dsl_config(cfg)
|
||||
# cfg should be hash with one entry of { "key" => "val" }
|
||||
@dsl_config ||= Hash.new
|
||||
key, value = cfg.to_a.first
|
||||
puts "#{@section} {"
|
||||
puts " #{key} => #{value}"
|
||||
puts "}"
|
||||
end # def self.config
|
||||
end # module ClassMethods
|
||||
@dsl_config[key] = value
|
||||
end # def config
|
||||
|
||||
def dsl_gen
|
||||
puts "#{@dsl_parent.dsl_name} { #parent" if @dsl_parent
|
||||
config = []
|
||||
config << "#{@dsl_name} { #node"
|
||||
config << " \"somename\":"
|
||||
attrs = []
|
||||
(@dsl_config || Hash.new).each do |key, value|
|
||||
attrs << " #{key} => #{value},"
|
||||
end
|
||||
config += attrs
|
||||
config << "} #node"
|
||||
config = config.collect { |p| "#{@dsl_parent.nil? ? "" : " "}#{p}" }
|
||||
puts config.join("\n")
|
||||
puts "} #parent" if @dsl_parent
|
||||
end
|
||||
|
||||
def inherited(subclass)
|
||||
# Copy our parent's config to a subclass.
|
||||
# This method is invoked whenever someone subclasses us, like:
|
||||
# class Foo < Bar ...
|
||||
config = Hash.new
|
||||
@dsl_config.each do |key, val|
|
||||
#puts "#{self}: Sharing config '#{key}' with subclass #{subclass}"
|
||||
config[key] = val
|
||||
end
|
||||
subclass.instance_variable_set("@dsl_config", config)
|
||||
subclass.dsl_parent = self
|
||||
end # def inherited
|
||||
end # module LogStash::Config::DSL
|
||||
end # module LogStash::Config
|
||||
|
|
|
@ -8,6 +8,9 @@ require "cgi"
|
|||
class LogStash::Inputs::Amqp < LogStash::Inputs::Base
|
||||
MQTYPES = [ "fanout", "queue", "topic" ]
|
||||
|
||||
dsl_name "amqp"
|
||||
dsl_config "pantscon" => :string #LogStash::Config::Path
|
||||
|
||||
public
|
||||
def initialize(url, type, config={}, &block)
|
||||
super
|
||||
|
|
|
@ -8,9 +8,11 @@ class LogStash::Inputs::Base
|
|||
include LogStash::Config
|
||||
attr_accessor :logger
|
||||
|
||||
dsl_name "input"
|
||||
dsl_parent nil
|
||||
# Define the basic config
|
||||
config "path" => :string #LogStash::Config::Path
|
||||
config "tag" => :string #LogStash::Config::Array
|
||||
dsl_config "path" => :string #LogStash::Config::Path
|
||||
dsl_config "tag" => :string #LogStash::Config::Array
|
||||
|
||||
public
|
||||
def initialize(configs, output_queue)
|
||||
|
|
|
@ -4,6 +4,10 @@ require "logstash/namespace"
|
|||
require "socket" # for Socket.gethostname
|
||||
|
||||
class LogStash::Inputs::File < LogStash::Inputs::Base
|
||||
|
||||
dsl_name "file"
|
||||
#dsl_parent LogStash::Inputs::Base
|
||||
|
||||
public
|
||||
def initialize(configs, output_queue)
|
||||
super
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue