class GrnLine::Wrapper
Constants
- GROONGA_COMMANDS
- GROONGA_FUNCTIONS
- GROONGA_SHUTDOWN_COMMANDS
- GROONGA_VARIABLES
- HISTORY_FILE
Attributes
options[RW]
Public Class Methods
new()
click to toggle source
# File lib/grnline/wrapper.rb, line 52 def initialize @options = nil @history = GrnLine::History.new(HISTORY_FILE) @commandline_prefix = "" setup_input_completion end
run(argv)
click to toggle source
# File lib/grnline/wrapper.rb, line 47 def run(argv) new.run(argv) end
Public Instance Methods
run(argv)
click to toggle source
# File lib/grnline/wrapper.rb, line 59 def run(argv) @options = parse_commandline_options(argv) groonga_commandline = generate_groonga_commandline Open3.popen3(*groonga_commandline) do |input, output, error, _| @input, @output, @error = input, output, error return true unless ensure_groonga_running return false unless ensure_groonga_ready setup_interrupt_shutdown command = nil @history.load while(command = Readline.readline("#{@commandline_prefix}> ", true)) do @history.store(Readline::HISTORY.to_a.last) process_command(command) break if GROONGA_SHUTDOWN_COMMANDS.include?(command) end shutdown_groonga unless GROONGA_SHUTDOWN_COMMANDS.include?(command) @history.save true end end
Private Instance Methods
ensure_groonga_ready()
click to toggle source
# File lib/grnline/wrapper.rb, line 196 def ensure_groonga_ready begin execute("status") rescue if IO.select([@error], [], [], 0) $stderr.puts(@error.read) return false end end return true end
ensure_groonga_running()
click to toggle source
# File lib/grnline/wrapper.rb, line 188 def ensure_groonga_running if IO.select([@output], nil, nil, 1) and not @output.eof? output(@output.read) return false end return true end
execute(command)
click to toggle source
# File lib/grnline/wrapper.rb, line 162 def execute(command) @input.puts(command) @input.flush @output.gets end
generate_groonga_commandline()
click to toggle source
# File lib/grnline/wrapper.rb, line 93 def generate_groonga_commandline [@options.groonga, *@options.groonga_arguments] end
output(object)
click to toggle source
# File lib/grnline/wrapper.rb, line 145 def output(object) output = nil if @options.output.instance_of?(String) output = File.open(@options.output, "w") else output = @options.output end if object.instance_of?(Lazy::PP::JSON) PP.pp(object, output) else output.puts(object) end output.close if output.instance_of?(File) end
output_response(raw_response)
click to toggle source
# File lib/grnline/wrapper.rb, line 132 def output_response(raw_response) # TODO: support pretty print for formats except JSON response = raw_response if @options.use_pretty_print begin response = Lazy::PP::JSON.new(response) rescue end end output(response) end
parse_commandline_options(argv)
click to toggle source
# File lib/grnline/wrapper.rb, line 88 def parse_commandline_options(argv) options_parser = GrnLine::OptionsParser.new options_parser.parse(argv) end
process_command(command)
click to toggle source
# File lib/grnline/wrapper.rb, line 97 def process_command(command) raw_response = nil begin raw_response = read_groonga_response(command) rescue => e raise("Failed to access the groonga database: #{e.message}") end if raw_response and not raw_response.empty? # TODO: support pretty print for formats except JSON @commandline_prefix = "" output_response(raw_response) else @commandline_prefix = "*" end end
read_groonga_response(command)
click to toggle source
# File lib/grnline/wrapper.rb, line 114 def read_groonga_response(command) return nil if command.empty? response = "" @input.puts(command) @input.flush timeout = 1 while IO.select([@output], [], [], timeout) break if @output.eof? read_content = @output.readpartial(1024) response << read_content timeout = 0 if read_content.bytesize < 1024 end response end
setup_input_completion()
click to toggle source
# File lib/grnline/wrapper.rb, line 181 def setup_input_completion Readline.completion_proc = lambda do |word| completions = (GROONGA_COMMANDS + GROONGA_VARIABLES + GROONGA_FUNCTIONS) completions.grep(/\A#{Regexp.escape(word)}/) end end
setup_interrupt_shutdown()
click to toggle source
# File lib/grnline/wrapper.rb, line 173 def setup_interrupt_shutdown trap("INT") do shutdown_groonga @history.save exit(true) end end
shutdown_groonga()
click to toggle source
# File lib/grnline/wrapper.rb, line 168 def shutdown_groonga execute("shutdown") @input.close end