Kouhei Sutou
null+****@clear*****
Mon Jan 15 12:13:19 JST 2018
Kouhei Sutou 2018-01-15 12:13:19 +0900 (Mon, 15 Jan 2018) New Revision: de1ec58a1deabf94f9223e7d5d53c5150ab040c8 https://github.com/groonga/groonga-query-log/commit/de1ec58a1deabf94f9223e7d5d53c5150ab040c8 Message: Clean common command line codes Modified files: lib/groonga-query-log/command/analyze-load.rb lib/groonga-query-log/command/analyzer.rb lib/groonga-query-log/command/extract.rb Renamed files: lib/groonga-query-log/command-line.rb (from lib/groonga-query-log/command-line-utils.rb) Renamed: lib/groonga-query-log/command-line.rb (+18 -3) 67% =================================================================== --- lib/groonga-query-log/command-line-utils.rb 2018-01-15 11:51:28 +0900 (c591461) +++ lib/groonga-query-log/command-line.rb 2018-01-15 12:13:19 +0900 (5a67d21) @@ -15,9 +15,24 @@ # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA module GroongaQueryLog - module CommandLineUtils - def log_via_stdin? - stdin_with_pipe? or stdin_with_redirect? + class CommandLine + class Error < StandardError + end + + class NoInputError < Error + end + + private + def parse_log(parser, log_paths, &process_statistic) + if log_paths.empty? + if stdin_with_pipe? or stdin_with_redirect? + parser.parse($stdin, &process_statistic) + else + raise NoInputError, "Error: Please specify input log files." + end + else + parser.parse_paths(log_paths, &process_statistic) + end end def stdin_with_pipe? Modified: lib/groonga-query-log/command/analyze-load.rb (+3 -18) =================================================================== --- lib/groonga-query-log/command/analyze-load.rb 2018-01-15 11:51:28 +0900 (a874f25) +++ lib/groonga-query-log/command/analyze-load.rb 2018-01-15 12:13:19 +0900 (a08c6fd) @@ -17,19 +17,11 @@ require "optparse" require "groonga-query-log" -require "groonga-query-log/command-line-utils" +require "groonga-query-log/command-line" module GroongaQueryLog module Command - class AnalyzeLoad - include CommandLineUtils - - class Error < StandardError - end - - class NoInputError < Error - end - + class AnalyzeLoad < CommandLine def initialize setup_options @pending_entry = nil @@ -106,14 +98,7 @@ module GroongaQueryLog def parse(log_paths, &process_statistic) parser = Parser.new(@options) - if log_paths.empty? - unless log_via_stdin? - raise(NoInputError, "Error: Please specify input log files.") - end - parser.parse($stdin, &process_statistic) - end - - parser.parse_paths(log_paths, &process_statistic) + parse_log(parser, log_paths, &process_statistic) end def report_statistic(output, statistic) Modified: lib/groonga-query-log/command/analyzer.rb (+11 -25) =================================================================== --- lib/groonga-query-log/command/analyzer.rb 2018-01-15 11:51:28 +0900 (d48ad56) +++ lib/groonga-query-log/command/analyzer.rb 2018-01-15 12:13:19 +0900 (0785245) @@ -19,26 +19,11 @@ require "optparse" require "json" require "groonga-query-log" -require "groonga-query-log/command-line-utils" -require "groonga-query-log/command/analyzer/streamer" -require "groonga-query-log/command/analyzer/sized-statistics" -require "groonga-query-log/command/analyzer/reporter/console" -require "groonga-query-log/command/analyzer/reporter/csv" -require "groonga-query-log/command/analyzer/reporter/html" -require "groonga-query-log/command/analyzer/reporter/json" -require "groonga-query-log/command/analyzer/reporter/json-stream" +require "groonga-query-log/command-line" module GroongaQueryLog module Command - class Analyzer - include CommandLineUtils - - class Error < StandardError - end - - class NoInputError < Error - end - + class Analyzer < CommandLine class UnsupportedReporter < Error end @@ -274,15 +259,16 @@ module GroongaQueryLog def parse(log_paths, &process_statistic) parser = Parser.new(@options) - if log_paths.empty? - unless log_via_stdin? - raise(NoInputError, "Error: Please specify input log files.") - end - parser.parse($stdin, &process_statistic) - end - - parser.parse_paths(log_paths, &process_statistic) + parse_log(parser, log_paths, &process_statistic) end end end end + +require "groonga-query-log/command/analyzer/streamer" +require "groonga-query-log/command/analyzer/sized-statistics" +require "groonga-query-log/command/analyzer/reporter/console" +require "groonga-query-log/command/analyzer/reporter/csv" +require "groonga-query-log/command/analyzer/reporter/html" +require "groonga-query-log/command/analyzer/reporter/json" +require "groonga-query-log/command/analyzer/reporter/json-stream" Modified: lib/groonga-query-log/command/extract.rb (+21 -44) =================================================================== --- lib/groonga-query-log/command/extract.rb 2018-01-15 11:51:28 +0900 (e85c024) +++ lib/groonga-query-log/command/extract.rb 2018-01-15 12:13:19 +0900 (b7ec8e7) @@ -1,4 +1,4 @@ -# Copyright (C) 2011-2017 Kouhei Sutou <kou �� clear-code.com> +# Copyright (C) 2011-2018 Kouhei Sutou <kou �� clear-code.com> # Copyright (C) 2012 Haruka Yoshihara <yoshihara �� clear-code.com> # # This library is free software; you can redistribute it and/or @@ -19,16 +19,11 @@ require "optparse" require "pathname" require "groonga-query-log" -require "groonga-query-log/command-line-utils" +require "groonga-query-log/command-line" module GroongaQueryLog module Command - class Extract - include CommandLineUtils - - class Error < StandardError - end - + class Extract < CommandLine attr_accessor :options attr_reader :option_parser @@ -61,22 +56,12 @@ module GroongaQueryLog return false end - if log_paths.empty? - unless log_via_stdin? - $stderr.puts("Error: Please specify input log files.") - return false - end - log = $stdin - else - log = log_paths - end - if****@optio*****_path File.open(@options.output_path, "w") do |output| - extract(log, output) + extract(log_paths, output) end else - extract(log, $stdout) + extract(log_paths, $stdout) end true @@ -138,34 +123,26 @@ module GroongaQueryLog end end - def extract(log, output) - if log.instance_of?(Array) - log.each do |log_path| - File.open(log_path) do |log_file| - extract_command(log_file, output) - end - end - else - extract_command(log, output) + def extract(log_paths, output) + parser = Parser.new + parse_log(parser, log_paths) do |statistic| + extract_command(statistic, output) end end - def extract_command(log, output) - parser = Parser.new - parser.parse(log) do |statistic| - command = statistic.command - next unless target?(command) - command_text = nil - case****@optio*****_format - when "uri" - command_text = command.to_uri_format - when "command" - command_text = command.to_command_format - else - command_text = statistic.raw_command - end - output.puts(command_text) + def extract_command(statistic, output) + command = statistic.command + return unless target?(command) + command_text = nil + case****@optio*****_format + when "uri" + command_text = command.to_uri_format + when "command" + command_text = command.to_command_format + else + command_text = statistic.raw_command end + output.puts(command_text) end def target?(command) -------------- next part -------------- HTML����������������������������... URL: https://lists.osdn.me/mailman/archives/groonga-commit/attachments/20180115/d6f89771/attachment-0001.htm