Last active
July 7, 2021 11:40
-
-
Save fukajun/f25347e3d3b5a5b7d24e94ab82c6b171 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
def redis_host | |
ENV['REDIS_HOST'] | |
end | |
def rediscmd(cmd) | |
escaped_cmd = cmd.gsub('*', '\*') | |
`redis-cli -h #{redis_host} #{escaped_cmd}`.strip | |
end | |
def redisinspect(key, detail = false) | |
type = rediscmd("type #{key}").strip | |
return %Q(type: "#{type}", key: "#{key}", count_cmd: "", inspect: "") if type == "none" | |
count_cmd = | |
case type | |
when 'set' then "scard #{key}" | |
when 'zset' then "zcount #{key} -inf +inf" | |
when 'string' then "get #{key}" | |
when 'hash' then "hlen #{key}" | |
when 'none' then "hlen #{key}" | |
end | |
result = rediscmd count_cmd | |
output = %Q(type: "#{type}", key: "#{key}", count_cmd: "#{count_cmd}", inspect: "#{result}") | |
if detail | |
inspect_cmd = | |
case type | |
when 'set' then "smembers #{key}" | |
when 'zset' then "zrange #{key} 1 10" | |
when 'string' then "get #{key}" | |
when 'hash' then "hvals #{key}" | |
end | |
inspect_result = rediscmd inspect_cmd | |
output << "\n#{inspect_result}\n\n" | |
end | |
output | |
end | |
sub_command = ARGV[0] | |
case sub_command | |
when 'inspect', 'i' | |
puts redisinspect(ARGV[1]) | |
when 'inspect-detail', 'id' | |
puts redisinspect(ARGV[1], true) | |
when 'cmd', 'c' | |
args = ARGV[1..-1].join(' ') | |
puts args | |
puts rediscmd(args) | |
else | |
puts <<-TXT | |
sub command of #{sub_command} is not found. | |
Usage: | |
#{__FILE__} cmd get key | |
#{__FILE__} inspect key | |
#{__FILE__} inspect-detail key | |
TXT | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment