tile38/internal/server/output.go

42 lines
936 B
Go
Raw Permalink Normal View History

package server
2016-03-29 22:29:15 +03:00
import (
"strings"
"time"
2017-10-05 18:20:40 +03:00
"github.com/tidwall/resp"
2016-03-29 22:29:15 +03:00
)
func (c *Server) cmdOutput(msg *Message) (res resp.Value, err error) {
2016-03-29 22:29:15 +03:00
start := time.Now()
vs := msg.Args[1:]
2016-03-29 22:29:15 +03:00
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 == "" {
return 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:
return NOMessage, errInvalidArgument(arg)
2016-03-29 22:29:15 +03:00
case "json":
msg.OutputType = JSON
2016-03-29 22:29:15 +03:00
case "resp":
msg.OutputType = RESP
2016-03-29 22:29:15 +03:00
}
return OKMessage(msg, start), nil
2016-03-29 22:29:15 +03:00
}
// return the output
switch msg.OutputType {
default:
return NOMessage, nil
case JSON:
2017-10-05 18:20:40 +03:00
return resp.StringValue(`{"ok":true,"output":"json","elapsed":` + time.Now().Sub(start).String() + `}`), nil
case RESP:
2017-10-05 18:20:40 +03:00
return resp.StringValue("resp"), nil
2016-03-29 22:29:15 +03:00
}
}