7 "github.com/iotbzh/xds-server/lib/xdsconfig"
8 "github.com/syncthing/syncthing/lib/config"
9 "github.com/syncthing/syncthing/lib/protocol"
12 // FolderChange is called when configuration has changed
13 func (s *SyncThing) FolderChange(f xdsconfig.FolderConfig) error {
16 stCfg, err := s.ConfigGet()
22 // Add new Device if needed
23 var devID protocol.DeviceID
24 if err := devID.UnmarshalText([]byte(f.SyncThingID)); err != nil {
25 s.log.Errorf("not a valid device id (err %v)\n", err)
29 newDevice := config.DeviceConfiguration{
32 Addresses: []string{"dynamic"},
36 for _, device := range stCfg.Devices {
37 if device.DeviceID == devID {
43 stCfg.Devices = append(stCfg.Devices, newDevice)
46 // Add or update Folder settings
48 if label = f.Label; label == "" {
49 label = strings.Split(id, "/")[0]
51 if id = f.ID; id == "" {
52 id = f.SyncThingID[0:15] + "_" + label
55 folder := config.FolderConfiguration{
58 RawPath: filepath.Join(s.conf.ShareRootDir, f.RelativePath),
61 folder.Devices = append(folder.Devices, config.FolderDeviceConfiguration{
62 DeviceID: newDevice.DeviceID,
66 var fld config.FolderConfiguration
67 for _, fld = range stCfg.Folders {
68 if folder.ID == fld.ID {
75 stCfg.Folders = append(stCfg.Folders, folder)
76 fld = stCfg.Folders[0]
79 err = s.ConfigSet(stCfg)
87 // FolderDelete is called to delete a folder config
88 func (s *SyncThing) FolderDelete(id string) error {
90 stCfg, err := s.ConfigGet()
96 for i, fld := range stCfg.Folders {
98 stCfg.Folders = append(stCfg.Folders[:i], stCfg.Folders[i+1:]...)
99 err = s.ConfigSet(stCfg)