Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add: Support for COMMAND LIST command #2491

Merged
merged 10 commits into from
Mar 19, 2023
Merged
10 changes: 10 additions & 0 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -3992,3 +3992,13 @@ func (cmd *FunctionListCmd) readFunctions(rd *proto.Reader) ([]Function, error)
}
return functions, nil
}

type FilterBy struct {
Module string
AclCat string
SoulPancake marked this conversation as resolved.
Show resolved Hide resolved
Pattern string
}

type CommandListOptions struct {
SoulPancake marked this conversation as resolved.
Show resolved Hide resolved
FilterBy *FilterBy
}
19 changes: 19 additions & 0 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ type Cmdable interface {
TxPipeline() Pipeliner

Command(ctx context.Context) *CommandsInfoCmd
CommandList(ctx context.Context, opts *CommandListOptions) *StringSliceCmd
SoulPancake marked this conversation as resolved.
Show resolved Hide resolved
ClientGetName(ctx context.Context) *StringCmd
Echo(ctx context.Context, message interface{}) *StringCmd
Ping(ctx context.Context) *StatusCmd
Expand Down Expand Up @@ -537,6 +538,24 @@ func (c cmdable) Command(ctx context.Context) *CommandsInfoCmd {
return cmd
}

func (c cmdable) CommandList(ctx context.Context, opts *CommandListOptions) *StringSliceCmd {
args := []interface{}{"command", "list"}
SoulPancake marked this conversation as resolved.
Show resolved Hide resolved
if opts != nil && opts.FilterBy != nil {
filter := opts.FilterBy
if filter.Module != "" {
args = append(args, "filterby", "module", filter.Module)
} else if filter.AclCat != "" {
args = append(args, "filterby", "aclcat", filter.AclCat)
} else if filter.Pattern != "" {
args = append(args, "filterby", "pattern", filter.Pattern)
}
}
cmd := NewStringSliceCmd(ctx, args...)
_ = c(ctx, cmd)
return cmd
}


// ClientGetName returns the name of the connection.
func (c cmdable) ClientGetName(ctx context.Context) *StringCmd {
cmd := NewStringCmd(ctx, "client", "getname")
Expand Down
55 changes: 55 additions & 0 deletions commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6277,6 +6277,61 @@ var _ = Describe("Commands", func() {
Expect(err).To(Equal(redis.Nil))
})

It("should return all command names", func() {
cmdList := client.CommandList(ctx, nil)
Expect(cmdList.Err()).NotTo(HaveOccurred())
cmdNames := cmdList.Val()

Expect(cmdNames).NotTo(BeEmpty())

// Assert that some expected commands are present in the list
Expect(cmdNames).To(ContainElement("get"))
Expect(cmdNames).To(ContainElement("set"))
Expect(cmdNames).To(ContainElement("hset"))
})

It("should filter commands by module", func() {
opts := &redis.CommandListOptions{
FilterBy: &redis.FilterBy{
Module: "JSON",
},
}
cmdList := client.CommandList(ctx, opts)
Expect(cmdList.Err()).NotTo(HaveOccurred())
Expect(cmdList.Val()).To(HaveLen(0))
SoulPancake marked this conversation as resolved.
Show resolved Hide resolved
})

It("should filter commands by ACL category", func() {
opts := &redis.CommandListOptions{
FilterBy: &redis.FilterBy{
AclCat: "admin",
},
}
cmdList := client.CommandList(ctx, opts)
Expect(cmdList.Err()).NotTo(HaveOccurred())
cmdNames := cmdList.Val()

// Assert that the returned list only contains commands from the admin ACL category
Expect(len(cmdNames)).To(BeNumerically(">", 10))
})

It("should filter commands by pattern", func() {
opts := &redis.CommandListOptions{
FilterBy: &redis.FilterBy{
Pattern: "*GET*",
},
}
cmdList := client.CommandList(ctx, opts)
Expect(cmdList.Err()).NotTo(HaveOccurred())
cmdNames := cmdList.Val()

// Assert that the returned list only contains commands that match the given pattern
Expect(cmdNames).To(ContainElement("get"))
Expect(cmdNames).To(ContainElement("getbit"))
Expect(cmdNames).To(ContainElement("getrange"))
Expect(cmdNames).NotTo(ContainElement("set"))
})

It("Dump and restores all libraries", func() {
err := client.FunctionLoad(ctx, lib1Code).Err()
Expect(err).NotTo(HaveOccurred())
Expand Down