forked from zpsean/go4api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gredis.go
156 lines (124 loc) · 3.21 KB
/
gredis.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
* go4api - an api testing tool written in Go
* Created by: Ping Zhu 2018
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
*/
package gredis
import (
"fmt"
"strings"
"github.com/Aysnine/go4api/cmd"
"github.com/Aysnine/go4api/utils"
redigo "github.com/gomodule/redigo/redis"
)
var RedisCons map[string]redigo.Conn
type RedisExec struct {
TargetRedis string
CmdStr string
CmdKey string
CmdValue string
CmdAffectedCount int
CmdResults interface{}
}
func InitRedisConnection() {
RedisCons = make(map[string]redigo.Conn)
reds := cmd.GetRedisConfig()
for k, v := range reds {
envMap := utils.GetOsEnviron()
ip := renderValue(v.Ip, envMap)
port := renderValue(fmt.Sprint(v.Port), envMap)
password := renderValue(v.Password, envMap)
// defaultRedisDb := os.Getenv("go4_qa_db_defaultRedisDb")
c, err := redigo.Dial("tcp", ip+":"+fmt.Sprint(port))
if err != nil {
panic(err)
}
// defer c.Close()
if len(password) > 0 {
if _, err = c.Do("AUTH", password); err != nil {
c.Close()
panic(err)
}
}
_, err = c.Do("PING")
if err != nil {
panic(err)
}
key := strings.ToLower(k)
RedisCons[key] = c
}
}
func Run(cmdStr string, cmdKey string, cmdValue string) (int, interface{}, string) {
var err error
redExecStatus := ""
tDb := "master"
redisExec := &RedisExec{tDb, cmdStr, cmdKey, cmdValue, 0, ""}
err = redisExec.Do()
if err == nil {
redExecStatus = "cmdSuccess"
} else {
redExecStatus = "cmdFailed"
}
return redisExec.CmdAffectedCount, redisExec.CmdResults, redExecStatus
}
func (redisExec *RedisExec) Do() error {
c := RedisCons[redisExec.TargetRedis]
var err error
var res interface{}
// to support del, set, get first
switch strings.ToUpper(redisExec.CmdStr) {
case "SET":
res, err = c.Do(redisExec.CmdStr, redisExec.CmdKey, redisExec.CmdValue)
if err == nil {
r, _ := redigo.String(res, nil)
redisExec.CmdAffectedCount = 1
redisExec.CmdResults = r
}
case "GET":
res, err = c.Do(redisExec.CmdStr, redisExec.CmdKey)
if err == nil {
r, _ := redigo.String(res, nil)
redisExec.CmdAffectedCount = 1
redisExec.CmdResults = r
}
case "DEL":
res, err = c.Do(redisExec.CmdStr, redisExec.CmdKey)
if err == nil {
r, _ := redigo.Int(res, nil)
redisExec.CmdAffectedCount = 1
redisExec.CmdResults = r
}
case "EXISTS":
res, err = c.Do(redisExec.CmdStr, redisExec.CmdKey)
if err == nil {
r, _ := redigo.Int(res, nil)
redisExec.CmdAffectedCount = 1
redisExec.CmdResults = r
}
case "KEYS":
res, err = c.Do(redisExec.CmdStr, redisExec.CmdKey)
if err == nil {
r, _ := redigo.Strings(res, nil)
redisExec.CmdAffectedCount = 1
redisExec.CmdResults = r
}
default:
redisExec.CmdAffectedCount = -1
fmt.Println("!! Warning, Command ", redisExec.CmdStr, " is not supported currently, will enhance it later")
}
return err
}
func renderValue(jsonStr string, feeder map[string]string) string {
s := jsonStr
for key, value := range feeder {
k := "${" + key + "}"
if k == s {
s = strings.Replace(s, k, value, -1)
}
}
return s
}