github.com/flower-corp/rosedb@v1.1.2-0.20230117132829-21dc4f7b319a/cmd/cli.go (about) 1 package main 2 3 import ( 4 "strings" 5 6 "github.com/flower-corp/rosedb" 7 "github.com/tidwall/redcon" 8 ) 9 10 type cmdHandler func(cli *Client, args [][]byte) (interface{}, error) 11 12 var supportedCommands = map[string]cmdHandler{ 13 // string commands 14 "set": set, 15 "get": get, 16 "mget": mGet, 17 "getrange": getRange, 18 "getdel": getDel, 19 "setex": setEX, 20 "setnx": setNX, 21 "mset": mSet, 22 "msetnx": mSetNX, 23 "append": appendStr, 24 "decr": decr, 25 "decrby": decrBy, 26 "incr": incr, 27 "incrby": incrBy, 28 "strlen": strLen, 29 30 // list 31 "lpush": lPush, 32 "lpushx": lPushX, 33 "rpush": rPush, 34 "rpushx": rPushX, 35 "lpop": lPop, 36 "rpop": rPop, 37 "lmove": lMove, 38 "llen": lLen, 39 "lindex": lIndex, 40 "lset": lSet, 41 "lrange": lRange, 42 "lrem": lRem, 43 44 // hash commands 45 "hset": hSet, 46 "hsetnx": hSetNX, 47 "hget": hGet, 48 "hmget": hmGet, 49 "hdel": hDel, 50 "hexists": hExists, 51 "hlen": hLen, 52 "hkeys": hKeys, 53 "hvals": hVals, 54 "hgetall": hGetAll, 55 "hstrlen": hStrLen, 56 "hscan": hScan, 57 "hincrby": hIncrBy, 58 "hrandfield": hRandField, 59 60 // set commands 61 "sadd": sAdd, 62 "spop": sPop, 63 "srem": sRem, 64 "sismember": sIsMember, 65 "smismember": sMisMember, 66 "smembers": sMembers, 67 "scard": sCard, 68 "sdiff": sDiff, 69 "sdiffstore": sDiffStore, 70 "sunion": sUnion, 71 "sunionstore": sUnionStore, 72 "sinter": sInter, 73 "sinterstore": sInterStore, 74 75 // zset commands 76 "zadd": zAdd, 77 "zscore": zScore, 78 "zrem": zRem, 79 "zcard": zCard, 80 "zrange": zRange, 81 "zrevrange": zRevRange, 82 "zrank": zRank, 83 "zrevrank": zRevRank, 84 85 // generic commands 86 "type": keyType, 87 "del": del, 88 89 // connection management commands 90 "select": selectDB, 91 "ping": ping, 92 "quit": nil, 93 94 // server management commands 95 "info": info, 96 } 97 98 type Client struct { 99 svr *Server 100 db *rosedb.RoseDB 101 } 102 103 func execClientCommand(conn redcon.Conn, cmd redcon.Command) { 104 command := strings.ToLower(string(cmd.Args[0])) 105 cmdFunc, ok := supportedCommands[command] 106 if !ok { 107 conn.WriteError("ERR unsupported command '" + string(cmd.Args[0]) + "'") 108 return 109 } 110 111 cli, _ := conn.Context().(*Client) 112 if cli == nil { 113 conn.WriteError(errClientIsNil.Error()) 114 return 115 } 116 switch command { 117 case "quit": 118 _ = conn.Close() 119 default: 120 if res, err := cmdFunc(cli, cmd.Args[1:]); err != nil { 121 if err == rosedb.ErrKeyNotFound { 122 conn.WriteNull() 123 } else { 124 conn.WriteError(err.Error()) 125 } 126 } else { 127 conn.WriteAny(res) 128 } 129 } 130 }