forked from mirror/ledisdb
353 lines
9.6 KiB
Go
353 lines
9.6 KiB
Go
// Package cmd
|
|
// This file was generated by .tools/generate_commands.py on Sat Oct 28 2017 18:15:49 -0500
|
|
package cmd
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"os"
|
|
"regexp"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/peterh/liner"
|
|
"github.com/siddontang/goredis"
|
|
)
|
|
|
|
var helpCommands = [][]string{
|
|
{"APPEND", "key value", "KV"},
|
|
{"BITCOUNT", "key [start] [end]", "KV"},
|
|
{"BITOP", "operation destkey key [key ...]", "KV"},
|
|
{"BITPOS", "key bit [start] [end]", "KV"},
|
|
{"BLPOP", "key [key ...] timeout", "List"},
|
|
{"BRPOP", "key [key ...] timeout", "List"},
|
|
{"CONFIG GET", "parameter", "Server"},
|
|
{"CONFIG REWRITE", "-", "Server"},
|
|
{"DECR", "key", "KV"},
|
|
{"DECRBY", "key decrement", "KV"},
|
|
{"DEL", "key [key ...]", "KV"},
|
|
{"DUMP", "key", "KV"},
|
|
{"ECHO", "message", "Server"},
|
|
{"EVAL", "script numkeys key [key ...] arg [arg ...]", "Script"},
|
|
{"EVALSHA", "sha1 numkeys key [key ...] arg [arg ...]", "Script"},
|
|
{"EXISTS", "key", "KV"},
|
|
{"EXPIRE", "key seconds", "KV"},
|
|
{"EXPIREAT", "key timestamp", "KV"},
|
|
{"FLUSHALL", "-", "Server"},
|
|
{"FLUSHDB", "-", "Server"},
|
|
{"FULLSYNC", "[NEW]", "Replication"},
|
|
{"GET", "key", "KV"},
|
|
{"GETBIT", "key offset", "KV"},
|
|
{"GETRANGE", "key start end", "KV"},
|
|
{"GETSET", " key value", "KV"},
|
|
{"HCLEAR", "key", "Hash"},
|
|
{"HDEL", "key field [field ...]", "Hash"},
|
|
{"HDUMP", "key", "Hash"},
|
|
{"HEXISTS", "key field", "Hash"},
|
|
{"HEXPIRE", "key seconds", "Hash"},
|
|
{"HEXPIREAT", "key timestamp", "Hash"},
|
|
{"HGET", "key field", "Hash"},
|
|
{"HGETALL", "key", "Hash"},
|
|
{"HINCRBY", "key field increment", "Hash"},
|
|
{"HKEYEXISTS", "key", "Hash"},
|
|
{"HKEYS", "key", "Hash"},
|
|
{"HLEN", "key", "Hash"},
|
|
{"HMCLEAR", "key [key ...]", "Hash"},
|
|
{"HMGET", "key field [field ...]", "Hash"},
|
|
{"HMSET", "key field value [field value ...]", "Hash"},
|
|
{"HPERSIST", "key", "Hash"},
|
|
{"HSCAN", "key cursor [MATCH match] [COUNT count] [ASC|DESC]", "Hash"},
|
|
{"HSET", "key field value", "Hash"},
|
|
{"HTTL", "key", "Hash"},
|
|
{"HVALS", "key", "Hash"},
|
|
{"INCR", "key", "KV"},
|
|
{"INCRBY", "key increment", "KV"},
|
|
{"INFO", "[section]", "Server"},
|
|
{"LCLEAR", "key", "List"},
|
|
{"LDUMP", "key", "List"},
|
|
{"LEXPIRE", "key seconds", "List"},
|
|
{"LEXPIREAT", "key timestamp", "List"},
|
|
{"LINDEX", "key index", "List"},
|
|
{"LKEYEXISTS", "key", "List"},
|
|
{"LLEN", "key", "List"},
|
|
{"LMCLEAR", "key [key ...]", "List"},
|
|
{"LPERSIST", "key", "List"},
|
|
{"LPOP", "key", "List"},
|
|
{"LPUSH", "key value [value ...]", "List"},
|
|
{"LRANGE", "key start stop", "List"},
|
|
{"LSET", "key index value", "List"},
|
|
{"LTTL", "key", "List"},
|
|
{"MGET", "key [key ...]", "KV"},
|
|
{"MSET", "key value [key value ...]", "KV"},
|
|
{"PERSIST", "key", "KV"},
|
|
{"PING", "-", "Server"},
|
|
{"RESTORE", "key ttl value", "Server"},
|
|
{"ROLE", "-", "Server"},
|
|
{"RPOP", "key", "List"},
|
|
{"RPUSH", "key value [value ...]", "List"},
|
|
{"SADD", "key member [member ...]", "Set"},
|
|
{"SCARD", "key", "Set"},
|
|
{"SCLEAR", "key", "Set"},
|
|
{"SCRIPT EXISTS", "script [script ...]", "Script"},
|
|
{"SCRIPT FLUSH", "-", "Script"},
|
|
{"SCRIPT LOAD", "script", "Script"},
|
|
{"SDIFF", "key [key ...]", "Set"},
|
|
{"SDIFFSTORE", "destination key [key ...]", "Set"},
|
|
{"SDUMP", "key", "Set"},
|
|
{"SELECT", "index", "Server"},
|
|
{"SET", "key value", "KV"},
|
|
{"SETBIT", "key offset value", "KV"},
|
|
{"SETEX", "key seconds value", "KV"},
|
|
{"SETNX", "key value", "KV"},
|
|
{"SETRANGE", "key offset value", "KV"},
|
|
{"SEXPIRE", "key seconds", "Set"},
|
|
{"SEXPIREAT", "key timestamp", "Set"},
|
|
{"SINTER", "key [key ...]", "Set"},
|
|
{"SINTERSTORE", "destination key [key ...]", "Set"},
|
|
{"SISMEMBER", "key member", "Set"},
|
|
{"SKEYEXISTS", "key", "Set"},
|
|
{"SLAVEOF", "host port [RESTART] [READONLY]", "Replication"},
|
|
{"SMCLEAR", "key [key ...]", "Set"},
|
|
{"SMEMBERS", "key", "Set"},
|
|
{"SPERSIST", "key", "Set"},
|
|
{"SREM", "key member [member ...]", "Set"},
|
|
{"SSCAN", "key cursor [MATCH match] [COUNT count] [ASC|DESC]", "Set"},
|
|
{"STRLEN", "key", "KV"},
|
|
{"STTL", "key", "Set"},
|
|
{"SUNION", "key [key ...]", "Set"},
|
|
{"SUNIONSTORE", "destination key [key ...]", "Set"},
|
|
{"SYNC", "logid", "Replication"},
|
|
{"TIME", "-", "Server"},
|
|
{"TTL", "key", "KV"},
|
|
{"XHSCAN", "key cursor [MATCH match] [COUNT count] [ASC|DESC]", "Hash"},
|
|
{"XLSORT", "key [BY pattern] [LIMIT offset count] [GET pattern [GET pattern ...]] [ASC|DESC] [ALPHA] [STORE destination]", "List"},
|
|
{"XSCAN", "type cursor [MATCH match] [COUNT count] [ASC|DESC]", "Server"},
|
|
{"XSSCAN", "key cursor [MATCH match] [COUNT count] [ASC|DESC]", "Set"},
|
|
{"XSSORT", "key [BY pattern] [LIMIT offset count] [GET pattern [GET pattern ...]] [ASC|DESC] [ALPHA] [STORE destination]", "Set"},
|
|
{"XZSCAN", "key cursor [MATCH match] [COUNT count] [ASC|DESC]", "ZSet"},
|
|
{"XZSORT", "key [BY pattern] [LIMIT offset count] [GET pattern [GET pattern ...]] [ASC|DESC] [ALPHA] [STORE destination]", "ZSet"},
|
|
{"ZADD", "key score member [score member ...]", "ZSet"},
|
|
{"ZCARD", "key", "ZSet"},
|
|
{"ZCLEAR", "key", "ZSet"},
|
|
{"ZCOUNT", "key min max", "ZSet"},
|
|
{"ZDUMP", "key", "ZSet"},
|
|
{"ZEXPIRE", "key seconds", "ZSet"},
|
|
{"ZEXPIREAT", "key timestamp", "ZSet"},
|
|
{"ZINCRBY", "key increment member", "ZSet"},
|
|
{"ZINTERSTORE", "destkey numkeys key [key ...] [WEIGHTS weight [weight ...]] [AGGREGATE SUM|MIN|MAX]", "ZSet"},
|
|
{"ZKEYEXISTS", "key", "ZSet"},
|
|
{"ZLEXCOUNT", "key min max", "ZSet"},
|
|
{"ZMCLEAR", "key [key ...]", "ZSet"},
|
|
{"ZPERSIST", "key", "ZSet"},
|
|
{"ZRANGE", "key start stop [WITHSCORES]", "ZSet"},
|
|
{"ZRANGEBYLEX", "key min max [LIMIT offset count]", "ZSet"},
|
|
{"ZRANGEBYSCORE", "key min max [WITHSCORES] [LIMIT offset count]", "ZSet"},
|
|
{"ZRANK", "key member", "ZSet"},
|
|
{"ZREM", "key member [member ...]", "ZSet"},
|
|
{"ZREMRANGBYLEX", "key min max", "ZSet"},
|
|
{"ZREMRANGEBYRANK", "key start stop", "ZSet"},
|
|
{"ZREMRANGEBYSCORE", "key min max", "ZSet"},
|
|
{"ZREVRANGE", "key start stop [WITHSCORES]", "ZSet"},
|
|
{"ZREVRANGEBYSCORE", "key max min [WITHSCORES][LIMIT offset count]", "ZSet"},
|
|
{"ZREVRANK", "key member", "ZSet"},
|
|
{"ZSCAN", "key cursor [MATCH match] [COUNT count] [ASC|DESC]", "ZSet"},
|
|
{"ZSCORE", "key member", "ZSet"},
|
|
{"ZTTL", "key", "ZSet"},
|
|
{"ZUNIONSTORE", "destkey numkeys key [key ...] [WEIGHTS weight [weight ...]] [AGGREGATE SUM|MIN|MAX]", "ZSet"},
|
|
}
|
|
|
|
var ip = flag.String("h", "127.0.0.1", "ledisdb server ip (default 127.0.0.1)")
|
|
var port = flag.Int("p", 6380, "ledisdb server port (default 6380)")
|
|
var socket = flag.String("s", "", "ledisdb server socket, overwrite ip and port")
|
|
|
|
var (
|
|
line *liner.State
|
|
historyPath = "/tmp/ledis-cli"
|
|
)
|
|
|
|
func Cli() {
|
|
var dbn = flag.Int("n", 0, "ledisdb database number(default 0)")
|
|
|
|
flag.Parse()
|
|
|
|
line = liner.NewLiner()
|
|
defer line.Close()
|
|
|
|
line.SetCtrlCAborts(true)
|
|
|
|
setCompletionHandler()
|
|
loadHisotry()
|
|
|
|
defer saveHisotry()
|
|
|
|
var addr string
|
|
if len(*socket) > 0 {
|
|
addr = *socket
|
|
} else {
|
|
addr = fmt.Sprintf("%s:%d", *ip, *port)
|
|
}
|
|
|
|
c := goredis.NewClient(addr, "")
|
|
c.SetMaxIdleConns(1)
|
|
sendSelect(c, *dbn)
|
|
|
|
reg, _ := regexp.Compile(`'.*?'|".*?"|\S+`)
|
|
|
|
prompt := ""
|
|
|
|
for {
|
|
if *dbn > 0 && *dbn < 16 {
|
|
prompt = fmt.Sprintf("%s[%d]> ", addr, *dbn)
|
|
} else {
|
|
prompt = fmt.Sprintf("%s> ", addr)
|
|
}
|
|
|
|
cmd, err := line.Prompt(prompt)
|
|
if err != nil {
|
|
fmt.Printf("%s\n", err.Error())
|
|
return
|
|
}
|
|
|
|
cmds := reg.FindAllString(cmd, -1)
|
|
if len(cmds) == 0 {
|
|
continue
|
|
} else {
|
|
line.AppendHistory(cmd)
|
|
|
|
args := make([]interface{}, len(cmds[1:]))
|
|
|
|
for i := range args {
|
|
args[i] = strings.Trim(string(cmds[1+i]), "\"'")
|
|
}
|
|
|
|
cmd := strings.ToLower(cmds[0])
|
|
if cmd == "help" || cmd == "?" {
|
|
printHelp(cmds)
|
|
} else {
|
|
r, err := c.Do(cmds[0], args...)
|
|
|
|
if err == nil && strings.ToLower(cmds[0]) == "select" {
|
|
*dbn, _ = strconv.Atoi(cmds[1])
|
|
}
|
|
|
|
if err != nil {
|
|
fmt.Printf("%s", err.Error())
|
|
} else {
|
|
if cmd == "info" {
|
|
printInfo(r.([]byte))
|
|
} else {
|
|
printReply(0, r)
|
|
}
|
|
}
|
|
|
|
fmt.Printf("\n")
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
func printInfo(s []byte) {
|
|
fmt.Printf("%s", s)
|
|
}
|
|
|
|
func printReply(level int, reply interface{}) {
|
|
switch reply := reply.(type) {
|
|
case int64:
|
|
fmt.Printf("(integer) %d", reply)
|
|
case string:
|
|
fmt.Printf("%s", reply)
|
|
case []byte:
|
|
fmt.Printf("%q", reply)
|
|
case nil:
|
|
fmt.Printf("(nil)")
|
|
case goredis.Error:
|
|
fmt.Printf("%s", string(reply))
|
|
case []interface{}:
|
|
for i, v := range reply {
|
|
if i != 0 {
|
|
fmt.Printf("%s", strings.Repeat(" ", level*4))
|
|
}
|
|
|
|
s := fmt.Sprintf("%d) ", i+1)
|
|
fmt.Printf("%-4s", s)
|
|
|
|
printReply(level+1, v)
|
|
if i != len(reply)-1 {
|
|
fmt.Printf("\n")
|
|
}
|
|
}
|
|
default:
|
|
fmt.Printf("invalid ledis reply")
|
|
}
|
|
}
|
|
|
|
func printGenericHelp() {
|
|
msg :=
|
|
`ledis-cli
|
|
Type: "help <command>" for help on <command>
|
|
`
|
|
fmt.Println(msg)
|
|
}
|
|
|
|
func printCommandHelp(arr []string) {
|
|
fmt.Println()
|
|
fmt.Printf("\t%s %s \n", arr[0], arr[1])
|
|
fmt.Printf("\tGroup: %s \n", arr[2])
|
|
fmt.Println()
|
|
}
|
|
|
|
func printHelp(cmds []string) {
|
|
args := cmds[1:]
|
|
if len(args) == 0 {
|
|
printGenericHelp()
|
|
} else if len(args) > 1 {
|
|
fmt.Println()
|
|
} else {
|
|
cmd := strings.ToUpper(args[0])
|
|
for i := 0; i < len(helpCommands); i++ {
|
|
if helpCommands[i][0] == cmd {
|
|
printCommandHelp(helpCommands[i])
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func sendSelect(client *goredis.Client, index int) {
|
|
if index > 16 || index < 0 {
|
|
index = 0
|
|
fmt.Println("index out of range, should less than 16")
|
|
}
|
|
_, err := client.Do("select", index)
|
|
if err != nil {
|
|
fmt.Printf("%s\n", err.Error())
|
|
}
|
|
}
|
|
|
|
func setCompletionHandler() {
|
|
line.SetCompleter(func(line string) (c []string) {
|
|
for _, i := range helpCommands {
|
|
if strings.HasPrefix(i[0], strings.ToLower(line)) {
|
|
c = append(c, i[0])
|
|
}
|
|
}
|
|
return
|
|
})
|
|
}
|
|
|
|
func loadHisotry() {
|
|
if f, err := os.Open(historyPath); err == nil {
|
|
line.ReadHistory(f)
|
|
f.Close()
|
|
}
|
|
}
|
|
|
|
func saveHisotry() {
|
|
if f, err := os.Create(historyPath); err != nil {
|
|
fmt.Printf("Error writing history file, err: %v", err)
|
|
} else {
|
|
line.WriteHistory(f)
|
|
f.Close()
|
|
}
|
|
}
|