From 07ea7befb8050baecb1593e90cd8c4571119ff6e Mon Sep 17 00:00:00 2001 From: uu59 Date: Mon, 12 May 2014 14:50:30 +0900 Subject: [PATCH] Add primitive Fluentd::Configuration class --- app/models/fluentd.rb | 2 +- app/models/fluentd/configuration.rb | 33 +++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 app/models/fluentd/configuration.rb diff --git a/app/models/fluentd.rb b/app/models/fluentd.rb index 6cc770b..91e8e01 100644 --- a/app/models/fluentd.rb +++ b/app/models/fluentd.rb @@ -92,7 +92,7 @@ class Fluentd end def config - File.read config_file # TODO: Use Fluent::Engine or Fluent::V1Config + ::Fluentd::Configuration.new(config_file) end private diff --git a/app/models/fluentd/configuration.rb b/app/models/fluentd/configuration.rb new file mode 100644 index 0000000..bd18cea --- /dev/null +++ b/app/models/fluentd/configuration.rb @@ -0,0 +1,33 @@ +require 'fluent/config/v1_parser' + +class Fluentd + class Configuration + include Enumerable + + attr_reader :file + + def initialize(config_file) + @file = config_file + end + + def config + @config ||= ::Fluent::Config::V1Parser.read(file) + end + + def to_s + config.to_s.gsub(/\A\n/, "").gsub(/<\/ROOT>\n\z/, "").gsub(/^ {2}/, "") + end + + def each(&block) + config.each_element(&block) + end + + def sources + find_all{|e| e.name == "source"} + end + + def matches + find_all{|e| e.name == "match"} + end + end +end