diff --git a/lib/fluent/command/plugin_config_formatter.rb b/lib/fluent/command/plugin_config_formatter.rb index a23657391a..af39ea7c0d 100644 --- a/lib/fluent/command/plugin_config_formatter.rb +++ b/lib/fluent/command/plugin_config_formatter.rb @@ -24,7 +24,7 @@ class FluentPluginConfigFormatter - AVAILABLE_FORMATS = [:txt, :markdown] + AVAILABLE_FORMATS = [:txt, :markdown, :json] def initialize(argv = ARGV) @argv = argv @@ -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 @@ -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 diff --git a/lib/fluent/config/configure_proxy.rb b/lib/fluent/config/configure_proxy.rb index d649b5e6d2..59e4af0853 100644 --- a/lib/fluent/config/configure_proxy.rb +++ b/lib/fluent/config/configure_proxy.rb @@ -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)