- 'eval' wasn't working well on 'config :foo ...' lines that used

in-scope constants/variables.

  Switched to using monkeypatching the 'config' method instead.

  There's still slight bugs, like the 'charset'  on all inputs has no
  validation nor default listed.
This commit is contained in:
Jordan Sissel 2012-12-31 16:40:58 -08:00
parent 5e9295b20e
commit 8cbb10bb4a

View file

@ -3,15 +3,13 @@ require "erb"
require "optparse"
require "bluecloth" # for markdown parsing
# TODO(sissel): Currently this doc generator doesn't follow ancestry, so
# LogStash::Input::Amqp inherits Base, but we don't parse the base file.
# We need this, though.
#
# TODO(sissel): Convert this to use ERB, not random bits of 'puts'
$: << Dir.pwd
$: << File.join(File.dirname(__FILE__), "..", "lib")
require "logstash/config/mixin"
require "logstash/inputs/base"
require "logstash/filters/base"
require "logstash/outputs/base"
require "logstash/version"
class LogStashConfigDocGenerator
@ -28,9 +26,6 @@ class LogStashConfigDocGenerator
/^ *flag[( ].*/ => lambda { |m| add_flag(m[0]) },
/^ *(class|def|module) / => lambda { |m| clear_comments },
}
@comments = []
@settings = {}
@flags = {}
end
def parse(string)
@ -69,24 +64,24 @@ class LogStashConfigDocGenerator
end # def add_comment
def add_config(code)
# trim off any possible multiline lamdas for a :validate
code.sub!(/, *:validate => \(lambda.*$/, '')
# I just care about the 'config :name' part
code = code.sub(/,.*/, "")
# call the code, which calls 'config' in this class.
# This will let us align comments with config options.
name, opts = eval(code)
description = BlueCloth.new(@comments.join("\n")).to_html
@settings[name] = opts.merge(:description => description)
@attributes[name][:description] = description
clear_comments
end # def add_config
def add_flag(code)
# call the code, which calls 'config' in this class.
# This will let us align comments with config options.
p :code => code
#p :code => code
fixed_code = code.gsub(/ do .*/, "")
p :fixedcode => fixed_code
#p :fixedcode => fixed_code
name, description = eval(fixed_code)
@flags[name] = description
clear_comments
@ -129,15 +124,27 @@ class LogStashConfigDocGenerator
end # def clear_comments
def generate(file, settings)
require "logstash/inputs/base"
require "logstash/filters/base"
require "logstash/outputs/base"
require file
@comments = []
@settings = {}
@class_description = ""
@plugin_status = ""
@comments = []
@attributes = Hash.new { |h,k| h[k] = {} }
@flags = {}
# local scoping for the monkeypatch belowg
attributes = @attributes
# Monkeypatch the 'config' method to capture
# Note, this monkeypatch requires us do the config processing
# one at a time.
LogStash::Config::Mixin::DSL.instance_eval do
define_method(:config) do |name, opts={}|
#p name => opts
attributes[name].merge!(opts)
end
end
# Loading the file will trigger the config dsl which should
# collect all the config settings.
load file
# parse base first
parse(File.new(File.join(File.dirname(file), "base.rb"), "r").read)
@ -174,7 +181,7 @@ class LogStashConfigDocGenerator
# descriptions are assumed to be markdown
description = BlueCloth.new(@class_description).to_html
sorted_settings = @settings.sort { |a,b| a.first.to_s <=> b.first.to_s }
sorted_attributes = @attributes.sort { |a,b| a.first.to_s <=> b.first.to_s }
klassname = LogStash::Config::Registry.registry[@name].to_s
name = @name