2014-06-21 18:12:37 +04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"github.com/siddontang/ledisdb/client/go/ledis"
|
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"
|
|
|
|
)
|
|
|
|
|
|
|
|
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 07:08:14 +04:00
|
|
|
var dbnum = flag.Int("n", 0, "ledisdb database number(default 0)")
|
2014-06-21 18:12:37 +04:00
|
|
|
|
|
|
|
func main() {
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
cfg := new(ledis.Config)
|
|
|
|
if len(*socket) > 0 {
|
|
|
|
cfg.Addr = *socket
|
|
|
|
} else {
|
|
|
|
cfg.Addr = fmt.Sprintf("%s:%d", *ip, *port)
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg.MaxIdleConns = 1
|
|
|
|
|
|
|
|
c := ledis.NewClient(cfg)
|
|
|
|
|
2014-07-01 06:34:45 +04:00
|
|
|
setHistoryCapacity(100)
|
2014-06-21 18:12:37 +04:00
|
|
|
|
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-17 07:08:14 +04:00
|
|
|
if *dbnum != 0 {
|
|
|
|
prompt = fmt.Sprintf("%s[%d]>", cfg.Addr, *dbnum)
|
|
|
|
} else {
|
|
|
|
prompt = fmt.Sprintf("%s>", cfg.Addr)
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd, err := line(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 {
|
2014-07-01 06:34:45 +04:00
|
|
|
addHistory(cmd)
|
|
|
|
|
2014-06-21 18:12:37 +04:00
|
|
|
args := make([]interface{}, len(cmds[1:]))
|
|
|
|
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
|
|
|
}
|
|
|
|
r, err := c.Do(cmds[0], args...)
|
2014-07-17 07:08:14 +04:00
|
|
|
if strings.ToLower(cmds[0]) == "select" {
|
|
|
|
*dbnum, _ = strconv.Atoi(cmds[1])
|
|
|
|
}
|
2014-06-21 18:12:37 +04:00
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("%s", err.Error())
|
|
|
|
} else {
|
2014-06-25 10:22:31 +04:00
|
|
|
printReply(cmd, r)
|
2014-06-21 18:12:37 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("\n")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-25 10:22:31 +04:00
|
|
|
func printReply(cmd string, 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)")
|
2014-06-21 18:12:37 +04:00
|
|
|
case ledis.Error:
|
|
|
|
fmt.Printf("%s", string(reply))
|
|
|
|
case []interface{}:
|
|
|
|
for i, v := range reply {
|
2014-06-22 06:21:53 +04:00
|
|
|
fmt.Printf("%d) ", i+1)
|
2014-06-21 18:12:37 +04:00
|
|
|
if v == nil {
|
|
|
|
fmt.Printf("(nil)")
|
|
|
|
} else {
|
|
|
|
fmt.Printf("%q", 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")
|
|
|
|
}
|
|
|
|
}
|