- 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:
Jordan Sissel 2011-02-14 22:24:49 -08:00
parent b025425e73
commit 9c9932bebe
4 changed files with 61 additions and 13 deletions

View file

@ -23,20 +23,59 @@ module LogStash::Config
# This method is called when someone does 'include LogStash::Config' # This method is called when someone does 'include LogStash::Config'
def self.included(base) def self.included(base)
# Add ClassMethods module methods to the 'base' given. # Add ClassMethods module methods to the 'base' given.
base.extend(ClassMethods) base.extend(LogStash::Config::DSL)
end end
module ClassMethods module DSL
def section(name) attr_accessor :dsl_name
@section = name attr_accessor :dsl_parent
end # def self.section
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" } # cfg should be hash with one entry of { "key" => "val" }
@dsl_config ||= Hash.new
key, value = cfg.to_a.first key, value = cfg.to_a.first
puts "#{@section} {" @dsl_config[key] = value
puts " #{key} => #{value}" end # def config
puts "}"
end # def self.config def dsl_gen
end # module ClassMethods 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 end # module LogStash::Config

View file

@ -8,6 +8,9 @@ require "cgi"
class LogStash::Inputs::Amqp < LogStash::Inputs::Base class LogStash::Inputs::Amqp < LogStash::Inputs::Base
MQTYPES = [ "fanout", "queue", "topic" ] MQTYPES = [ "fanout", "queue", "topic" ]
dsl_name "amqp"
dsl_config "pantscon" => :string #LogStash::Config::Path
public public
def initialize(url, type, config={}, &block) def initialize(url, type, config={}, &block)
super super

View file

@ -8,9 +8,11 @@ class LogStash::Inputs::Base
include LogStash::Config include LogStash::Config
attr_accessor :logger attr_accessor :logger
dsl_name "input"
dsl_parent nil
# Define the basic config # Define the basic config
config "path" => :string #LogStash::Config::Path dsl_config "path" => :string #LogStash::Config::Path
config "tag" => :string #LogStash::Config::Array dsl_config "tag" => :string #LogStash::Config::Array
public public
def initialize(configs, output_queue) def initialize(configs, output_queue)

View file

@ -4,6 +4,10 @@ require "logstash/namespace"
require "socket" # for Socket.gethostname require "socket" # for Socket.gethostname
class LogStash::Inputs::File < LogStash::Inputs::Base class LogStash::Inputs::File < LogStash::Inputs::Base
dsl_name "file"
#dsl_parent LogStash::Inputs::Base
public public
def initialize(configs, output_queue) def initialize(configs, output_queue)
super super