cmd/syncthing, lib/config: Pause/resume all devices whithout argument

GitHub-Pull-Request: https://github.com/syncthing/syncthing/pull/3989
LGTM: AudriusButkevicius, calmh
This commit is contained in:
Simon Frei
2017-03-04 07:54:13 +00:00
committed by Jakob Borg
parent c20d612736
commit 416811a2a9
3 changed files with 51 additions and 25 deletions

View File

@@ -104,6 +104,7 @@ type configIntf interface {
Folders() map[string]config.FolderConfiguration
Devices() map[protocol.DeviceID]config.DeviceConfiguration
SetDevice(config.DeviceConfiguration) error
SetDevices([]config.DeviceConfiguration) error
Save() error
ListenAddresses() []string
RequiresRestart() bool
@@ -269,8 +270,8 @@ func (s *apiService) Serve() {
postRestMux.HandleFunc("/rest/system/restart", s.postSystemRestart) // -
postRestMux.HandleFunc("/rest/system/shutdown", s.postSystemShutdown) // -
postRestMux.HandleFunc("/rest/system/upgrade", s.postSystemUpgrade) // -
postRestMux.HandleFunc("/rest/system/pause", s.makeDevicePauseHandler(true)) // device
postRestMux.HandleFunc("/rest/system/resume", s.makeDevicePauseHandler(false)) // device
postRestMux.HandleFunc("/rest/system/pause", s.makeDevicePauseHandler(true)) // [device]
postRestMux.HandleFunc("/rest/system/resume", s.makeDevicePauseHandler(false)) // [device]
postRestMux.HandleFunc("/rest/system/debug", s.postSystemDebug) // [enable] [disable]
// Debug endpoints, not for general use
@@ -1118,19 +1119,31 @@ func (s *apiService) makeDevicePauseHandler(paused bool) http.HandlerFunc {
var qs = r.URL.Query()
var deviceStr = qs.Get("device")
device, err := protocol.DeviceIDFromString(deviceStr)
if err != nil {
http.Error(w, err.Error(), 500)
return
var cfgs []config.DeviceConfiguration
if deviceStr == "" {
for _, cfg := range s.cfg.Devices() {
cfg.Paused = paused
cfgs = append(cfgs, cfg)
}
} else {
device, err := protocol.DeviceIDFromString(deviceStr)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
cfg, ok := s.cfg.Devices()[device]
if !ok {
http.Error(w, "not found", http.StatusNotFound)
return
}
cfg.Paused = paused
cfgs = append(cfgs, cfg)
}
cfg, ok := s.cfg.Devices()[device]
if !ok {
http.Error(w, "not found", http.StatusNotFound)
}
cfg.Paused = paused
if err := s.cfg.SetDevice(cfg); err != nil {
if err := s.cfg.SetDevices(cfgs); err != nil {
http.Error(w, err.Error(), 500)
}
}