ledisdb/server/command.go

60 lines
1.0 KiB
Go
Raw Normal View History

package server
2014-05-02 13:08:20 +04:00
2014-05-03 10:55:12 +04:00
import (
"fmt"
2014-05-20 04:41:24 +04:00
"github.com/siddontang/ledisdb/ledis"
"strconv"
2014-05-03 10:55:12 +04:00
"strings"
)
2014-05-02 13:08:20 +04:00
type CommandFunc func(req *requestContext) error
2014-05-02 13:08:20 +04:00
var regCmds = map[string]CommandFunc{}
2014-05-03 10:55:12 +04:00
func register(name string, f CommandFunc) {
if _, ok := regCmds[strings.ToLower(name)]; ok {
panic(fmt.Sprintf("%s has been registered", name))
}
regCmds[name] = f
}
func pingCommand(req *requestContext) error {
req.resp.writeStatus(PONG)
2014-05-03 10:55:12 +04:00
return nil
}
func echoCommand(req *requestContext) error {
if len(req.args) != 1 {
2014-05-03 10:55:12 +04:00
return ErrCmdParams
}
req.resp.writeBulk(req.args[0])
2014-05-03 10:55:12 +04:00
return nil
}
func selectCommand(req *requestContext) error {
if len(req.args) != 1 {
2014-05-20 04:41:24 +04:00
return ErrCmdParams
}
if index, err := strconv.Atoi(ledis.String(req.args[0])); err != nil {
2014-05-20 04:41:24 +04:00
return err
} else {
if db, err := req.ldb.Select(index); err != nil {
2014-05-20 04:41:24 +04:00
return err
} else {
req.db = db
req.resp.writeStatus(OK)
2014-05-20 04:41:24 +04:00
}
}
return nil
}
2014-05-03 10:55:12 +04:00
func init() {
register("ping", pingCommand)
register("echo", echoCommand)
2014-05-20 04:41:24 +04:00
register("select", selectCommand)
2014-05-03 10:55:12 +04:00
}