Skip to content

Commit

Permalink
command: server - config get
Browse files Browse the repository at this point in the history
  • Loading branch information
nalgeon committed Nov 11, 2024
1 parent 85cd2e5 commit 3049a9f
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 0 deletions.
2 changes: 2 additions & 0 deletions internal/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ func Parse(args [][]byte) (redis.Cmd, error) {
// server
case "command":
return server.ParseOK(b)
case "config":
return server.ParseConfig(b)
case "dbsize":
return server.ParseDBSize(b)
case "flushdb":
Expand Down
49 changes: 49 additions & 0 deletions internal/command/server/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package server

import (
"github.com/nalgeon/redka/internal/redis"
)

// Container command for runtime configuration commands.
// CONFIG
// https://redis.io/commands/config
type Config struct {
redis.BaseCmd
subcmd string
get ConfigGet
}

func ParseConfig(b redis.BaseCmd) (Config, error) {
// Extract the subcommand.
cmd := Config{BaseCmd: b}
if len(cmd.Args()) == 0 {
return Config{}, redis.ErrInvalidArgNum
}
cmd.subcmd = string(cmd.Args()[0])

// Parse the subcommand.
var err error
args := cmd.Args()[1:]
switch cmd.subcmd {
case "get":
cmd.get, err = ParseConfigGet(args)
default:
err = redis.ErrUnknownSubcmd
}

// Return the resulting command.
if err != nil {
return Config{}, err
}
return cmd, nil
}

func (c Config) Run(w redis.Writer, red redis.Redka) (any, error) {
switch c.subcmd {
case "get":
return c.get.Run(w, red)
default:
w.WriteString("OK")
return true, nil
}
}
63 changes: 63 additions & 0 deletions internal/command/server/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package server

import (
"testing"

"github.com/nalgeon/redka/internal/redis"
"github.com/nalgeon/redka/internal/testx"
)

func TestConfigParse(t *testing.T) {
tests := []struct {
cmd string
want Config
err error
}{
{
cmd: "config",
want: Config{},
err: redis.ErrInvalidArgNum,
},
{
cmd: "config get",
want: Config{},
err: redis.ErrInvalidArgNum,
},
{
cmd: "config get *",
want: Config{subcmd: "get"},
err: nil,
},
{
cmd: "config set parameter value",
want: Config{},
err: redis.ErrUnknownSubcmd,
},
}

for _, test := range tests {
t.Run(test.cmd, func(t *testing.T) {
cmd, err := redis.Parse(ParseConfig, test.cmd)
testx.AssertEqual(t, err, test.err)
if err == nil {
testx.AssertEqual(t, cmd.subcmd, test.want.subcmd)
} else {
testx.AssertEqual(t, cmd, test.want)
}
})
}
}

func TestConfigExec(t *testing.T) {
t.Run("config get", func(t *testing.T) {
db, red := getDB(t)
defer db.Close()

cmd := redis.MustParse(ParseConfig, "config get *")
conn := redis.NewFakeConn()
res, err := cmd.Run(conn, red)
testx.AssertNoErr(t, err)
testx.AssertEqual(t, res, true)
testx.AssertEqual(t, conn.Out(), "2,databases,1")
})
}
30 changes: 30 additions & 0 deletions internal/command/server/configget.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package server

import (
"github.com/nalgeon/redka/internal/redis"
)

// Returns the effective values of configuration parameters.
// CONFIG GET parameter [parameter ...]
// https://redis.io/commands/config-get
type ConfigGet struct {
params []string
}

func ParseConfigGet(args [][]byte) (ConfigGet, error) {
if len(args) < 1 {
return ConfigGet{}, redis.ErrInvalidArgNum
}
cmd := ConfigGet{params: make([]string, len(args))}
for i, arg := range args {
cmd.params[i] = string(arg)
}
return cmd, nil
}

func (c ConfigGet) Run(w redis.Writer, _ redis.Redka) (any, error) {
w.WriteArray(2)
w.WriteString("databases")
w.WriteInt(1)
return true, nil
}

0 comments on commit 3049a9f

Please sign in to comment.