Skip to content

Commit 343016b

Browse files
authored
add InfoMap command (#2665)
Add an extended version of Info() to parse the results from a call to redis.Info so that it’s simpler to operate on any given item in the result. Signed-off-by: Nic Gibson <[email protected]>
1 parent 4408f8c commit 343016b

File tree

3 files changed

+108
-0
lines changed

3 files changed

+108
-0
lines changed

command.go

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package redis
22

33
import (
4+
"bufio"
45
"context"
56
"fmt"
67
"net"
8+
"regexp"
79
"strconv"
810
"strings"
911
"time"
@@ -5298,3 +5300,84 @@ type LibraryInfo struct {
52985300
LibName *string
52995301
LibVer *string
53005302
}
5303+
5304+
// -------------------------------------------
5305+
5306+
type InfoCmd struct {
5307+
baseCmd
5308+
val map[string]map[string]string
5309+
}
5310+
5311+
var _ Cmder = (*InfoCmd)(nil)
5312+
5313+
func NewInfoCmd(ctx context.Context, args ...interface{}) *InfoCmd {
5314+
return &InfoCmd{
5315+
baseCmd: baseCmd{
5316+
ctx: ctx,
5317+
args: args,
5318+
},
5319+
}
5320+
}
5321+
5322+
func (cmd *InfoCmd) SetVal(val map[string]map[string]string) {
5323+
cmd.val = val
5324+
}
5325+
5326+
func (cmd *InfoCmd) Val() map[string]map[string]string {
5327+
return cmd.val
5328+
}
5329+
5330+
func (cmd *InfoCmd) Result() (map[string]map[string]string, error) {
5331+
return cmd.Val(), cmd.Err()
5332+
}
5333+
5334+
func (cmd *InfoCmd) String() string {
5335+
return cmdString(cmd, cmd.val)
5336+
}
5337+
5338+
func (cmd *InfoCmd) readReply(rd *proto.Reader) error {
5339+
val, err := rd.ReadString()
5340+
if err != nil {
5341+
return err
5342+
}
5343+
5344+
section := ""
5345+
scanner := bufio.NewScanner(strings.NewReader(val))
5346+
moduleRe := regexp.MustCompile(`module:name=(.+?),(.+)$`)
5347+
5348+
for scanner.Scan() {
5349+
line := scanner.Text()
5350+
if strings.HasPrefix(line, "#") {
5351+
if cmd.val == nil {
5352+
cmd.val = make(map[string]map[string]string)
5353+
}
5354+
section = strings.TrimPrefix(line, "# ")
5355+
cmd.val[section] = make(map[string]string)
5356+
} else if line != "" {
5357+
if section == "Modules" {
5358+
kv := moduleRe.FindStringSubmatch(line)
5359+
if len(kv) == 3 {
5360+
cmd.val[section][kv[1]] = kv[2]
5361+
}
5362+
} else {
5363+
kv := strings.SplitN(line, ":", 2)
5364+
if len(kv) == 2 {
5365+
cmd.val[section][kv[0]] = kv[1]
5366+
}
5367+
}
5368+
}
5369+
}
5370+
5371+
return nil
5372+
5373+
}
5374+
5375+
func (cmd *InfoCmd) Item(section, key string) string {
5376+
if cmd.val == nil {
5377+
return ""
5378+
} else if cmd.val[section] == nil {
5379+
return ""
5380+
} else {
5381+
return cmd.val[section][key]
5382+
}
5383+
}

commands.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,17 @@ func (c cmdable) Info(ctx context.Context, sections ...string) *StringCmd {
571571
return cmd
572572
}
573573

574+
func (c cmdable) InfoMap(ctx context.Context, sections ...string) *InfoCmd {
575+
args := make([]interface{}, 1+len(sections))
576+
args[0] = "info"
577+
for i, section := range sections {
578+
args[i+1] = section
579+
}
580+
cmd := NewInfoCmd(ctx, args...)
581+
_ = c(ctx, cmd)
582+
return cmd
583+
}
584+
574585
func (c cmdable) LastSave(ctx context.Context) *IntCmd {
575586
cmd := NewIntCmd(ctx, "lastsave")
576587
_ = c(ctx, cmd)

commands_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,20 @@ var _ = Describe("Commands", func() {
335335
Expect(info.Val()).NotTo(Equal(""))
336336
})
337337

338+
It("should InfoMap", Label("redis.info"), func() {
339+
info := client.InfoMap(ctx)
340+
Expect(info.Err()).NotTo(HaveOccurred())
341+
Expect(info.Val()).NotTo(BeNil())
342+
343+
info = client.InfoMap(ctx, "dummy")
344+
Expect(info.Err()).NotTo(HaveOccurred())
345+
Expect(info.Val()).To(BeNil())
346+
347+
info = client.InfoMap(ctx, "server")
348+
Expect(info.Err()).NotTo(HaveOccurred())
349+
Expect(info.Val()).To(HaveLen(1))
350+
})
351+
338352
It("should Info cpu", func() {
339353
info := client.Info(ctx, "cpu")
340354
Expect(info.Err()).NotTo(HaveOccurred())

0 commit comments

Comments
 (0)