fix select bug

This commit is contained in:
holys 2014-07-22 16:37:34 +08:00
parent 325f153885
commit 31a069653f
1 changed files with 21 additions and 8 deletions

View File

@ -28,6 +28,8 @@ func main() {
c := ledis.NewClient(cfg) c := ledis.NewClient(cfg)
sendSelect(c, *dbn)
setHistoryCapacity(100) setHistoryCapacity(100)
reg, _ := regexp.Compile(`'.*?'|".*?"|\S+`) reg, _ := regexp.Compile(`'.*?'|".*?"|\S+`)
@ -35,11 +37,7 @@ func main() {
prompt := "" prompt := ""
for { for {
if *dbn >= 16 { if *dbn > 0 && *dbn < 16 {
fmt.Printf("ERR invalid db index %d. Auto switch back db 0.\n", *dbn)
*dbn = 0
continue
} else if *dbn > 0 && *dbn < 16 {
prompt = fmt.Sprintf("%s[%d]>", cfg.Addr, *dbn) prompt = fmt.Sprintf("%s[%d]>", cfg.Addr, *dbn)
} else { } else {
prompt = fmt.Sprintf("%s>", cfg.Addr) prompt = fmt.Sprintf("%s>", cfg.Addr)
@ -67,13 +65,17 @@ func main() {
if strings.ToLower(cmd) == "help" || cmd == "?" { if strings.ToLower(cmd) == "help" || cmd == "?" {
printHelp(cmds) printHelp(cmds)
} else { } else {
if len(cmds) == 2 && strings.ToLower(cmds[0]) == "select" {
if db, _ := strconv.Atoi(cmds[1]); db < 16 && db >= 0 {
*dbn = db
}
}
r, err := c.Do(cmds[0], args...) r, err := c.Do(cmds[0], args...)
if err != nil { if err != nil {
fmt.Printf("%s", err.Error()) fmt.Printf("%s", err.Error())
} else if nb, _ := strconv.Atoi(cmds[1]); strings.ToLower(cmds[0]) == "select" && nb < 16 {
*dbn = nb
printReply(cmd, r)
} else { } else {
printReply(cmd, r) printReply(cmd, r)
} }
@ -144,3 +146,14 @@ func printHelp(cmds []string) {
} }
} }
} }
func sendSelect(client *ledis.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.Println("index out of range, should less than 16")
}
}