8 common "github.com/iotbzh/xds-common/golib"
9 stconfig "github.com/syncthing/syncthing/lib/config"
10 "github.com/syncthing/syncthing/lib/protocol"
13 // FIXME remove and use an interface on xdsconfig.FolderConfig
14 type FolderChangeArg struct {
22 // FolderChange is called when configuration has changed
23 func (s *SyncThing) FolderChange(f FolderChangeArg) (string, error) {
26 stCfg, err := s.ConfigGet()
32 // Add new Device if needed
33 var devID protocol.DeviceID
34 if err := devID.UnmarshalText([]byte(f.SyncThingID)); err != nil {
35 s.log.Errorf("not a valid device id (err %v)", err)
39 newDevice := stconfig.DeviceConfiguration{
42 Addresses: []string{"dynamic"},
46 for _, device := range stCfg.Devices {
47 if device.DeviceID == devID {
53 stCfg.Devices = append(stCfg.Devices, newDevice)
56 // Add or update Folder settings
58 if label = f.Label; label == "" {
59 label = strings.Split(id, "/")[0]
61 if id = f.ID; id == "" {
62 id = f.SyncThingID[0:15] + "_" + label
66 pathCli, err := common.ResolveEnvVar(f.RelativePath)
68 pathCli = f.RelativePath
70 // SEB still need ShareRootDir ? a sup
71 // pathCli := filepath.Join(f.ShareRootDir, f.RelativePath)
73 folder := stconfig.FolderConfiguration{
81 if s.conf.FileConf.SThgConf.RescanIntervalS > 0 {
82 folder.RescanIntervalS = s.conf.FileConf.SThgConf.RescanIntervalS
86 folder.Devices = append(folder.Devices, stconfig.FolderDeviceConfiguration{
87 DeviceID: newDevice.DeviceID,
91 var fld stconfig.FolderConfiguration
92 for _, fld = range stCfg.Folders {
93 if folder.ID == fld.ID {
100 stCfg.Folders = append(stCfg.Folders, folder)
101 fld = stCfg.Folders[0]
104 err = s.ConfigSet(stCfg)
112 // FolderDelete is called to delete a folder config
113 func (s *SyncThing) FolderDelete(id string) error {
114 // Get current config
115 stCfg, err := s.ConfigGet()
121 for i, fld := range stCfg.Folders {
123 stCfg.Folders = append(stCfg.Folders[:i], stCfg.Folders[i+1:]...)
124 err = s.ConfigSet(stCfg)
135 // FolderConfigGet Returns the configuration of a specific folder
136 func (s *SyncThing) FolderConfigGet(folderID string) (stconfig.FolderConfiguration, error) {
137 fc := stconfig.FolderConfiguration{}
139 return fc, fmt.Errorf("folderID not set")
141 cfg, err := s.ConfigGet()
145 for _, f := range cfg.Folders {
146 if f.ID == folderID {
151 return fc, fmt.Errorf("id not found")
154 // FolderStatus Returns all information about the current
155 func (s *SyncThing) FolderStatus(folderID string) (*FolderStatus, error) {
159 return nil, fmt.Errorf("folderID not set")
161 if err := s.client.HTTPGet("db/status?folder="+folderID, &data); err != nil {
164 if err := json.Unmarshal(data, &res); err != nil {
170 // IsFolderInSync Returns true when folder is in sync
171 func (s *SyncThing) IsFolderInSync(folderID string) (bool, error) {
172 sts, err := s.FolderStatus(folderID)
176 return sts.NeedBytes == 0 && sts.State == "idle", nil
179 // FolderScan Request immediate folder scan.
180 // Scan all folders if folderID param is empty
181 func (s *SyncThing) FolderScan(folderID string, subpath string) error {
184 url += "?folder=" + folderID
187 url += "&sub=" + subpath
190 return s.client.HTTPPost(url, "")