tile38/controller/readonly.go

49 lines
948 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
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
)
2016-03-29 15:53:53 +03:00
func (c *Controller) cmdReadOnly(msg *server.Message) (res string, err error) {
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
if vs, arg, ok = tokenval(vs); !ok || arg == "" {
return "", errInvalidNumberOfArguments
2016-03-05 02:08:16 +03:00
}
2016-03-29 15:53:53 +03:00
if len(vs) != 0 {
return "", 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
backup := c.config
switch strings.ToLower(arg) {
default:
2016-03-29 15:53:53 +03:00
return "", errInvalidArgument(arg)
2016-03-05 02:08:16 +03:00
case "yes":
2016-03-29 15:53:53 +03:00
if !c.config.ReadOnly {
update = true
c.config.ReadOnly = true
log.Info("read only")
2016-03-05 02:08:16 +03:00
}
case "no":
2016-03-29 15:53:53 +03:00
if c.config.ReadOnly {
update = true
c.config.ReadOnly = false
log.Info("read write")
2016-03-05 02:08:16 +03:00
}
}
2016-03-29 15:53:53 +03:00
if update {
err := c.writeConfig(false)
if err != nil {
c.config = backup
return "", err
}
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
}