Skip to content

Commit 049d4f9

Browse files
authored
Add: Support for COMMAND LIST command (redis#2491)
* feat: add support and tests for Command list command Co-authored-by: Anuragkillswitch <70265851+Anuragkillswitch@users.noreply.github.com>
1 parent 30a6f71 commit 049d4f9

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

command.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3992,3 +3992,10 @@ func (cmd *FunctionListCmd) readFunctions(rd *proto.Reader) ([]Function, error)
39923992
}
39933993
return functions, nil
39943994
}
3995+
3996+
type FilterBy struct {
3997+
Module string
3998+
ACLCat string
3999+
Pattern string
4000+
}
4001+

commands.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ type Cmdable interface {
124124
TxPipeline() Pipeliner
125125

126126
Command(ctx context.Context) *CommandsInfoCmd
127+
CommandList(ctx context.Context, filter *FilterBy) *StringSliceCmd
127128
ClientGetName(ctx context.Context) *StringCmd
128129
Echo(ctx context.Context, message interface{}) *StringCmd
129130
Ping(ctx context.Context) *StatusCmd
@@ -537,6 +538,23 @@ func (c cmdable) Command(ctx context.Context) *CommandsInfoCmd {
537538
return cmd
538539
}
539540

541+
func (c cmdable) CommandList(ctx context.Context, filter *FilterBy) *StringSliceCmd {
542+
args := make([]interface{}, 0, 5)
543+
args = append(args, "command", "list")
544+
if filter != nil {
545+
if filter.Module != "" {
546+
args = append(args, "filterby", "module", filter.Module)
547+
} else if filter.ACLCat != "" {
548+
args = append(args, "filterby", "aclcat", filter.ACLCat)
549+
} else if filter.Pattern != "" {
550+
args = append(args, "filterby", "pattern", filter.Pattern)
551+
}
552+
}
553+
cmd := NewStringSliceCmd(ctx, args...)
554+
_ = c(ctx, cmd)
555+
return cmd
556+
}
557+
540558
// ClientGetName returns the name of the connection.
541559
func (c cmdable) ClientGetName(ctx context.Context) *StringCmd {
542560
cmd := NewStringCmd(ctx, "client", "getname")

commands_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6277,6 +6277,57 @@ var _ = Describe("Commands", func() {
62776277
Expect(err).To(Equal(redis.Nil))
62786278
})
62796279

6280+
It("should return all command names", func() {
6281+
cmdList := client.CommandList(ctx, nil)
6282+
Expect(cmdList.Err()).NotTo(HaveOccurred())
6283+
cmdNames := cmdList.Val()
6284+
6285+
Expect(cmdNames).NotTo(BeEmpty())
6286+
6287+
// Assert that some expected commands are present in the list
6288+
Expect(cmdNames).To(ContainElement("get"))
6289+
Expect(cmdNames).To(ContainElement("set"))
6290+
Expect(cmdNames).To(ContainElement("hset"))
6291+
})
6292+
6293+
It("should filter commands by module", func() {
6294+
filter := &redis.FilterBy{
6295+
Module: "JSON",
6296+
}
6297+
cmdList := client.CommandList(ctx, filter)
6298+
Expect(cmdList.Err()).NotTo(HaveOccurred())
6299+
Expect(cmdList.Val()).To(HaveLen(0))
6300+
})
6301+
6302+
It("should filter commands by ACL category", func() {
6303+
6304+
filter := &redis.FilterBy{
6305+
ACLCat: "admin",
6306+
}
6307+
6308+
cmdList := client.CommandList(ctx, filter)
6309+
Expect(cmdList.Err()).NotTo(HaveOccurred())
6310+
cmdNames := cmdList.Val()
6311+
6312+
// Assert that the returned list only contains commands from the admin ACL category
6313+
Expect(len(cmdNames)).To(BeNumerically(">", 10))
6314+
})
6315+
6316+
It("should filter commands by pattern", func() {
6317+
filter := &redis.FilterBy{
6318+
Pattern: "*GET*",
6319+
}
6320+
cmdList := client.CommandList(ctx, filter)
6321+
Expect(cmdList.Err()).NotTo(HaveOccurred())
6322+
cmdNames := cmdList.Val()
6323+
6324+
// Assert that the returned list only contains commands that match the given pattern
6325+
Expect(cmdNames).To(ContainElement("get"))
6326+
Expect(cmdNames).To(ContainElement("getbit"))
6327+
Expect(cmdNames).To(ContainElement("getrange"))
6328+
Expect(cmdNames).NotTo(ContainElement("set"))
6329+
})
6330+
62806331
It("Dump and restores all libraries", func() {
62816332
err := client.FunctionLoad(ctx, lib1Code).Err()
62826333
Expect(err).NotTo(HaveOccurred())

0 commit comments

Comments
 (0)