mirror of
https://github.com/EchoVault/SugarDB.git
synced 2025-10-10 02:10:17 +08:00
Added GetAllCommands method to server interface. Implemented ACL CAT command
This commit is contained in:
@@ -119,6 +119,14 @@ func (server *Server) SetValue(ctx context.Context, key string, value interface{
|
|||||||
server.store[key] = value
|
server.store[key] = value
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (server *Server) GetAllCommands(ctx context.Context) []utils.Command {
|
||||||
|
var commands []utils.Command
|
||||||
|
for _, serverCommand := range server.commands {
|
||||||
|
commands = append(commands, serverCommand.Command)
|
||||||
|
}
|
||||||
|
return commands
|
||||||
|
}
|
||||||
|
|
||||||
func (server *Server) getCommand(cmd string) (utils.ServerCommand, error) {
|
func (server *Server) getCommand(cmd string) (utils.ServerCommand, error) {
|
||||||
for key, command := range server.commands {
|
for key, command := range server.commands {
|
||||||
if strings.EqualFold(key, cmd) {
|
if strings.EqualFold(key, cmd) {
|
||||||
|
@@ -77,7 +77,63 @@ func (p Plugin) handleGetUser(ctx context.Context, cmd []string, server utils.Se
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p Plugin) handleCat(ctx context.Context, cmd []string, server utils.Server) ([]byte, error) {
|
func (p Plugin) handleCat(ctx context.Context, cmd []string, server utils.Server) ([]byte, error) {
|
||||||
return nil, errors.New("ACL CAT not implemented")
|
if len(cmd) > 3 {
|
||||||
|
return nil, errors.New(utils.WRONG_ARGS_RESPONSE)
|
||||||
|
}
|
||||||
|
|
||||||
|
categories := make(map[string][]string)
|
||||||
|
|
||||||
|
commands := server.GetAllCommands(ctx)
|
||||||
|
|
||||||
|
for _, command := range commands {
|
||||||
|
if len(command.SubCommands) == 0 {
|
||||||
|
for _, category := range command.Categories {
|
||||||
|
categories[category] = append(categories[category], command.Command)
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
for _, subcommand := range command.SubCommands {
|
||||||
|
for _, category := range subcommand.Categories {
|
||||||
|
categories[category] = append(categories[category],
|
||||||
|
fmt.Sprintf("%s|%s", command.Command, subcommand.Command))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(cmd) == 2 {
|
||||||
|
var cats []string
|
||||||
|
length := 0
|
||||||
|
for key, _ := range categories {
|
||||||
|
cats = append(cats, key)
|
||||||
|
length += 1
|
||||||
|
}
|
||||||
|
res := fmt.Sprintf("*%d", length)
|
||||||
|
for i, cat := range cats {
|
||||||
|
res = fmt.Sprintf("%s\r\n+%s", res, cat)
|
||||||
|
if i == len(cats)-1 {
|
||||||
|
res = res + "\r\n\n"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return []byte(res), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(cmd) == 3 {
|
||||||
|
var res string
|
||||||
|
for category, commands := range categories {
|
||||||
|
if strings.EqualFold(category, cmd[2]) {
|
||||||
|
res = fmt.Sprintf("*%d", len(commands))
|
||||||
|
for i, command := range commands {
|
||||||
|
res = fmt.Sprintf("%s\r\n+%s", res, command)
|
||||||
|
if i == len(commands)-1 {
|
||||||
|
res = res + "\r\n\n"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return []byte(res), nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil, errors.New("category not found")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p Plugin) handleUsers(ctx context.Context, cmd []string, server utils.Server) ([]byte, error) {
|
func (p Plugin) handleUsers(ctx context.Context, cmd []string, server utils.Server) ([]byte, error) {
|
||||||
|
@@ -19,6 +19,7 @@ type Server interface {
|
|||||||
CreateKeyAndLock(ctx context.Context, key string) (bool, error)
|
CreateKeyAndLock(ctx context.Context, key string) (bool, error)
|
||||||
GetValue(key string) interface{}
|
GetValue(key string) interface{}
|
||||||
SetValue(ctx context.Context, key string, value interface{})
|
SetValue(ctx context.Context, key string, value interface{})
|
||||||
|
GetAllCommands(ctx context.Context) []Command
|
||||||
}
|
}
|
||||||
|
|
||||||
type ContextServerID string
|
type ContextServerID string
|
||||||
|
Reference in New Issue
Block a user