Skip to content

Commit

Permalink
Clean common command line codes
Browse files Browse the repository at this point in the history
  • Loading branch information
kou committed Jan 15, 2018
1 parent f81be0f commit de1ec58
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 90 deletions.
Expand Up @@ -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?
Expand Down
21 changes: 3 additions & 18 deletions lib/groonga-query-log/command/analyze-load.rb
Expand Up @@ -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
Expand Down Expand Up @@ -106,14 +98,7 @@ def open_output

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)
Expand Down
36 changes: 11 additions & 25 deletions lib/groonga-query-log/command/analyzer.rb
Expand Up @@ -19,26 +19,11 @@
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

Expand Down Expand Up @@ -274,15 +259,16 @@ def create_reporter(statistics)

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"
65 changes: 21 additions & 44 deletions lib/groonga-query-log/command/extract.rb
@@ -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
Expand All @@ -19,16 +19,11 @@
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

Expand Down Expand Up @@ -61,22 +56,12 @@ def run(arguments)
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 @options.output_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
Expand Down Expand Up @@ -138,34 +123,26 @@ def setup_options
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 @options.unify_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 @options.unify_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)
Expand Down

0 comments on commit de1ec58

Please sign in to comment.