diff --git a/src/main.go b/src/main.go index 5ea40d3..b24f9e8 100644 --- a/src/main.go +++ b/src/main.go @@ -119,6 +119,14 @@ func (server *Server) SetValue(ctx context.Context, key string, value interface{ 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) { for key, command := range server.commands { if strings.EqualFold(key, cmd) { diff --git a/src/modules/acl/commands.go b/src/modules/acl/commands.go index 00be0fc..3472803 100644 --- a/src/modules/acl/commands.go +++ b/src/modules/acl/commands.go @@ -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) { - 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) { diff --git a/src/utils/types.go b/src/utils/types.go index 2db439b..0da9b88 100644 --- a/src/utils/types.go +++ b/src/utils/types.go @@ -19,6 +19,7 @@ type Server interface { CreateKeyAndLock(ctx context.Context, key string) (bool, error) GetValue(key string) interface{} SetValue(ctx context.Context, key string, value interface{}) + GetAllCommands(ctx context.Context) []Command } type ContextServerID string