fix: error caused when execute lua script in cli #103

This commit is contained in:
Lykin 2023-12-13 12:22:41 +08:00
parent 08c42ca85c
commit c7a365e8e9
2 changed files with 18 additions and 2 deletions

View File

@ -2,6 +2,7 @@ package services
import ( import (
"context" "context"
"errors"
"fmt" "fmt"
"github.com/redis/go-redis/v9" "github.com/redis/go-redis/v9"
"github.com/wailsapp/wails/v2/pkg/runtime" "github.com/wailsapp/wails/v2/pkg/runtime"
@ -41,12 +42,12 @@ func Cli() *cliService {
} }
func (c *cliService) runCommand(server, data string) { func (c *cliService) runCommand(server, data string) {
if cmds := strings.Split(data, " "); len(cmds) > 0 && len(cmds[0]) > 0 { if cmds := strutil.SplitCmd(data); len(cmds) > 0 && len(cmds[0]) > 0 {
if client, err := c.getRedisClient(server); err == nil { if client, err := c.getRedisClient(server); err == nil {
args := sliceutil.Map(cmds, func(i int) any { args := sliceutil.Map(cmds, func(i int) any {
return cmds[i] return cmds[i]
}) })
if result, err := client.Do(c.ctx, args...).Result(); err == nil || err == redis.Nil { if result, err := client.Do(c.ctx, args...).Result(); err == nil || errors.Is(err, redis.Nil) {
if strings.ToLower(cmds[0]) == "select" { if strings.ToLower(cmds[0]) == "select" {
// switch database // switch database
if db, ok := strutil.AnyToInt(cmds[1]); ok { if db, ok := strutil.AnyToInt(cmds[1]); ok {

View File

@ -2,7 +2,9 @@ package strutil
import ( import (
"encoding/json" "encoding/json"
"regexp"
"strconv" "strconv"
"strings"
sliceutil "tinyrdm/backend/utils/slice" sliceutil "tinyrdm/backend/utils/slice"
) )
@ -129,3 +131,16 @@ func AnyToString(value interface{}, prefix string, layer int) (s string) {
// //
// return output.String(), true // return output.String(), true
//} //}
func SplitCmd(cmd string) []string {
re := regexp.MustCompile(`'[^']+'|"[^"]+"|\S+`)
args := re.FindAllString(cmd, -1)
return sliceutil.FilterMap(args, func(i int) (string, bool) {
arg := strings.Trim(args[i], "\"")
arg = strings.Trim(arg, "'")
if len(arg) <= 0 {
return "", false
}
return arg, true
})
}