tile38/controller/readonly.go

46 lines
943 B
Go
Raw Normal View History

2016-03-05 02:08:16 +03:00
package controller
import (
"strings"
2016-03-29 15:53:53 +03:00
"time"
2016-03-05 02:08:16 +03:00
2017-10-05 18:20:40 +03:00
"github.com/tidwall/resp"
2016-03-06 17:55:00 +03:00
"github.com/tidwall/tile38/controller/log"
2016-03-29 15:53:53 +03:00
"github.com/tidwall/tile38/controller/server"
2016-03-05 02:08:16 +03:00
)
2017-10-05 18:20:40 +03:00
func (c *Controller) cmdReadOnly(msg *server.Message) (res resp.Value, err error) {
2016-03-29 15:53:53 +03:00
start := time.Now()
vs := msg.Values[1:]
2016-03-05 02:08:16 +03:00
var arg string
2016-03-29 15:53:53 +03:00
var ok bool
2017-10-05 18:20:40 +03:00
2016-03-29 15:53:53 +03:00
if vs, arg, ok = tokenval(vs); !ok || arg == "" {
2017-10-05 18:20:40 +03:00
return server.NOMessage, errInvalidNumberOfArguments
2016-03-05 02:08:16 +03:00
}
2016-03-29 15:53:53 +03:00
if len(vs) != 0 {
2017-10-05 18:20:40 +03:00
return server.NOMessage, errInvalidNumberOfArguments
2016-03-05 02:08:16 +03:00
}
2016-03-29 15:53:53 +03:00
update := false
2016-03-05 02:08:16 +03:00
switch strings.ToLower(arg) {
default:
2017-10-05 18:20:40 +03:00
return server.NOMessage, errInvalidArgument(arg)
2016-03-05 02:08:16 +03:00
case "yes":
2017-09-30 04:11:05 +03:00
if !c.config.readOnly() {
2016-03-29 15:53:53 +03:00
update = true
2017-09-30 04:11:05 +03:00
c.config.setReadOnly(true)
2016-03-29 15:53:53 +03:00
log.Info("read only")
2016-03-05 02:08:16 +03:00
}
case "no":
2017-09-30 04:11:05 +03:00
if c.config.readOnly() {
2016-03-29 15:53:53 +03:00
update = true
2017-09-30 04:11:05 +03:00
c.config.setReadOnly(false)
2016-03-29 15:53:53 +03:00
log.Info("read write")
2016-03-05 02:08:16 +03:00
}
}
2016-03-29 15:53:53 +03:00
if update {
2017-09-30 04:11:05 +03:00
c.config.write(false)
2016-03-05 02:08:16 +03:00
}
2016-03-29 15:53:53 +03:00
return server.OKMessage(msg, start), nil
2016-03-05 02:08:16 +03:00
}