2014-06-21 18:12:37 +04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
2015-08-02 04:23:51 +03:00
|
|
|
"os"
|
2014-07-02 06:13:34 +04:00
|
|
|
"regexp"
|
2014-07-17 07:08:14 +04:00
|
|
|
"strconv"
|
2014-06-21 18:12:37 +04:00
|
|
|
"strings"
|
2015-05-04 17:42:28 +03:00
|
|
|
|
2015-08-02 04:23:51 +03:00
|
|
|
"github.com/peterh/liner"
|
2015-05-04 17:42:28 +03:00
|
|
|
"github.com/siddontang/goredis"
|
2014-06-21 18:12:37 +04:00
|
|
|
)
|
|
|
|
|
|
|
|
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")
|
2014-07-17 08:06:07 +04:00
|
|
|
var dbn = flag.Int("n", 0, "ledisdb database number(default 0)")
|
2014-06-21 18:12:37 +04:00
|
|
|
|
2015-08-02 04:23:51 +03:00
|
|
|
var (
|
|
|
|
line *liner.State
|
|
|
|
historyPath = "/tmp/ledis-cli"
|
|
|
|
)
|
|
|
|
|
2014-06-21 18:12:37 +04:00
|
|
|
func main() {
|
|
|
|
flag.Parse()
|
|
|
|
|
2015-08-02 04:23:51 +03:00
|
|
|
line = liner.NewLiner()
|
|
|
|
defer line.Close()
|
|
|
|
|
|
|
|
line.SetCtrlCAborts(true)
|
|
|
|
|
|
|
|
setCompletionHandler()
|
|
|
|
loadHisotry()
|
|
|
|
|
|
|
|
defer saveHisotry()
|
|
|
|
|
2015-03-11 06:54:02 +03:00
|
|
|
var addr string
|
2014-06-21 18:12:37 +04:00
|
|
|
if len(*socket) > 0 {
|
2015-03-11 06:54:02 +03:00
|
|
|
addr = *socket
|
2014-06-21 18:12:37 +04:00
|
|
|
} else {
|
2015-03-11 06:54:02 +03:00
|
|
|
addr = fmt.Sprintf("%s:%d", *ip, *port)
|
2014-06-21 18:12:37 +04:00
|
|
|
}
|
|
|
|
|
2015-03-11 06:54:02 +03:00
|
|
|
c := goredis.NewClient(addr, "")
|
|
|
|
c.SetMaxIdleConns(1)
|
2014-07-22 12:37:34 +04:00
|
|
|
sendSelect(c, *dbn)
|
|
|
|
|
2014-07-02 06:13:34 +04:00
|
|
|
reg, _ := regexp.Compile(`'.*?'|".*?"|\S+`)
|
|
|
|
|
2014-07-17 07:08:14 +04:00
|
|
|
prompt := ""
|
|
|
|
|
2014-06-21 18:12:37 +04:00
|
|
|
for {
|
2014-07-22 12:37:34 +04:00
|
|
|
if *dbn > 0 && *dbn < 16 {
|
2015-08-02 04:23:51 +03:00
|
|
|
prompt = fmt.Sprintf("%s[%d]> ", addr, *dbn)
|
2014-07-17 07:08:14 +04:00
|
|
|
} else {
|
2015-08-02 04:23:51 +03:00
|
|
|
prompt = fmt.Sprintf("%s> ", addr)
|
2014-07-17 07:08:14 +04:00
|
|
|
}
|
|
|
|
|
2015-08-02 04:23:51 +03:00
|
|
|
cmd, err := line.Prompt(prompt)
|
2014-07-01 06:34:45 +04:00
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("%s\n", err.Error())
|
|
|
|
return
|
|
|
|
}
|
2014-06-21 18:12:37 +04:00
|
|
|
|
2014-07-02 06:13:34 +04:00
|
|
|
cmds := reg.FindAllString(cmd, -1)
|
2014-06-21 18:12:37 +04:00
|
|
|
if len(cmds) == 0 {
|
|
|
|
continue
|
|
|
|
} else {
|
2015-08-02 04:23:51 +03:00
|
|
|
line.AppendHistory(cmd)
|
2014-07-01 06:34:45 +04:00
|
|
|
|
2014-06-21 18:12:37 +04:00
|
|
|
args := make([]interface{}, len(cmds[1:]))
|
2014-07-18 08:20:25 +04:00
|
|
|
|
2014-06-21 18:12:37 +04:00
|
|
|
for i := range args {
|
2014-07-01 12:23:44 +04:00
|
|
|
args[i] = strings.Trim(string(cmds[1+i]), "\"'")
|
2014-06-21 18:12:37 +04:00
|
|
|
}
|
2014-07-17 08:06:07 +04:00
|
|
|
|
2014-08-25 10:18:23 +04:00
|
|
|
cmd := strings.ToLower(cmds[0])
|
|
|
|
if cmd == "help" || cmd == "?" {
|
2014-07-18 08:20:25 +04:00
|
|
|
printHelp(cmds)
|
2014-06-21 18:12:37 +04:00
|
|
|
} else {
|
2014-09-03 13:01:06 +04:00
|
|
|
r, err := c.Do(cmds[0], args...)
|
2014-07-22 12:37:34 +04:00
|
|
|
|
2014-09-03 13:01:06 +04:00
|
|
|
if err == nil && strings.ToLower(cmds[0]) == "select" {
|
|
|
|
*dbn, _ = strconv.Atoi(cmds[1])
|
2014-07-22 12:37:34 +04:00
|
|
|
}
|
|
|
|
|
2014-07-18 08:20:25 +04:00
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("%s", err.Error())
|
|
|
|
} else {
|
2014-08-25 10:18:23 +04:00
|
|
|
if cmd == "info" {
|
|
|
|
printInfo(r.([]byte))
|
|
|
|
} else {
|
2014-09-03 13:01:06 +04:00
|
|
|
printReply(0, r)
|
2014-08-25 10:18:23 +04:00
|
|
|
}
|
2014-07-18 08:20:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("\n")
|
2014-06-21 18:12:37 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-25 10:18:23 +04:00
|
|
|
func printInfo(s []byte) {
|
|
|
|
fmt.Printf("%s", s)
|
|
|
|
}
|
|
|
|
|
2014-09-03 13:01:06 +04:00
|
|
|
func printReply(level int, reply interface{}) {
|
2014-06-21 18:12:37 +04:00
|
|
|
switch reply := reply.(type) {
|
|
|
|
case int64:
|
|
|
|
fmt.Printf("(integer) %d", reply)
|
|
|
|
case string:
|
2014-06-25 10:22:31 +04:00
|
|
|
fmt.Printf("%s", reply)
|
2014-06-21 18:12:37 +04:00
|
|
|
case []byte:
|
2014-07-16 22:18:53 +04:00
|
|
|
fmt.Printf("%q", reply)
|
2014-06-21 18:12:37 +04:00
|
|
|
case nil:
|
2014-06-25 10:22:31 +04:00
|
|
|
fmt.Printf("(nil)")
|
2015-03-11 06:54:02 +03:00
|
|
|
case goredis.Error:
|
2014-06-21 18:12:37 +04:00
|
|
|
fmt.Printf("%s", string(reply))
|
|
|
|
case []interface{}:
|
|
|
|
for i, v := range reply {
|
2014-09-03 13:52:38 +04:00
|
|
|
if i != 0 {
|
|
|
|
fmt.Printf("%s", strings.Repeat(" ", level*4))
|
|
|
|
}
|
|
|
|
|
|
|
|
s := fmt.Sprintf("%d) ", i+1)
|
|
|
|
fmt.Printf("%-4s", s)
|
|
|
|
|
2014-09-03 13:01:06 +04:00
|
|
|
printReply(level+1, v)
|
2014-06-22 06:21:53 +04:00
|
|
|
if i != len(reply)-1 {
|
|
|
|
fmt.Printf("\n")
|
|
|
|
}
|
2014-06-21 18:12:37 +04:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
fmt.Printf("invalid ledis reply")
|
|
|
|
}
|
|
|
|
}
|
2014-07-18 08:20:25 +04:00
|
|
|
|
|
|
|
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])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-07-22 12:37:34 +04:00
|
|
|
|
2015-03-11 06:54:02 +03:00
|
|
|
func sendSelect(client *goredis.Client, index int) {
|
2014-07-22 12:37:34 +04:00
|
|
|
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 {
|
2014-07-24 08:07:54 +04:00
|
|
|
fmt.Printf("%s\n", err.Error())
|
2014-07-22 12:37:34 +04:00
|
|
|
}
|
|
|
|
}
|
2014-07-22 15:54:17 +04:00
|
|
|
|
2015-08-02 04:23:51 +03:00
|
|
|
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])
|
|
|
|
}
|
2014-07-22 15:54:17 +04:00
|
|
|
}
|
2015-08-02 04:23:51 +03:00
|
|
|
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)
|
|
|
|
} else {
|
|
|
|
line.WriteHistory(f)
|
|
|
|
f.Close()
|
2014-07-22 15:54:17 +04:00
|
|
|
}
|
|
|
|
}
|