lib/api: Add /rest/config endpoint (fixes #6540) (#7001)

This commit is contained in:
Simon Frei
2020-10-22 19:54:35 +02:00
committed by GitHub
parent 1c2be84e4e
commit f0f60ba2e7
12 changed files with 717 additions and 154 deletions
+2
View File
@@ -304,7 +304,9 @@ func (cfg *Configuration) clean() error {
}
// Upgrade configuration versions as appropriate
migrationsMut.Lock()
migrations.apply(cfg)
migrationsMut.Unlock()
// Build a list of available devices
existingDevices := make(map[protocol.DeviceID]bool)
+28 -24
View File
@@ -14,6 +14,7 @@ import (
"runtime"
"sort"
"strings"
"sync"
"github.com/syncthing/syncthing/lib/fs"
"github.com/syncthing/syncthing/lib/upgrade"
@@ -24,30 +25,33 @@ import (
// config version. The conversion function can be nil in which case we just
// update the config version. The order of migrations doesn't matter here,
// put the newest on top for readability.
var migrations = migrationSet{
{32, migrateToConfigV32},
{31, migrateToConfigV31},
{30, migrateToConfigV30},
{29, migrateToConfigV29},
{28, migrateToConfigV28},
{27, migrateToConfigV27},
{26, nil}, // triggers database update
{25, migrateToConfigV25},
{24, migrateToConfigV24},
{23, migrateToConfigV23},
{22, migrateToConfigV22},
{21, migrateToConfigV21},
{20, migrateToConfigV20},
{19, nil}, // Triggers a database tweak
{18, migrateToConfigV18},
{17, nil}, // Fsync = true removed
{16, nil}, // Triggers a database tweak
{15, migrateToConfigV15},
{14, migrateToConfigV14},
{13, migrateToConfigV13},
{12, migrateToConfigV12},
{11, migrateToConfigV11},
}
var (
migrations = migrationSet{
{32, migrateToConfigV32},
{31, migrateToConfigV31},
{30, migrateToConfigV30},
{29, migrateToConfigV29},
{28, migrateToConfigV28},
{27, migrateToConfigV27},
{26, nil}, // triggers database update
{25, migrateToConfigV25},
{24, migrateToConfigV24},
{23, migrateToConfigV23},
{22, migrateToConfigV22},
{21, migrateToConfigV21},
{20, migrateToConfigV20},
{19, nil}, // Triggers a database tweak
{18, migrateToConfigV18},
{17, nil}, // Fsync = true removed
{16, nil}, // Triggers a database tweak
{15, migrateToConfigV15},
{14, migrateToConfigV14},
{13, migrateToConfigV13},
{12, migrateToConfigV12},
{11, migrateToConfigV11},
}
migrationsMut = sync.Mutex{}
)
type migrationSet []migration
+2
View File
@@ -26,7 +26,9 @@ func TestMigrateCrashReporting(t *testing.T) {
for i, tc := range cases {
cfg := Configuration{Version: 28, Options: tc.opts}
migrationsMut.Lock()
migrations.apply(&cfg)
migrationsMut.Unlock()
if cfg.Options.CREnabled != tc.enabled {
t.Errorf("%d: unexpected result, CREnabled: %v != %v", i, cfg.Options.CREnabled, tc.enabled)
}
+34
View File
@@ -64,6 +64,7 @@ type Wrapper interface {
GUI() GUIConfiguration
SetGUI(gui GUIConfiguration) (Waiter, error)
LDAP() LDAPConfiguration
SetLDAP(ldap LDAPConfiguration) (Waiter, error)
Options() OptionsConfiguration
SetOptions(opts OptionsConfiguration) (Waiter, error)
@@ -71,11 +72,13 @@ type Wrapper interface {
Folder(id string) (FolderConfiguration, bool)
Folders() map[string]FolderConfiguration
FolderList() []FolderConfiguration
RemoveFolder(id string) (Waiter, error)
SetFolder(fld FolderConfiguration) (Waiter, error)
SetFolders(folders []FolderConfiguration) (Waiter, error)
Device(id protocol.DeviceID) (DeviceConfiguration, bool)
Devices() map[protocol.DeviceID]DeviceConfiguration
DeviceList() []DeviceConfiguration
RemoveDevice(id protocol.DeviceID) (Waiter, error)
SetDevice(DeviceConfiguration) (Waiter, error)
SetDevices([]DeviceConfiguration) (Waiter, error)
@@ -230,6 +233,13 @@ func (w *wrapper) Devices() map[protocol.DeviceID]DeviceConfiguration {
return deviceMap
}
// DeviceList returns a slice of devices.
func (w *wrapper) DeviceList() []DeviceConfiguration {
w.mut.Lock()
defer w.mut.Unlock()
return w.cfg.Copy().Devices
}
// SetDevices adds new devices to the configuration, or overwrites existing
// devices with the same ID.
func (w *wrapper) SetDevices(devs []DeviceConfiguration) (Waiter, error) {
@@ -327,6 +337,22 @@ func (w *wrapper) SetFolders(folders []FolderConfiguration) (Waiter, error) {
return w.replaceLocked(newCfg)
}
// RemoveFolder removes the folder from the configuration
func (w *wrapper) RemoveFolder(id string) (Waiter, error) {
w.mut.Lock()
defer w.mut.Unlock()
newCfg := w.cfg.Copy()
for i := range newCfg.Folders {
if newCfg.Folders[i].ID == id {
newCfg.Folders = append(newCfg.Folders[:i], newCfg.Folders[i+1:]...)
return w.replaceLocked(newCfg)
}
}
return noopWaiter{}, nil
}
// Options returns the current options configuration object.
func (w *wrapper) Options() OptionsConfiguration {
w.mut.Lock()
@@ -349,6 +375,14 @@ func (w *wrapper) LDAP() LDAPConfiguration {
return w.cfg.LDAP.Copy()
}
func (w *wrapper) SetLDAP(ldap LDAPConfiguration) (Waiter, error) {
w.mut.Lock()
defer w.mut.Unlock()
newCfg := w.cfg.Copy()
newCfg.LDAP = ldap.Copy()
return w.replaceLocked(newCfg)
}
// GUI returns the current GUI configuration object.
func (w *wrapper) GUI() GUIConfiguration {
w.mut.Lock()