[Groonga-commit] ranguba/groonga-client at 468e196 [master] Extract common code

Back to archive index

Kouhei Sutou null+****@clear*****
Fri Oct 27 17:58:52 JST 2017


Kouhei Sutou	2017-10-27 17:58:52 +0900 (Fri, 27 Oct 2017)

  New Revision: 468e19632946a049e2518048f2d50333bb03801f
  https://github.com/ranguba/groonga-client/commit/468e19632946a049e2518048f2d50333bb03801f

  Message:
    Extract common code

  Added files:
    lib/groonga/client/command-line/parser.rb
  Modified files:
    lib/groonga/client/command-line/groonga-client-index-check.rb
    lib/groonga/client/command-line/groonga-client-index-recreate.rb
    lib/groonga/client/command-line/groonga-client.rb

  Modified: lib/groonga/client/command-line/groonga-client-index-check.rb (+9 -42)
===================================================================
--- lib/groonga/client/command-line/groonga-client-index-check.rb    2017-10-27 17:39:57 +0900 (1e2daaa)
+++ lib/groonga/client/command-line/groonga-client-index-check.rb    2017-10-27 17:58:52 +0900 (c667050)
@@ -15,45 +15,36 @@
 # License along with this library; if not, write to the Free Software
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 
-require "optparse"
-
 require "groonga/client"
+require "groonga/client/command-line/parser"
 
 module Groonga
   class Client
     module CommandLine
       class GroongaClientIndexCheck
         def initialize
-          @url      = nil
-          @protocol = :http
-          @host     = "localhost"
-          @port     = 10041
-
           @available_methods = [:source, :content]
           @methods = []
         end
 
-        def run(argv)
-          targets = parse_command_line(argv)
+        def run(arguments)
+          parser = Parser.new
+          indexes = parser.parse(arguments) do |option_parser|
+            parse_command_line(option_parser)
+          end
 
           if****@metho*****?
             @methods = @available_methods
           end
 
-          Client.open(:url      => @url,
-                      :protocol => @protocol,
-                      :host     => @host,
-                      :port     => @port,
-                      :backend  => :synchronous) do |client|
-            checker = Checker.new(client, @methods, targets)
+          parser.open_client do |client|
+            checker = Checker.new(client, @methods, indexes)
             checker.check
           end
         end
 
         private
-        def parse_command_line(argv)
-          parser = OptionParser.new
-          parser.version = VERSION
+        def parse_command_line(parser)
           parser.banner += " [LEXICON1.INDEX1 LEXICON2.INDEX2 ...]"
 
           parser.separator("")
@@ -61,7 +52,6 @@ module Groonga
                            "all indexes are checked.")
 
           parser.separator("")
-
           parser.separator("Method:")
 
           parser.on("--method=METHOD", @available_methods,
@@ -75,29 +65,6 @@ module Groonga
                     "(#{@available_methods.join(", ")})") do |method|
             @methods << method
           end
-
-          parser.separator("Connection:")
-
-          parser.on("--url=URL",
-                    "URL to connect to Groonga server.",
-                    "If this option is specified,",
-                    "--protocol, --host and --port are ignored.") do |url|
-            @url = url
-          end
-
-          parser.on("--host=HOST",
-                    "Groonga server to be connected.",
-                    "(#{@host})") do |host|
-            @host = host
-          end
-
-          parser.on("--port=PORT", Integer,
-                    "Port number of Groonga server to be connected.",
-                    "(auto)") do |port|
-            @port = port
-          end
-
-          parser.parse(argv)
         end
 
         class Checker

  Modified: lib/groonga/client/command-line/groonga-client-index-recreate.rb (+9 -72)
===================================================================
--- lib/groonga/client/command-line/groonga-client-index-recreate.rb    2017-10-27 17:39:57 +0900 (6e437d0)
+++ lib/groonga/client/command-line/groonga-client-index-recreate.rb    2017-10-27 17:58:52 +0900 (c114562)
@@ -14,38 +14,29 @@
 # License along with this library; if not, write to the Free Software
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 
-require "optparse"
 require "json"
 
 require "groonga/client"
+require "groonga/client/command-line/parser"
 
 module Groonga
   class Client
     module CommandLine
       class GroongaClientIndexRecreate
         def initialize
-          @url      = nil
-          @protocol = :http
-          @host     = "localhost"
-          @port     = nil
-
-          @read_timeout = -1
-
           @interval = :day
 
           @n_workers = 0
         end
 
-        def run(argv)
-          target_indexes = parse_command_line(argv)
+        def run(arguments)
+          parser = Parser.new(:read_timeout => -1)
+          indexes = parser.parse(arguments) do |option_parser|
+            parse_command_line(option_parser)
+          end
 
-          Client.open(:url      => @url,
-                      :protocol => @protocol,
-                      :host     => @host,
-                      :port     => @port,
-                      :read_timeout => @read_timeout,
-                      :backend  => :synchronous) do |client|
-            runner = Runner.new(client, @interval, target_indexes)
+          parser.open_client do |client|
+            runner = Runner.new(client, @interval, indexes)
             runner.run do
               @n_workers.times do
                 client.database_unmap
@@ -55,49 +46,10 @@ module Groonga
         end
 
         private
-        def parse_command_line(argv)
-          parser = OptionParser.new
-          parser.version = VERSION
+        def parse_command_line(parser)
           parser.banner += " LEXICON1.INDEX1 LEXICON2.INDEX2 ..."
 
           parser.separator("")
-          parser.separator("Connection:")
-
-          parser.on("--url=URL",
-                    "URL to connect to Groonga server.",
-                    "If this option is specified,",
-                    "--protocol, --host and --port are ignored.") do |url|
-            @url = url
-          end
-
-          available_protocols = [:http, :gqtp]
-          parser.on("--protocol=PROTOCOL", [:http, :gqtp],
-                    "Protocol to connect to Groonga server.",
-                    "[#{available_protocols.join(", ")}]",
-                    "(#{@protocol})") do |protocol|
-            @protocol = protocol
-          end
-
-          parser.on("--host=HOST",
-                    "Groonga server to be connected.",
-                    "(#{@host})") do |host|
-            @host = host
-          end
-
-          parser.on("--port=PORT", Integer,
-                    "Port number of Groonga server to be connected.",
-                    "(auto)") do |port|
-            @port = port
-          end
-
-          parser.on("--read-timeout=TIMEOUT", Integer,
-                    "Timeout on reading response from Groonga server.",
-                    "You can disable timeout by specifying -1.",
-                    "(#{@read_timeout})") do |timeout|
-            @read_timeout = timeout
-          end
-
-          parser.separator("")
           parser.separator("Configuration:")
 
           available_intervals = [:day]
@@ -117,21 +69,6 @@ module Groonga
                     "(#{@n_workers})") do |n|
             @n_workers = n
           end
-
-          target_indexes = parser.parse(argv)
-
-          @port ||= default_port(@protocol)
-
-          target_indexes
-        end
-
-        def default_port(protocol)
-          case protocol
-          when :http
-            10041
-          when :gqtp
-            10043
-          end
         end
 
         class Runner

  Modified: lib/groonga/client/command-line/groonga-client.rb (+11 -73)
===================================================================
--- lib/groonga/client/command-line/groonga-client.rb    2017-10-27 17:39:57 +0900 (a5b1a14)
+++ lib/groonga/client/command-line/groonga-client.rb    2017-10-27 17:58:52 +0900 (c13360d)
@@ -14,26 +14,19 @@
 # License along with this library; if not, write to the Free Software
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 
-require "optparse"
 require "json"
 require "securerandom"
 
-require "groonga/client"
-
 require "groonga/command/parser"
 
+require "groonga/client"
+require "groonga/client/command-line/parser"
+
 module Groonga
   class Client
     module CommandLine
       class GroongaClient
         def initialize
-          @url      = nil
-          @protocol = :http
-          @host     = "localhost"
-          @port     = nil
-
-          @read_timeout = Client::Default::READ_TIMEOUT
-
           @chunk = false
 
           @runner_options = {
@@ -42,16 +35,13 @@ module Groonga
           }
         end
 
-        def run(argv)
-          command_file_paths = parse_command_line(argv)
+        def run(arguments)
+          parser = Parser.new
+          command_file_paths = parser.parse(arguments) do |option_parser|
+            parse_command_line(option_parser)
+          end
 
-          Client.open(:url      => @url,
-                      :protocol => @protocol,
-                      :host     => @host,
-                      :port     => @port,
-                      :read_timeout => @read_timeout,
-                      :chunk    => @chunk,
-                      :backend  => :synchronous) do |client|
+          parser.open_client(:chunk => @chunk) do |client|
             runner = Runner.new(client, @runner_options)
 
             if command_file_paths.empty?
@@ -79,48 +69,11 @@ module Groonga
         end
 
         private
-        def parse_command_line(argv)
-          parser = OptionParser.new
-          parser.version = VERSION
+        def parse_command_line(parser)
           parser.banner += " GROONGA_COMMAND_FILE1 GROONGA_COMMAND_FILE2 ..."
 
           parser.separator("")
-
-          parser.separator("Connection:")
-
-          parser.on("--url=URL",
-                    "URL to connect to Groonga server.",
-                    "If this option is specified,",
-                    "--protocol, --host and --port are ignored.") do |url|
-            @url = url
-          end
-
-          available_protocols = [:http, :gqtp]
-          parser.on("--protocol=PROTOCOL", [:http, :gqtp],
-                    "Protocol to connect to Groonga server.",
-                    "[#{available_protocols.join(", ")}]",
-                    "(#{@protocol})") do |protocol|
-            @protocol = protocol
-          end
-
-          parser.on("--host=HOST",
-                    "Groonga server to be connected.",
-                    "(#{@host})") do |host|
-            @host = host
-          end
-
-          parser.on("--port=PORT", Integer,
-                    "Port number of Groonga server to be connected.",
-                    "(auto)") do |port|
-            @port = port
-          end
-
-          parser.on("--read-timeout=TIMEOUT", Integer,
-                    "Timeout on reading response from Groonga server.",
-                    "You can disable timeout by specifying -1.",
-                    "(#{@read_timeout})") do |timeout|
-            @read_timeout = timeout
-          end
+          parser.separator("Request:")
 
           parser.on("--split-load-chunk-size=SIZE", Integer,
                     "Split a large load to small loads.",
@@ -142,21 +95,6 @@ module Groonga
                     "(#{@chunk})") do |boolean|
             @chunk = boolean
           end
-
-          command_file_paths = parser.parse(argv)
-
-          @port ||= default_port(@protocol)
-
-          command_file_paths
-        end
-
-        def default_port(protocol)
-          case protocol
-          when :http
-            10041
-          when :gqtp
-            10043
-          end
         end
 
         class Runner

  Added: lib/groonga/client/command-line/parser.rb (+111 -0) 100644
===================================================================
--- /dev/null
+++ lib/groonga/client/command-line/parser.rb    2017-10-27 17:58:52 +0900 (07e5f0f)
@@ -0,0 +1,111 @@
+# Copyright (C) 2015-2017  Kouhei Sutou <kou �� clear-code.com>
+#
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License, or (at your option) any later version.
+#
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+
+require "optparse"
+
+require "groonga/client"
+
+module Groonga
+  class Client
+    module CommandLine
+      class Parser
+        def initialize(options={})
+          @url      = nil
+          @protocol = :http
+          @host     = "localhost"
+          @port     = nil
+
+          @read_timeout = options[:read_timeout] || Client::Default::READ_TIMEOUT
+        end
+
+        def parse(arguments)
+          parser = OptionParser.new
+          parser.version = VERSION
+
+          parser.separator("")
+
+          parser.separator("Connection:")
+
+          parser.on("--url=URL",
+                    "URL to connect to Groonga server.",
+                    "If this option is specified,",
+                    "--protocol, --host and --port are ignored.") do |url|
+            @url = url
+          end
+
+          available_protocols = [:http, :gqtp]
+          parser.on("--protocol=PROTOCOL", [:http, :gqtp],
+                    "Protocol to connect to Groonga server.",
+                    "[#{available_protocols.join(", ")}]",
+                    "(#{@protocol})") do |protocol|
+            @protocol = protocol
+          end
+
+          parser.on("--host=HOST",
+                    "Groonga server to be connected.",
+                    "(#{@host})") do |host|
+            @host = host
+          end
+
+          parser.on("--port=PORT", Integer,
+                    "Port number of Groonga server to be connected.",
+                    "(auto)") do |port|
+            @port = port
+          end
+
+          parser.on("--read-timeout=TIMEOUT", Integer,
+                    "Timeout on reading response from Groonga server.",
+                    "You can disable timeout by specifying -1.",
+                    "(#{@read_timeout})") do |timeout|
+            @read_timeout = timeout
+          end
+
+          yield(parser)
+
+          rest = parser.parse(arguments)
+
+          @port ||= default_port(@protocol)
+
+          rest
+        end
+
+        def open_client(options={})
+          default_options = {
+            :url      => @url,
+            :protocol => @protocol,
+            :host     => @host,
+            :port     => @port,
+            :read_timeout => @read_timeout,
+            :backend  => :synchronous,
+          }
+          Client.open(default_options.merge(options)) do |client|
+            yield(client)
+          end
+        end
+
+        private
+        def default_port(protocol)
+          case protocol
+          when :http
+            10041
+          when :gqtp
+            10043
+          end
+        end
+      end
+    end
+  end
+end
-------------- next part --------------
HTML����������������������������...
URL: https://lists.osdn.me/mailman/archives/groonga-commit/attachments/20171027/e2b20b41/attachment-0001.htm 



More information about the Groonga-commit mailing list
Back to archive index