From 55592137a22d63b9990abded58e483a576477fa7 Mon Sep 17 00:00:00 2001 From: Jakob Borg Date: Sat, 7 Nov 2015 09:47:31 +0100 Subject: [PATCH] Use constructor functions for FolderConfiguration and DeviceConfiguration --- cmd/syncthing/main.go | 30 ++++++++++++------------------ lib/config/deviceconfiguration.go | 7 +++++++ lib/config/folderconfiguration.go | 9 +++++++++ lib/model/model_test.go | 17 +++-------------- 4 files changed, 31 insertions(+), 32 deletions(-) diff --git a/cmd/syncthing/main.go b/cmd/syncthing/main.go index 749d21e0e..4bcf6c088 100644 --- a/cmd/syncthing/main.go +++ b/cmd/syncthing/main.go @@ -904,25 +904,19 @@ func setupGUI(mainSvc *suture.Supervisor, cfg *config.Wrapper, m *model.Model, a } func defaultConfig(myName string) config.Configuration { + defaultFolder := config.NewFolderConfiguration("default", locations[locDefFolder]) + defaultFolder.RescanIntervalS = 60 + defaultFolder.MinDiskFreePct = 1 + defaultFolder.Devices = []config.FolderDeviceConfiguration{{DeviceID: myID}} + defaultFolder.AutoNormalize = true + defaultFolder.MaxConflicts = -1 + + thisDevice := config.NewDeviceConfiguration(myID, myName) + thisDevice.Addresses = []string{"dynamic"} + newCfg := config.New(myID) - newCfg.Folders = []config.FolderConfiguration{ - { - ID: "default", - RawPath: locations[locDefFolder], - RescanIntervalS: 60, - MinDiskFreePct: 1, - Devices: []config.FolderDeviceConfiguration{{DeviceID: myID}}, - AutoNormalize: true, - MaxConflicts: -1, - }, - } - newCfg.Devices = []config.DeviceConfiguration{ - { - DeviceID: myID, - Addresses: []string{"dynamic"}, - Name: myName, - }, - } + newCfg.Folders = []config.FolderConfiguration{defaultFolder} + newCfg.Devices = []config.DeviceConfiguration{thisDevice} port, err := getFreePort("127.0.0.1", 8384) if err != nil { diff --git a/lib/config/deviceconfiguration.go b/lib/config/deviceconfiguration.go index cc479d546..42d7b707e 100644 --- a/lib/config/deviceconfiguration.go +++ b/lib/config/deviceconfiguration.go @@ -17,6 +17,13 @@ type DeviceConfiguration struct { Introducer bool `xml:"introducer,attr" json:"introducer"` } +func NewDeviceConfiguration(id protocol.DeviceID, name string) DeviceConfiguration { + return DeviceConfiguration{ + DeviceID: id, + Name: name, + } +} + func (orig DeviceConfiguration) Copy() DeviceConfiguration { c := orig c.Addresses = make([]string, len(orig.Addresses)) diff --git a/lib/config/folderconfiguration.go b/lib/config/folderconfiguration.go index fe6c0b244..bcd6dcfca 100644 --- a/lib/config/folderconfiguration.go +++ b/lib/config/folderconfiguration.go @@ -44,6 +44,15 @@ type FolderDeviceConfiguration struct { DeviceID protocol.DeviceID `xml:"id,attr" json:"deviceID"` } +func NewFolderConfiguration(id, path string) FolderConfiguration { + f := FolderConfiguration{ + ID: id, + RawPath: path, + } + f.prepare() + return f +} + func (f FolderConfiguration) Copy() FolderConfiguration { c := f c.Devices = make([]FolderDeviceConfiguration, len(f.Devices)) diff --git a/lib/model/model_test.go b/lib/model/model_test.go index dcd28207d..007973cea 100644 --- a/lib/model/model_test.go +++ b/lib/model/model_test.go @@ -32,22 +32,11 @@ func init() { device1, _ = protocol.DeviceIDFromString("AIR6LPZ-7K4PTTV-UXQSMUU-CPQ5YWH-OEDFIIQ-JUG777G-2YQXXR5-YD6AWQR") device2, _ = protocol.DeviceIDFromString("GYRZZQB-IRNPV4Z-T7TC52W-EQYJ3TT-FDQW6MW-DFLMU42-SSSU6EM-FBK2VAY") - defaultFolderConfig = config.FolderConfiguration{ - ID: "default", - RawPath: "testdata", - Devices: []config.FolderDeviceConfiguration{ - { - DeviceID: device1, - }, - }, - } + defaultFolderConfig = config.NewFolderConfiguration("default", "testdata") + defaultConfig.Devices = []config.FolderDeviceConfiguration{{DeviceID: device1}} _defaultConfig := config.Configuration{ Folders: []config.FolderConfiguration{defaultFolderConfig}, - Devices: []config.DeviceConfiguration{ - { - DeviceID: device1, - }, - }, + Devices: []config.DeviceConfiguration{config.NewDeviceConfiguration(device1, "device1")}, Options: config.OptionsConfiguration{ // Don't remove temporaries directly on startup KeepTemporariesH: 1,