-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
module Fluentd | ||
module Config | ||
module DSL | ||
module DSLParser | ||
def self.read(path) | ||
path = File.expand_path(path) | ||
data = File.read(path) | ||
parse(data, path) | ||
end | ||
|
||
def self.parse(source, source_path="config.rb") | ||
DSLElement.new('ROOT', nil).instance_eval(source, source_path).__config_element | ||
end | ||
|
||
end | ||
|
||
class DSLElement | ||
attr_accessor :name, :arg, :attrs, :elements | ||
|
||
def initialize(name, arg) | ||
@name = name | ||
@arg = arg | ||
@attrs = {} | ||
@elements = [] | ||
end | ||
|
||
def __config_element | ||
Fluentd::Config::Element.new(@name, @arg, @attrs, @elements) | ||
end | ||
|
||
def method_missing(name, *args) | ||
raise ArgumentError, "Configuration DSL Syntax Error: only one argument allowed" if args.size > 1 | ||
value = args.first | ||
@attrs[name.to_s] = value.is_a?(Symbol) ? value.to_s : value | ||
self | ||
end | ||
|
||
def __element(name, arg, block) | ||
raise ArgumentError, "#{name} block must be specified" if block.nil? | ||
element = DSLElement.new(name, arg) | ||
element.instance_exec(&block) | ||
@elements.push(element.__config_element) | ||
self | ||
end | ||
|
||
def __need_arg(name, args) | ||
raise ArgumentError, "#{name} block requires arguments for match pattern" if args.nil? || args.size != 1 | ||
true | ||
end | ||
|
||
def worker(&block); __element('worker', nil, block); end | ||
def source(&block); __element('source', nil, block); end | ||
def filter(*args, &block); __need_arg('filter', args); __element('filter', args.first, block); end | ||
def match(*args, &block); __need_arg('match', args); __element('match', args.first, block); end | ||
def label(*args, &block); __need_arg('label', args); __element('label', args.first, block); end | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
yuya-takeyama
|
||
end | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
require_relative "./helper" | ||
|
||
require 'fluentd/config/element' | ||
require "fluentd/config/dsl" | ||
|
||
DSL_CONFIG_EXAMPLE = %q[ | ||
worker { | ||
hostname = "myhostname" | ||
(0..9).each { |i| | ||
source { | ||
type :tail | ||
path "/var/log/httpd/access.part#{i}.log" | ||
filter ('bar.**') { | ||
type :hoge | ||
val1 "moge" | ||
val2 ["foo", "bar", "baz"] | ||
val3 10 | ||
id :hoge | ||
} | ||
filter ('foo.**') { | ||
type "pass" | ||
} | ||
match ('{foo,bar}.**') { | ||
type "file" | ||
path "/var/log/httpd/access.#{hostname}.#{i}.log" | ||
} | ||
} | ||
} | ||
} | ||
] | ||
|
||
describe Fluentd::Config::DSL::DSLParser do | ||
include_context 'config_helper' | ||
|
||
def parse_dsl(text) | ||
Fluentd::Config::DSL::DSLParser.parse(text) | ||
end | ||
|
||
def e(name, arg='', attrs={}, elements=[]) | ||
Fluentd::Config::Element.new(name, arg, attrs, elements) | ||
end | ||
|
||
describe '.parse' do | ||
it 'can parse simple dsl style configuration' do | ||
root = parse_dsl(DSL_CONFIG_EXAMPLE) | ||
expect(root.name).to eql('ROOT') | ||
expect(root.arg).to be_nil | ||
expect(root.keys.size).to eql(0) | ||
expect(root.elements.size).to eql(1) | ||
|
||
worker = root.elements.first | ||
expect(worker.name).to eql('worker') | ||
expect(worker.arg).to be_nil | ||
expect(worker.keys.size).to eql(0) | ||
expect(worker.elements.size).to eql(10) | ||
|
||
ele4 = worker.elements[4] | ||
expect(ele4.name).to eql('source') | ||
expect(ele4.arg).to be_nil | ||
expect(ele4.keys.size).to eql(2) | ||
expect(ele4['type']).to eql('tail') | ||
expect(ele4['path']).to eql("/var/log/httpd/access.part4.log") | ||
end | ||
end | ||
|
||
end |
3 comments
on commit 01cdc5f
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great job 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fantastic 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome 👍
What is the reason to use name mangling rather than
private
keyword ......???