YUKI Hiroshi
null+****@clear*****
Fri May 29 17:19:30 JST 2015
YUKI Hiroshi 2015-05-29 17:19:30 +0900 (Fri, 29 May 2015) New Revision: a958248a36ad8853e52744732940672470f5e3d4 https://github.com/droonga/droonga-client-ruby/commit/a958248a36ad8853e52744732940672470f5e3d4 Message: Use grn2drn to convert Groonga commands to Droonga's message format Modified files: bin/droonga-groonga droonga-client.gemspec Modified: bin/droonga-groonga (+48 -36) =================================================================== --- bin/droonga-groonga 2015-05-08 11:02:11 +0900 (b970421) +++ bin/droonga-groonga 2015-05-29 17:19:30 +0900 (95809c5) @@ -18,6 +18,7 @@ require "json" require "droonga/command/base" +require "grn2drn/command-converter" module Droonga module Command @@ -33,33 +34,35 @@ module Droonga option.banner("Usage: droonga-groonga [groonga-command-name] [options]\n" + " ex: droonga-groonga select --table=Users") + option.on(:"dry-run", + "Only reports messages converted from Groonga to Droonga.", + :default => false) + option.separator("Formatting:") option.on(:pretty, "Output result as a pretty print JSON.", :default => false) end - @command = ARGV.shift - assert_valid_command - - groonga_message = { - "dataset" => @options[:dataset], - "type" => @command, - "body" => build_params(ARGV), - } - - response = request(groonga_message) - raise NoResponse.new unless response - body = response["body"] - - if @options[:pretty] - puts(JSON.pretty_generate(body)) + unless ARGV.empty? + groonga_command_args = ARGV.collect do |arg| + if /\s/ =~ arg + JSON.generate(arg) + else + arg + end + end + command_converter.convert(groonga_command_args.join(" ")) do |command| + process_groonga_message(command) + end else - puts(JSON.generate(body)) + command_converter.convert(ARGF) do |command| + process_groonga_message(command) + end end true - rescue MissingRequiredParameter, InvalidCommandName, MissingCommandName + rescue MissingRequiredParameter puts(@options) false rescue NoResponse @@ -68,28 +71,37 @@ module Droonga end private - def assert_valid_command - raise InvalidCommandName.new(@command) if /\A--.+\z/ =~ @command - raise MissingCommandName.new if****@comma*****? - end - - def build_params(argv) - params = {} - option_name = nil - argv.each do |arg| - case arg - when /\A--([^\s=]+)=(.+)\z/ - params[$1] = $2 - when /\A--([^\s=]+)\z/ - option_name = $1 + def process_groonga_message(groonga_message) + if @options[:"dry-run"] + if @options[:pretty] + puts(JSON.pretty_generate(groonga_message)) else - if option_name - params[option_name] = arg - option_name = nil - end + puts(JSON.generate(groonga_message)) end + return end - params + + response = request(groonga_message) + raise NoResponse.new unless response + body = response["body"] + + if @options[:pretty] + puts(JSON.pretty_generate(body)) + else + puts(JSON.generate(body)) + end + end + + def command_converter + @command_converter ||= create_command_converter + end + + def create_command_converter + convert_options = { + :id_prefix => "droonga-groonga:", + :dataset => @options[:dataset], + } + ::Grn2Drn::CommandConverter.new(convert_options) end end end Modified: droonga-client.gemspec (+1 -0) =================================================================== --- droonga-client.gemspec 2015-05-08 11:02:11 +0900 (7bff131) +++ droonga-client.gemspec 2015-05-29 17:19:30 +0900 (9af1671) @@ -37,6 +37,7 @@ Gem::Specification.new do |spec| spec.add_runtime_dependency "droonga-message-pack-packer" spec.add_runtime_dependency "fluent-logger" + spec.add_runtime_dependency "grn2drn" spec.add_runtime_dependency "msgpack" spec.add_runtime_dependency "rack" spec.add_runtime_dependency "slop", "<= 3.6.0" -------------- next part -------------- HTML����������������������������... ダウンロード