diff --git a/cmd/ledis-cli/main.go b/cmd/ledis-cli/main.go index 1b06f00..e26af0b 100644 --- a/cmd/ledis-cli/main.go +++ b/cmd/ledis-cli/main.go @@ -12,7 +12,7 @@ import ( 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 dbnum = flag.Int("n", 0, "ledisdb database number(default 0)") +var dbn = flag.Int("n", 0, "ledisdb database number(default 0)") func main() { flag.Parse() @@ -35,8 +35,12 @@ func main() { prompt := "" for { - if *dbnum != 0 { - prompt = fmt.Sprintf("%s[%d]>", cfg.Addr, *dbnum) + if *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) } else { prompt = fmt.Sprintf("%s>", cfg.Addr) } @@ -58,11 +62,12 @@ func main() { args[i] = strings.Trim(string(cmds[1+i]), "\"'") } r, err := c.Do(cmds[0], args...) - if strings.ToLower(cmds[0]) == "select" { - *dbnum, _ = strconv.Atoi(cmds[1]) - } + if err != nil { 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 { printReply(cmd, r) }