-
Notifications
You must be signed in to change notification settings - Fork 220
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add client get/set name commands
- Loading branch information
Showing
5 changed files
with
120 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package miniredis | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/alicebob/miniredis/v2/server" | ||
) | ||
|
||
// commandsClient handles client operations. | ||
func commandsClient(m *Miniredis) { | ||
m.srv.Register("CLIENT", m.cmdClient) | ||
} | ||
|
||
// CLIENT | ||
func (m *Miniredis) cmdClient(c *server.Peer, cmd string, args []string) { | ||
if len(args) == 0 { | ||
setDirty(c) | ||
c.WriteError("ERR wrong number of arguments for 'client' command") | ||
return | ||
} | ||
|
||
switch strings.ToUpper(args[0]) { | ||
case "SETNAME": | ||
m.cmdClientSetName(c, args[1:]) | ||
case "GETNAME": | ||
m.cmdClientGetName(c, args[1:]) | ||
default: | ||
setDirty(c) | ||
c.WriteError(fmt.Sprintf("ERR 'CLIENT %s' not supported", strings.Join(args, " "))) | ||
} | ||
} | ||
|
||
// CLIENT SETNAME | ||
func (m *Miniredis) cmdClientSetName(c *server.Peer, args []string) { | ||
if len(args) != 1 { | ||
setDirty(c) | ||
c.WriteError("ERR wrong number of arguments for 'client setname' command") | ||
return | ||
} | ||
|
||
c.ClientName = args[0] | ||
c.WriteOK() | ||
} | ||
|
||
// CLIENT GETNAME | ||
func (m *Miniredis) cmdClientGetName(c *server.Peer, args []string) { | ||
if len(args) > 0 { | ||
setDirty(c) | ||
c.WriteError("ERR wrong number of arguments for 'client getname' command") | ||
return | ||
} | ||
|
||
if c.ClientName == "" { | ||
c.WriteNull() | ||
} else { | ||
c.WriteBulk(c.ClientName) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package miniredis | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/alicebob/miniredis/v2/proto" | ||
) | ||
|
||
// Test CLIENT *. | ||
func TestClient(t *testing.T) { | ||
t.Run("setname and getname", func(t *testing.T) { | ||
s := RunT(t) | ||
c, err := proto.Dial(s.Addr()) | ||
ok(t, err) | ||
defer c.Close() | ||
|
||
// Set the client name | ||
mustDo(t, c, | ||
"CLIENT", "SETNAME", "miniredis-tests", | ||
proto.Inline("OK"), | ||
) | ||
|
||
// Get the client name | ||
mustDo(t, c, | ||
"CLIENT", "GETNAME", | ||
proto.String("miniredis-tests"), | ||
) | ||
}) | ||
|
||
t.Run("getname without setname", func(t *testing.T) { | ||
s := RunT(t) | ||
c, err := proto.Dial(s.Addr()) | ||
ok(t, err) | ||
defer c.Close() | ||
|
||
// Get the client name without setting it first | ||
mustDo(t, c, | ||
"CLIENT", "GETNAME", | ||
proto.Nil, | ||
) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters