Initial commit.
[src/xds/xds-agent.git] / lib / syncthing / stfolder.go
1 package st
2
3 import (
4         "path/filepath"
5         "strings"
6
7         "github.com/syncthing/syncthing/lib/config"
8         "github.com/syncthing/syncthing/lib/protocol"
9 )
10
11 // FIXME remove and use an interface on xdsconfig.FolderConfig
12 type FolderChangeArg struct {
13         ID           string
14         Label        string
15         RelativePath string
16         SyncThingID  string
17         ShareRootDir string
18 }
19
20 // FolderChange is called when configuration has changed
21 func (s *SyncThing) FolderChange(f FolderChangeArg) error {
22
23         // Get current config
24         stCfg, err := s.ConfigGet()
25         if err != nil {
26                 s.log.Errorln(err)
27                 return err
28         }
29
30         // Add new Device if needed
31         var devID protocol.DeviceID
32         if err := devID.UnmarshalText([]byte(f.SyncThingID)); err != nil {
33                 s.log.Errorf("not a valid device id (err %v)\n", err)
34                 return err
35         }
36
37         newDevice := config.DeviceConfiguration{
38                 DeviceID:  devID,
39                 Name:      f.SyncThingID,
40                 Addresses: []string{"dynamic"},
41         }
42
43         var found = false
44         for _, device := range stCfg.Devices {
45                 if device.DeviceID == devID {
46                         found = true
47                         break
48                 }
49         }
50         if !found {
51                 stCfg.Devices = append(stCfg.Devices, newDevice)
52         }
53
54         // Add or update Folder settings
55         var label, id string
56         if label = f.Label; label == "" {
57                 label = strings.Split(id, "/")[0]
58         }
59         if id = f.ID; id == "" {
60                 id = f.SyncThingID[0:15] + "_" + label
61         }
62
63         folder := config.FolderConfiguration{
64                 ID:      id,
65                 Label:   label,
66                 RawPath: filepath.Join(f.ShareRootDir, f.RelativePath),
67         }
68
69         folder.Devices = append(folder.Devices, config.FolderDeviceConfiguration{
70                 DeviceID: newDevice.DeviceID,
71         })
72
73         found = false
74         var fld config.FolderConfiguration
75         for _, fld = range stCfg.Folders {
76                 if folder.ID == fld.ID {
77                         fld = folder
78                         found = true
79                         break
80                 }
81         }
82         if !found {
83                 stCfg.Folders = append(stCfg.Folders, folder)
84                 fld = stCfg.Folders[0]
85         }
86
87         err = s.ConfigSet(stCfg)
88         if err != nil {
89                 s.log.Errorln(err)
90         }
91
92         return nil
93 }
94
95 // FolderDelete is called to delete a folder config
96 func (s *SyncThing) FolderDelete(id string) error {
97         // Get current config
98         stCfg, err := s.ConfigGet()
99         if err != nil {
100                 s.log.Errorln(err)
101                 return err
102         }
103
104         for i, fld := range stCfg.Folders {
105                 if id == fld.ID {
106                         stCfg.Folders = append(stCfg.Folders[:i], stCfg.Folders[i+1:]...)
107                         err = s.ConfigSet(stCfg)
108                         if err != nil {
109                                 s.log.Errorln(err)
110                                 return err
111                         }
112                 }
113         }
114
115         return nil
116 }