2021-07-28 18:03:19 +08:00
|
|
|
|
package application
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
2023-11-07 21:05:21 +08:00
|
|
|
|
"context"
|
2023-12-05 23:03:51 +08:00
|
|
|
|
"mayfly-go/internal/common/consts"
|
2024-10-28 12:13:41 +08:00
|
|
|
|
"mayfly-go/internal/common/utils"
|
2024-03-02 19:08:19 +08:00
|
|
|
|
flowapp "mayfly-go/internal/flow/application"
|
|
|
|
|
|
flowentity "mayfly-go/internal/flow/domain/entity"
|
2024-05-09 21:29:34 +08:00
|
|
|
|
"mayfly-go/internal/redis/application/dto"
|
2022-09-09 18:26:08 +08:00
|
|
|
|
"mayfly-go/internal/redis/domain/entity"
|
|
|
|
|
|
"mayfly-go/internal/redis/domain/repository"
|
2024-11-20 22:43:53 +08:00
|
|
|
|
"mayfly-go/internal/redis/imsg"
|
2023-10-27 17:41:45 +08:00
|
|
|
|
"mayfly-go/internal/redis/rdm"
|
2023-12-05 23:03:51 +08:00
|
|
|
|
tagapp "mayfly-go/internal/tag/application"
|
2024-05-09 21:29:34 +08:00
|
|
|
|
tagdto "mayfly-go/internal/tag/application/dto"
|
2024-04-13 17:01:12 +08:00
|
|
|
|
tagentity "mayfly-go/internal/tag/domain/entity"
|
2023-10-26 17:15:49 +08:00
|
|
|
|
"mayfly-go/pkg/base"
|
|
|
|
|
|
"mayfly-go/pkg/errorx"
|
2024-03-02 19:08:19 +08:00
|
|
|
|
"mayfly-go/pkg/logx"
|
2022-06-02 17:41:11 +08:00
|
|
|
|
"mayfly-go/pkg/model"
|
2024-10-16 17:24:50 +08:00
|
|
|
|
"mayfly-go/pkg/utils/collx"
|
2024-03-02 19:08:19 +08:00
|
|
|
|
"mayfly-go/pkg/utils/jsonx"
|
2023-12-05 23:03:51 +08:00
|
|
|
|
"mayfly-go/pkg/utils/stringx"
|
2022-09-29 13:14:50 +08:00
|
|
|
|
"strconv"
|
2022-07-10 12:14:06 +08:00
|
|
|
|
"strings"
|
2024-03-02 19:08:19 +08:00
|
|
|
|
|
2024-04-13 17:01:12 +08:00
|
|
|
|
"github.com/may-fly/cast"
|
2024-03-02 19:08:19 +08:00
|
|
|
|
"github.com/redis/go-redis/v9"
|
2021-07-28 18:03:19 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type Redis interface {
|
2023-10-26 17:15:49 +08:00
|
|
|
|
base.App[*entity.Redis]
|
2024-03-02 19:08:19 +08:00
|
|
|
|
flowapp.FlowBizHandler
|
2023-10-26 17:15:49 +08:00
|
|
|
|
|
2021-07-28 18:03:19 +08:00
|
|
|
|
// 分页获取机器脚本信息列表
|
2023-10-26 17:15:49 +08:00
|
|
|
|
GetPageList(condition *entity.RedisQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error)
|
2021-07-28 18:03:19 +08:00
|
|
|
|
|
2023-11-12 20:14:44 +08:00
|
|
|
|
// 测试连接
|
2024-05-09 21:29:34 +08:00
|
|
|
|
TestConn(re *dto.SaveRedis) error
|
2023-11-12 20:14:44 +08:00
|
|
|
|
|
2024-05-09 21:29:34 +08:00
|
|
|
|
SaveRedis(ctx context.Context, param *dto.SaveRedis) error
|
2021-07-28 18:03:19 +08:00
|
|
|
|
|
|
|
|
|
|
// 删除数据库信息
|
2023-11-07 21:05:21 +08:00
|
|
|
|
Delete(ctx context.Context, id uint64) error
|
2021-07-28 18:03:19 +08:00
|
|
|
|
|
|
|
|
|
|
// 获取数据库连接实例
|
2022-09-29 13:14:50 +08:00
|
|
|
|
// id: 数据库实例id
|
|
|
|
|
|
// db: 库号
|
2023-10-27 17:41:45 +08:00
|
|
|
|
GetRedisConn(id uint64, db int) (*rdm.RedisConn, error)
|
2024-03-02 19:08:19 +08:00
|
|
|
|
|
|
|
|
|
|
// 执行redis命令
|
2024-05-09 21:29:34 +08:00
|
|
|
|
RunCmd(ctx context.Context, redisConn *rdm.RedisConn, cmdParam *dto.RunCmd) (any, error)
|
2021-07-28 18:03:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-09-09 18:26:08 +08:00
|
|
|
|
type redisAppImpl struct {
|
2023-10-26 17:15:49 +08:00
|
|
|
|
base.AppImpl[*entity.Redis, repository.Redis]
|
2023-12-05 23:03:51 +08:00
|
|
|
|
|
2024-04-13 17:01:12 +08:00
|
|
|
|
tagApp tagapp.TagTree `inject:"TagTreeApp"`
|
2024-05-08 21:04:25 +08:00
|
|
|
|
procdefApp flowapp.Procdef `inject:"ProcdefApp"`
|
2024-04-13 17:01:12 +08:00
|
|
|
|
resourceAuthCertApp tagapp.ResourceAuthCert `inject:"ResourceAuthCertApp"`
|
2024-01-21 22:52:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-10-27 17:41:45 +08:00
|
|
|
|
// 分页获取redis列表
|
2023-10-26 17:15:49 +08:00
|
|
|
|
func (r *redisAppImpl) GetPageList(condition *entity.RedisQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error) {
|
|
|
|
|
|
return r.GetRepo().GetRedisList(condition, pageParam, toEntity, orderBy...)
|
2021-07-28 18:03:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-05-09 21:29:34 +08:00
|
|
|
|
func (r *redisAppImpl) TestConn(param *dto.SaveRedis) error {
|
2023-11-12 20:14:44 +08:00
|
|
|
|
db := 0
|
2024-04-13 17:01:12 +08:00
|
|
|
|
re := param.Redis
|
2023-11-12 20:14:44 +08:00
|
|
|
|
if re.Db != "" {
|
2024-04-13 17:01:12 +08:00
|
|
|
|
db = cast.ToInt(strings.Split(re.Db, ",")[0])
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-07-05 13:14:31 +08:00
|
|
|
|
authCert, err := r.resourceAuthCertApp.GetRealAuthCert(param.AuthCert)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return err
|
2022-07-18 20:36:31 +08:00
|
|
|
|
}
|
2021-07-28 18:03:19 +08:00
|
|
|
|
|
2024-04-13 17:01:12 +08:00
|
|
|
|
rc, err := re.ToRedisInfo(db, authCert).Conn()
|
2023-11-12 20:14:44 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
rc.Close()
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-05-09 21:29:34 +08:00
|
|
|
|
func (r *redisAppImpl) SaveRedis(ctx context.Context, param *dto.SaveRedis) error {
|
2024-04-13 17:01:12 +08:00
|
|
|
|
re := param.Redis
|
2024-04-17 21:28:28 +08:00
|
|
|
|
tagCodePaths := param.TagCodePaths
|
2021-07-28 18:03:19 +08:00
|
|
|
|
// 查找是否存在该库
|
2023-12-11 01:00:09 +08:00
|
|
|
|
oldRedis := &entity.Redis{
|
|
|
|
|
|
Host: re.Host,
|
|
|
|
|
|
SshTunnelMachineId: re.SshTunnelMachineId,
|
2023-03-17 09:46:41 +08:00
|
|
|
|
}
|
2024-04-28 23:45:57 +08:00
|
|
|
|
err := r.GetByCond(oldRedis)
|
2021-07-28 18:03:19 +08:00
|
|
|
|
|
|
|
|
|
|
if re.Id == 0 {
|
|
|
|
|
|
if err == nil {
|
2024-11-20 22:43:53 +08:00
|
|
|
|
return errorx.NewBizI(ctx, imsg.ErrRedisInfoExist)
|
2021-07-28 18:03:19 +08:00
|
|
|
|
}
|
2024-10-16 17:24:50 +08:00
|
|
|
|
// 生成随机编号
|
|
|
|
|
|
re.Code = stringx.Rand(10)
|
|
|
|
|
|
|
2023-12-05 23:03:51 +08:00
|
|
|
|
return r.Tx(ctx, func(ctx context.Context) error {
|
|
|
|
|
|
return r.Insert(ctx, re)
|
|
|
|
|
|
}, func(ctx context.Context) error {
|
2024-05-09 21:29:34 +08:00
|
|
|
|
return r.tagApp.SaveResourceTag(ctx, &tagdto.SaveResourceTag{
|
|
|
|
|
|
ResourceTag: &tagdto.ResourceTag{
|
2024-04-17 21:28:28 +08:00
|
|
|
|
Type: tagentity.TagTypeRedis,
|
|
|
|
|
|
Code: re.Code,
|
|
|
|
|
|
Name: re.Name,
|
|
|
|
|
|
},
|
|
|
|
|
|
ParentTagCodePaths: tagCodePaths,
|
2024-04-06 18:19:17 +08:00
|
|
|
|
})
|
2024-04-13 17:01:12 +08:00
|
|
|
|
}, func(ctx context.Context) error {
|
2024-05-16 17:26:32 +08:00
|
|
|
|
return r.resourceAuthCertApp.RelateAuthCert(ctx, &tagdto.RelateAuthCert{
|
2024-04-13 17:01:12 +08:00
|
|
|
|
ResourceCode: re.Code,
|
|
|
|
|
|
ResourceType: tagentity.TagTypeRedis,
|
|
|
|
|
|
AuthCerts: []*tagentity.ResourceAuthCert{param.AuthCert},
|
|
|
|
|
|
})
|
2023-12-05 23:03:51 +08:00
|
|
|
|
})
|
2021-07-28 18:03:19 +08:00
|
|
|
|
}
|
2023-10-26 17:15:49 +08:00
|
|
|
|
|
|
|
|
|
|
// 如果存在该库,则校验修改的库是否为该库
|
|
|
|
|
|
if err == nil && oldRedis.Id != re.Id {
|
2024-11-20 22:43:53 +08:00
|
|
|
|
return errorx.NewBizI(ctx, imsg.ErrRedisInfoExist)
|
2023-10-26 17:15:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
// 如果修改了redis实例的库信息,则关闭旧库的连接
|
2024-05-08 21:04:25 +08:00
|
|
|
|
if oldRedis.Db != re.Db || oldRedis.SshTunnelMachineId != re.SshTunnelMachineId {
|
2023-10-26 17:15:49 +08:00
|
|
|
|
for _, dbStr := range strings.Split(oldRedis.Db, ",") {
|
|
|
|
|
|
db, _ := strconv.Atoi(dbStr)
|
2023-10-27 17:41:45 +08:00
|
|
|
|
rdm.CloseConn(re.Id, db)
|
2023-10-26 17:15:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-10-16 17:24:50 +08:00
|
|
|
|
// 如果调整了host sshid等会查不到旧数据,故需要根据id获取旧信息将code赋值给标签进行关联
|
2023-12-20 23:01:51 +08:00
|
|
|
|
if oldRedis.Code == "" {
|
2024-05-05 14:53:30 +08:00
|
|
|
|
oldRedis, _ = r.GetById(re.Id)
|
2023-12-20 23:01:51 +08:00
|
|
|
|
}
|
2023-12-05 23:03:51 +08:00
|
|
|
|
|
2024-04-06 18:19:17 +08:00
|
|
|
|
re.Code = ""
|
2023-12-05 23:03:51 +08:00
|
|
|
|
return r.Tx(ctx, func(ctx context.Context) error {
|
|
|
|
|
|
return r.UpdateById(ctx, re)
|
|
|
|
|
|
}, func(ctx context.Context) error {
|
2024-04-17 21:28:28 +08:00
|
|
|
|
if oldRedis.Name != re.Name {
|
2024-10-16 17:24:50 +08:00
|
|
|
|
if err := r.tagApp.UpdateTagName(ctx, tagentity.TagTypeRedis, oldRedis.Code, re.Name); err != nil {
|
2024-04-17 21:28:28 +08:00
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-05-09 21:29:34 +08:00
|
|
|
|
return r.tagApp.SaveResourceTag(ctx, &tagdto.SaveResourceTag{
|
|
|
|
|
|
ResourceTag: &tagdto.ResourceTag{
|
2024-04-17 21:28:28 +08:00
|
|
|
|
Type: tagentity.TagTypeRedis,
|
|
|
|
|
|
Code: oldRedis.Code,
|
|
|
|
|
|
Name: re.Name,
|
|
|
|
|
|
},
|
|
|
|
|
|
ParentTagCodePaths: tagCodePaths,
|
2024-04-06 18:19:17 +08:00
|
|
|
|
})
|
2024-04-13 17:01:12 +08:00
|
|
|
|
}, func(ctx context.Context) error {
|
2024-05-16 17:26:32 +08:00
|
|
|
|
return r.resourceAuthCertApp.RelateAuthCert(ctx, &tagdto.RelateAuthCert{
|
2024-04-13 17:01:12 +08:00
|
|
|
|
ResourceCode: oldRedis.Code,
|
|
|
|
|
|
ResourceType: tagentity.TagTypeRedis,
|
|
|
|
|
|
AuthCerts: []*tagentity.ResourceAuthCert{param.AuthCert},
|
|
|
|
|
|
})
|
2023-12-05 23:03:51 +08:00
|
|
|
|
})
|
2021-07-28 18:03:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 删除Redis信息
|
2023-11-07 21:05:21 +08:00
|
|
|
|
func (r *redisAppImpl) Delete(ctx context.Context, id uint64) error {
|
2024-05-05 14:53:30 +08:00
|
|
|
|
re, err := r.GetById(id)
|
2023-10-26 17:15:49 +08:00
|
|
|
|
if err != nil {
|
2024-11-20 22:43:53 +08:00
|
|
|
|
return errorx.NewBiz("redis not found")
|
2023-10-26 17:15:49 +08:00
|
|
|
|
}
|
2022-09-29 13:14:50 +08:00
|
|
|
|
// 如果存在连接,则关闭所有库连接信息
|
|
|
|
|
|
for _, dbStr := range strings.Split(re.Db, ",") {
|
|
|
|
|
|
db, _ := strconv.Atoi(dbStr)
|
2023-10-27 17:41:45 +08:00
|
|
|
|
rdm.CloseConn(re.Id, db)
|
2022-09-29 13:14:50 +08:00
|
|
|
|
}
|
2023-12-13 14:01:13 +08:00
|
|
|
|
|
|
|
|
|
|
return r.Tx(ctx, func(ctx context.Context) error {
|
|
|
|
|
|
return r.DeleteById(ctx, id)
|
|
|
|
|
|
}, func(ctx context.Context) error {
|
2024-05-09 21:29:34 +08:00
|
|
|
|
return r.tagApp.SaveResourceTag(ctx, &tagdto.SaveResourceTag{
|
|
|
|
|
|
ResourceTag: &tagdto.ResourceTag{
|
2024-04-17 21:28:28 +08:00
|
|
|
|
Type: tagentity.TagTypeRedis,
|
|
|
|
|
|
Code: re.Code,
|
|
|
|
|
|
},
|
2024-04-06 18:19:17 +08:00
|
|
|
|
})
|
2024-04-13 17:01:12 +08:00
|
|
|
|
}, func(ctx context.Context) error {
|
2024-05-16 17:26:32 +08:00
|
|
|
|
return r.resourceAuthCertApp.RelateAuthCert(ctx, &tagdto.RelateAuthCert{
|
2024-04-13 17:01:12 +08:00
|
|
|
|
ResourceCode: re.Code,
|
|
|
|
|
|
ResourceType: tagentity.TagTypeRedis,
|
|
|
|
|
|
})
|
2023-12-13 14:01:13 +08:00
|
|
|
|
})
|
2021-07-28 18:03:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 获取数据库连接实例
|
2023-10-27 17:41:45 +08:00
|
|
|
|
func (r *redisAppImpl) GetRedisConn(id uint64, db int) (*rdm.RedisConn, error) {
|
|
|
|
|
|
return rdm.GetRedisConn(id, db, func() (*rdm.RedisInfo, error) {
|
|
|
|
|
|
// 缓存不存在,则回调获取redis信息
|
2024-05-05 14:53:30 +08:00
|
|
|
|
re, err := r.GetById(id)
|
2023-10-26 17:15:49 +08:00
|
|
|
|
if err != nil {
|
2024-11-20 22:43:53 +08:00
|
|
|
|
return nil, errorx.NewBiz("redis not found")
|
2023-10-26 17:15:49 +08:00
|
|
|
|
}
|
2024-04-13 17:01:12 +08:00
|
|
|
|
authCert, err := r.resourceAuthCertApp.GetResourceAuthCert(tagentity.TagTypeRedis, re.Code)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
2024-01-05 08:55:34 +08:00
|
|
|
|
}
|
2024-04-13 17:01:12 +08:00
|
|
|
|
return re.ToRedisInfo(db, authCert, r.tagApp.ListTagPathByTypeAndCode(consts.ResourceTypeRedis, re.Code)...), nil
|
2021-08-18 17:57:33 +08:00
|
|
|
|
})
|
2021-07-28 18:03:19 +08:00
|
|
|
|
}
|
2024-03-02 19:08:19 +08:00
|
|
|
|
|
2024-05-09 21:29:34 +08:00
|
|
|
|
func (r *redisAppImpl) RunCmd(ctx context.Context, redisConn *rdm.RedisConn, cmdParam *dto.RunCmd) (any, error) {
|
2024-03-02 19:08:19 +08:00
|
|
|
|
if redisConn == nil {
|
2024-11-20 22:43:53 +08:00
|
|
|
|
return nil, errorx.NewBiz("redis connection not exist")
|
2024-03-02 19:08:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-10-16 17:24:50 +08:00
|
|
|
|
// 开启工单流程,则校验该流程是否需要校验
|
|
|
|
|
|
if procdef := r.procdefApp.GetProcdefByCodePath(ctx, redisConn.Info.CodePath...); procdef != nil {
|
|
|
|
|
|
cmd := cmdParam.Cmd[0]
|
|
|
|
|
|
cmdType := "read"
|
|
|
|
|
|
if rdm.IsWriteCmd(cmd) {
|
|
|
|
|
|
cmdType = "write"
|
|
|
|
|
|
}
|
|
|
|
|
|
if needStartProc := procdef.MatchCondition(RedisRunCmdFlowBizType, collx.Kvs("cmdType", cmdType, "cmd", cmd)); needStartProc {
|
2024-11-20 22:43:53 +08:00
|
|
|
|
return nil, errorx.NewBizI(ctx, imsg.ErrSubmitFlowRunCmd)
|
2024-03-02 19:08:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
res, err := redisConn.RunCmd(ctx, cmdParam.Cmd...)
|
|
|
|
|
|
// 获取的key不存在,不报错
|
|
|
|
|
|
if err == redis.Nil {
|
|
|
|
|
|
return nil, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
return res, err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-10-16 17:24:50 +08:00
|
|
|
|
type FlowRedisRunCmdBizForm struct {
|
|
|
|
|
|
Id uint64 `json:"id"` // redis id
|
|
|
|
|
|
Db int `json:"db"` // redis db
|
|
|
|
|
|
Cmd string `json:"cmd"` // redis cmd
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (r *redisAppImpl) FlowBizHandle(ctx context.Context, bizHandleParam *flowapp.BizHandleParam) (any, error) {
|
|
|
|
|
|
procinst := bizHandleParam.Procinst
|
|
|
|
|
|
bizKey := procinst.BizKey
|
|
|
|
|
|
procinstStatus := procinst.Status
|
2024-03-02 19:08:19 +08:00
|
|
|
|
|
|
|
|
|
|
logx.Debugf("RedisRunWriteCmd FlowBizHandle -> bizKey: %s, procinstStatus: %s", bizKey, flowentity.ProcinstStatusEnum.GetDesc(procinstStatus))
|
|
|
|
|
|
// 流程非完成状态,不处理
|
|
|
|
|
|
if procinstStatus != flowentity.ProcinstStatusCompleted {
|
2024-10-16 17:24:50 +08:00
|
|
|
|
return nil, nil
|
2024-03-02 19:08:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-10-16 17:24:50 +08:00
|
|
|
|
runCmdParam, err := jsonx.To(procinst.BizForm, new(FlowRedisRunCmdBizForm))
|
2024-03-02 19:08:19 +08:00
|
|
|
|
if err != nil {
|
2024-11-20 22:43:53 +08:00
|
|
|
|
return nil, errorx.NewBiz("failed to parse the business form information: %s", err.Error())
|
2024-03-02 19:08:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
redisConn, err := r.GetRedisConn(runCmdParam.Id, runCmdParam.Db)
|
|
|
|
|
|
if err != nil {
|
2024-10-16 17:24:50 +08:00
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
handleRes := make([]map[string]any, 0)
|
|
|
|
|
|
hasErr := false
|
2024-10-28 12:13:41 +08:00
|
|
|
|
|
|
|
|
|
|
utils.SplitStmts(strings.NewReader(runCmdParam.Cmd), func(stmt string) error {
|
|
|
|
|
|
cmd := strings.TrimSpace(stmt)
|
2024-10-16 17:24:50 +08:00
|
|
|
|
runRes := collx.Kvs("cmd", cmd)
|
|
|
|
|
|
if res, err := redisConn.RunCmd(ctx, collx.ArrayMap[string, any](parseRedisCommand(cmd), func(val string) any { return val })...); err != nil {
|
|
|
|
|
|
runRes["res"] = err.Error()
|
|
|
|
|
|
hasErr = true
|
|
|
|
|
|
} else {
|
|
|
|
|
|
runRes["res"] = res
|
|
|
|
|
|
}
|
|
|
|
|
|
handleRes = append(handleRes, runRes)
|
2024-10-28 12:13:41 +08:00
|
|
|
|
return nil
|
|
|
|
|
|
})
|
2024-10-16 17:24:50 +08:00
|
|
|
|
|
|
|
|
|
|
if hasErr {
|
2024-11-20 22:43:53 +08:00
|
|
|
|
return handleRes, errorx.NewBizI(ctx, imsg.ErrHasRunFailCmd)
|
2024-10-16 17:24:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
return handleRes, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// parseRedisCommand 解析 Redis 命令字符串到数组
|
|
|
|
|
|
func parseRedisCommand(commandStr string) []string {
|
|
|
|
|
|
var args []string
|
|
|
|
|
|
inSingleQuote := false
|
|
|
|
|
|
inDoubleQuote := false
|
|
|
|
|
|
currentArg := ""
|
|
|
|
|
|
|
|
|
|
|
|
for _, char := range commandStr {
|
|
|
|
|
|
switch char {
|
|
|
|
|
|
case '\'':
|
|
|
|
|
|
if !inDoubleQuote && !inSingleQuote {
|
|
|
|
|
|
inSingleQuote = true
|
|
|
|
|
|
} else if inSingleQuote && !inDoubleQuote {
|
|
|
|
|
|
inSingleQuote = false
|
|
|
|
|
|
args = append(args, strings.TrimSpace(currentArg))
|
|
|
|
|
|
currentArg = ""
|
|
|
|
|
|
}
|
|
|
|
|
|
case '"':
|
|
|
|
|
|
if !inSingleQuote && !inDoubleQuote {
|
|
|
|
|
|
inDoubleQuote = true
|
|
|
|
|
|
} else if !inSingleQuote && inDoubleQuote {
|
|
|
|
|
|
inDoubleQuote = false
|
|
|
|
|
|
args = append(args, strings.TrimSpace(currentArg))
|
|
|
|
|
|
currentArg = ""
|
|
|
|
|
|
}
|
|
|
|
|
|
case ' ':
|
|
|
|
|
|
if !inSingleQuote && !inDoubleQuote {
|
|
|
|
|
|
if strings.TrimSpace(currentArg) != "" {
|
|
|
|
|
|
args = append(args, strings.TrimSpace(currentArg))
|
|
|
|
|
|
currentArg = ""
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
currentArg += string(char)
|
|
|
|
|
|
}
|
|
|
|
|
|
default:
|
|
|
|
|
|
currentArg += string(char)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if strings.TrimSpace(currentArg) != "" {
|
|
|
|
|
|
args = append(args, strings.TrimSpace(currentArg))
|
2024-03-02 19:08:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-10-16 17:24:50 +08:00
|
|
|
|
return args
|
2024-03-02 19:08:19 +08:00
|
|
|
|
}
|