From 182495ec3ec53f0575d19de27a12dce53792013c Mon Sep 17 00:00:00 2001 From: ganmacs Date: Tue, 30 Aug 2016 19:22:32 +0900 Subject: [PATCH] Cat inherited Head --- lib/fluent/command/unpacker.rb | 42 ++++++++++++---------------------- test/command/test_unpacker.rb | 17 +++++++++++--- 2 files changed, 28 insertions(+), 31 deletions(-) diff --git a/lib/fluent/command/unpacker.rb b/lib/fluent/command/unpacker.rb index 74b945d01f..f6ab338ed9 100644 --- a/lib/fluent/command/unpacker.rb +++ b/lib/fluent/command/unpacker.rb @@ -153,7 +153,7 @@ class Head < Base def initialize(argv = ARGV) super - @options.merge!(DEFAULT_HEAD_OPTIONS) + @options.merge!(default_options) parse_options! end @@ -164,7 +164,7 @@ 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 @@ -172,49 +172,35 @@ def call 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 diff --git a/test/command/test_unpacker.rb b/test/command/test_unpacker.rb index e240b8a6de..c3499fa2f1 100644 --- a/test/command/test_unpacker.rb +++ b/test/command/test_unpacker.rb @@ -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 @@ -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