tile38/internal/controller/output.go

43 lines
1.0 KiB
Go
Raw Normal View History

2016-03-29 22:29:15 +03:00
package controller
import (
"strings"
"time"
2017-10-05 18:20:40 +03:00
"github.com/tidwall/resp"
"github.com/tidwall/tile38/internal/server"
2016-03-29 22:29:15 +03:00
)
2017-10-05 18:20:40 +03:00
func (c *Controller) cmdOutput(msg *server.Message) (res resp.Value, err error) {
2016-03-29 22:29:15 +03:00
start := time.Now()
vs := msg.Values[1:]
var arg string
var ok bool
2017-10-05 18:20:40 +03:00
2016-03-29 22:29:15 +03:00
if len(vs) != 0 {
2016-04-03 04:33:38 +03:00
if _, arg, ok = tokenval(vs); !ok || arg == "" {
2017-10-05 18:20:40 +03:00
return server.NOMessage, errInvalidNumberOfArguments
2016-03-29 22:29:15 +03:00
}
// Setting the original message output type will be picked up by the
// server prior to the next command being executed.
switch strings.ToLower(arg) {
default:
2017-10-05 18:20:40 +03:00
return server.NOMessage, errInvalidArgument(arg)
2016-03-29 22:29:15 +03:00
case "json":
msg.OutputType = server.JSON
case "resp":
msg.OutputType = server.RESP
}
return server.OKMessage(msg, start), nil
}
// return the output
switch msg.OutputType {
default:
2017-10-05 18:20:40 +03:00
return server.NOMessage, nil
2016-03-29 22:29:15 +03:00
case server.JSON:
2017-10-05 18:20:40 +03:00
return resp.StringValue(`{"ok":true,"output":"json","elapsed":` + time.Now().Sub(start).String() + `}`), nil
2016-03-29 22:29:15 +03:00
case server.RESP:
2017-10-05 18:20:40 +03:00
return resp.StringValue("resp"), nil
2016-03-29 22:29:15 +03:00
}
}