Skip to content

Commit

Permalink
Cat inherited Head
Browse files Browse the repository at this point in the history
  • Loading branch information
ganmacs committed Aug 30, 2016
1 parent f88c176 commit 182495e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 31 deletions.
42 changes: 14 additions & 28 deletions lib/fluent/command/unpacker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ class Head < Base

def initialize(argv = ARGV)
super
@options.merge!(DEFAULT_HEAD_OPTIONS)
@options.merge!(default_options)
parse_options!
end

Expand All @@ -164,57 +164,43 @@ def call
i = 1
Fluent::MessagePackFactory.unpacker(io).each do |(time, record)|
print @formatter.format(@path, time, record) # tag is use for tag
break if i == @options[:count]
break if i == @options[:count] && @options[:count] != -1
i += 1
end
end
end

private

def default_options
DEFAULT_HEAD_OPTIONS
end

def parse_options!
@opt_parser.on('-n COUNT', 'Set the number of lines to display') do |v|
@options[:count] = v.to_i
usage "illegal line count -- #{@options[:count]}" if @options[:count] < 1
end

super

case
when @argv.empty?
usage 'Path is required'
when @options[:count] < 1
usage "illegal line count -- #{@options[:count]}"
end

usage 'Path is required' if @argv.empty?
@path = @argv.first
usage "#{@path} is not found" unless File.exist?(@path)
end
end

class Cat < Base
include Formattable
class Cat < Head
DEFAULT_CAT_OPTIONS = {
count: -1
}

def initialize(argv = ARGV)
super
parse_options!
end

def call
@formatter = lookup_formatter(@options[:format], @options[:config_params])

File.open(@path, 'r') do |io|
Fluent::MessagePackFactory.unpacker(io).each do |(time, record)|
print @formatter.format(@path, time, record) # @path is used for tag
end
end
end

def parse_options!
super
usage 'Path is required' if @argv.empty?

@path = @argv.first
usage "#{@path} is not found" unless File.exist?(@path)
def default_options
DEFAULT_CAT_OPTIONS
end
end

Expand Down
17 changes: 14 additions & 3 deletions test/command/test_unpacker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -251,11 +251,22 @@ class TestCat < TestBaseCommand
@record = { 'message' => 'dummy' }
end

test 'should output the beginning of the file with default format(out_file)' do
test 'should output the file with default format(out_file)' do
argv = ["#{TMP_DIR}/#{@file_name}"]

timezone do
create_message_packed_file(@file_name, [event_time(@t).to_i], [@record])
create_message_packed_file(@file_name, [event_time(@t).to_i] * 6, [@record] * 6)
head = UnpackerCommand::Cat.new(argv)
out = capture_stdout { head.call }
assert_equal "2011-01-02T13:14:15+00:00\t#{TMP_DIR}/#{@file_name}\t#{Oj.dump(@record)}\n" * 6, out
end
end

test 'should set the number of lines to display' do
argv = ["#{TMP_DIR}/#{@file_name}", '-n', '1']

timezone do
create_message_packed_file(@file_name, [event_time(@t).to_i] * 6, [@record] * 6)
head = UnpackerCommand::Cat.new(argv)
out = capture_stdout { head.call }
assert_equal "2011-01-02T13:14:15+00:00\t#{TMP_DIR}/#{@file_name}\t#{Oj.dump(@record)}\n", out
Expand Down Expand Up @@ -291,7 +302,7 @@ class TestCat < TestBaseCommand
argv = ["#{TMP_DIR}/#{file_name}", '--format=csv', '-e', 'fields=message,fo', '-e', 'delimiter=|']
create_message_packed_file(file_name, [event_time], [{ 'message' => 'dummy', 'fo' => 'dummy2' }])

head = UnpackerCommand::Head.new(argv)
head = UnpackerCommand::Cat.new(argv)
assert_equal "\"dummy\"|\"dummy2\"\n", capture_stdout { head.call }
end
end
Expand Down

0 comments on commit 182495e

Please sign in to comment.