Skip to content

Instantly share code, notes, and snippets.

@badboy
Created April 13, 2014 11:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save badboy/10579966 to your computer and use it in GitHub Desktop.
Save badboy/10579966 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# encoding: utf-8
require 'optparse'
require 'redic'
options = {
port: 6379,
host: '127.0.0.1',
match: nil,
ask: true,
raw: false
}
OptionParser.new do |opts|
opts.banner = "Usage: example.rb [options]"
opts.on("-p", "--port PORT", Integer, "Server port (default: 6379)") do |p|
options[:port] = p
end
opts.on("-h", "--host HOST", String, "Server hostname (default: 127.0.0.1)") do |h|
options[:port] = h
end
opts.on("-m", "--match PATTERN", String, "Pattern to match against (default: nil)") do |m|
options[:match] = m
end
opts.on("-n", "--noask", "Don't ask before continue") do |n|
options[:ask] = false
end
opts.on("-r", "--raw", "Raw output") do |r|
options[:raw] = true
end
end.parse!
def scan redis, cursor, pattern
if pattern
redis.call "scan", cursor, "match", pattern
else
redis.call "scan", cursor
end
end
def format_puts idx, key, raw=false
if raw
puts key
else
puts "#{idx.to_s.rjust(4, ' ')}. #{key}"
end
end
url = "tcp://#{options[:host]}:#{options[:port]}/"
redis = Redic.new url
begin
cursor, data = scan redis, 0, options[:match]
skipped = 0
while cursor != "0"
data.each_with_index do |key, idx|
format_puts idx+1, key, options[:raw]
end
if options[:ask]
if data.empty?
skipped += 1
else
if skipped > 0
printf "[skipped=#{skipped}] "
end
skipped = 0
printf "[cursor=#{cursor}] "
printf "Continue? ENTER to continue, Ctrl-C to cancel."
gets
end
end
cursor, data = scan redis, cursor, options[:match]
end
rescue Interrupt
puts
puts
puts "[Canceled by user.]"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment