Skip to content

Commit

Permalink
Support JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
okkez committed Jan 24, 2017
1 parent 94e848b commit 8e151ec
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 10 deletions.
46 changes: 36 additions & 10 deletions lib/fluent/command/plugin_config_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

class FluentPluginConfigFormatter

AVAILABLE_FORMATS = [:txt, :markdown]
AVAILABLE_FORMATS = [:txt, :markdown, :json]

def initialize(argv = ARGV)
@argv = argv
Expand All @@ -42,15 +42,29 @@ def initialize(argv = ARGV)
def call
parse_options!
init_engine
plugin = Fluent::Plugin.__send__("new_#{@plugin_type}", @plugin_name)
if @format == :markdown
helpers = "### Plugin_helpers\n\n"
plugin.class.plugin_helpers.each do |helper|
helpers << "* #{helper}\n"
end
puts "#{helpers}\n"
@plugin = Fluent::Plugin.__send__("new_#{@plugin_type}", @plugin_name)
@plugin_helpers = @plugin.class.plugin_helpers
__send__("dump_#{@format}")
end

private

def dump_txt
puts "helpers: #{@plugin_helpers.join(',')}"
dump_body
end

def dump_markdown
helpers = "### Plugin_helpers\n\n"
@plugin_helpers.each do |helper|
helpers << "* #{helper}\n"
end
plugin.class.ancestors.reverse_each do |plugin_class|
puts "#{helpers}\n"
dump_body
end

def dump_body
@plugin.class.ancestors.reverse_each do |plugin_class|
next unless plugin_class.respond_to?(:dump)
next if plugin_class == Fluent::Plugin::Base
unless @verbose
Expand All @@ -61,7 +75,19 @@ def call
end
end

private
def dump_json
dumped_config = {}
dumped_config[:plugin_helpers] = @plugin_helpers
@plugin.class.ancestors.reverse_each do |plugin_class|
next unless plugin_class.respond_to?(:dump)
next if plugin_class == Fluent::Plugin::Base
unless @verbose
next if plugin_class.name =~ /::PluginHelper::/
end
dumped_config[plugin_class.name] = plugin_class.dump(0, @options)
end
puts dumped_config.to_json
end

def usage(message = nil)
puts @paser.to_s
Expand Down
24 changes: 24 additions & 0 deletions lib/fluent/config/configure_proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,30 @@ def dump_markdown(level = 0, options = {})
dumped_config
end

def dump_json(level = 0, options = {})
dumped_config = {}
@params.each do |name, config|
dumped_config[name] = {
type: config[1][:type],
required: required,
default: @defaults[name],
deprecated: config[1][:deprecated],
obsoleted: config[1][:obsoleted]
}
if config[1][:type] == :enum
dumped_config[name][:list] = config[1][:list]
end
end
@sections.each do |section_name, sub_proxy|
if dumped_config.key?(section_name)
dumped_config[section_name].merge(sub_proxy.dump(level + 1, options))
else
dumped_config[section_name] = sub_proxy.dump(level + 1, options)
end
end
dumped_config
end

private

def overwrite?(other, attribute_name)
Expand Down

0 comments on commit 8e151ec

Please sign in to comment.