added echo command

This commit is contained in:
Sahil
2024-06-18 15:47:49 +05:30
parent b87c836845
commit 1965f9b9c8

View File

@@ -17,6 +17,7 @@ package connection
import ( import (
"errors" "errors"
"fmt" "fmt"
"github.com/echovault/echovault/internal" "github.com/echovault/echovault/internal"
"github.com/echovault/echovault/internal/constants" "github.com/echovault/echovault/internal/constants"
) )
@@ -32,6 +33,13 @@ func handlePing(params internal.HandlerFuncParams) ([]byte, error) {
} }
} }
func handleEcho(params internal.HandlerFuncParams) ([]byte, error) {
if len(params.Command) != 2 {
return nil, errors.New(constants.WrongArgsResponse)
}
return []byte(fmt.Sprintf("$%d\r\n%s\r\n", len(params.Command[1]), params.Command[1])), nil
}
func Commands() []internal.Command { func Commands() []internal.Command {
return []internal.Command{ return []internal.Command{
{ {
@@ -51,5 +59,21 @@ Otherwise, the server will return "PONG".`,
}, },
HandlerFunc: handlePing, HandlerFunc: handlePing,
}, },
{
Command: "echo",
Module: constants.ConnectionModule,
Categories: []string{constants.ConnectionCategory, constants.FastCategory},
Description: `(ECHO message)
Echo the message back to the client.`,
Sync: false,
KeyExtractionFunc: func(cmd []string) (internal.KeyExtractionFuncResult, error) {
return internal.KeyExtractionFuncResult{
Channels: make([]string, 0),
ReadKeys: make([]string, 0),
WriteKeys: make([]string, 0),
}, nil
},
HandlerFunc: handleEcho,
},
} }
} }