Class: Readis::Inspect

Inherits:
Readis
  • Object
show all
Defined in:
lib/readis/inspect.rb

Constant Summary collapse

WHITELIST =
%w[
  keys
  exists get hexists hget hgetall hkeys hlen hmget hvals
  info lindex llen lrange mget randomkey scard sdiff
  sinter sismember smembers srandmember strlen sunion
  ttl type zcard zcount zrange zrangebyscore zrank
  zrevrange zrevrangebyscore zrevrank zscore
]

Instance Method Summary collapse

Methods inherited from Readis

command_runner, #help, #options

Constructor Details

#initialize(*args) ⇒ Inspect

Returns a new instance of Inspect.



13
14
15
16
# File 'lib/readis/inspect.rb', line 13

def initialize(*args)
  super
  @redis = Redis.new(:host => self.options[:host], :port => self.options[:port])
end

Instance Method Details

#execute_command(input_string) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/readis/inspect.rb', line 53

def execute_command(input_string)
  parts = input_string.split(" ")
  redis_command = parts.shift
  
  if redis_command
    redis_command.downcase!
  end
  
  case redis_command
  when nil
    # do nothing
  when *WHITELIST
    @redis.send(redis_command, *parts)
  when "exit", "quit"
    exit
  else
    raise ArgumentError, "Unknown or unsupported command"
  end
end

#parserObject



18
19
20
21
22
23
24
25
26
# File 'lib/readis/inspect.rb', line 18

def parser
  optparser = super
  optparser.banner = <<-BANNER

Usage: readis inspect [options]

  BANNER
  optparser
end

#runObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/readis/inspect.rb', line 28

def run
  loop do
    print "readis #{self.options[:host]}:#{self.options[:port]}> "
    input_string = gets.chomp
    # TODO: tab completion.  Example implementation in automatthew/flipper
    # https://github.com/automatthew/flipper/blob/master/lib/flipper.rb
    # Inititally, we should only try to complete using the list of command
    # names, but we may later consider adding keys, fields, member names, etc.
    # discovered through commands issued.
    begin
      out = execute_command(input_string)
      case out
      when nil
        # do nothing
      else
        # TODO: consider the formatting.  Do we want to mimic
        # the redis-cli output?
        puts out.inspect
      end
    rescue => error
      puts "Error: #{error.message}"
    end
  end
end