-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredis.go
69 lines (60 loc) · 1.27 KB
/
redis.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package main
import (
"fmt"
"strconv"
"github.com/go-redis/redis"
"github.com/urfave/cli/v2"
)
func getRedisClient(addr, pass string, db int) *redis.Client {
return redis.NewClient(&redis.Options{
Addr: addr,
Password: pass,
DB: db,
})
}
func NewRedisCommand() *cli.Command {
return &cli.Command{
Name: "redis",
Aliases: []string{"r"},
Usage: "redis",
UsageText: "redis <addr> <db> <operate> <key> [pass]. \noperate:Get",
Action: func(c *cli.Context) error {
if c.NArg() < 4 {
fmt.Println("Periodic incomplete")
return nil
}
addr := c.Args().Get(0)
dbStr := c.Args().Get(1)
db, err := strconv.Atoi(dbStr)
if err != nil {
return err
}
operate := c.Args().Get(2)
key := c.Args().Get(3)
var pass string
if c.NArg() == 5 {
pass = c.Args().Get(4)
}
r := RedisGet(addr, pass, key, operate, db)
fmt.Println(r)
return nil
},
}
}
const Get = "Get"
func RedisGet(addr, pass, key, operate string, db int) string {
client := getRedisClient(addr, pass, db)
switch operate {
case Get:
r, err := client.Get(key).Result()
if err != nil {
if err.Error() == "redis: nil" {
return r
}
panic(err)
}
return r
default:
return "This operation type is not currently supported"
}
}