summaryrefslogtreecommitdiffstats
path: root/lib/syncthing/stfolder.go
diff options
context:
space:
mode:
authorSebastien Douheret <sebastien.douheret@iot.bzh>2017-05-11 19:42:00 +0200
committerSebastien Douheret <sebastien.douheret@iot.bzh>2017-05-11 19:42:22 +0200
commitec7051e1da665206f594c7616ad381bfeaea333a (patch)
treeecc01ee358794c9d8c5fbb87d2f5b6ce3f60f431 /lib/syncthing/stfolder.go
parentca3e1762832b27dc25cf90125b376c56e24e2db2 (diff)
Initial main commit.
Signed-off-by: Sebastien Douheret <sebastien.douheret@iot.bzh>
Diffstat (limited to 'lib/syncthing/stfolder.go')
-rw-r--r--lib/syncthing/stfolder.go116
1 files changed, 116 insertions, 0 deletions
diff --git a/lib/syncthing/stfolder.go b/lib/syncthing/stfolder.go
new file mode 100644
index 0000000..d79e579
--- /dev/null
+++ b/lib/syncthing/stfolder.go
@@ -0,0 +1,116 @@
+package st
+
+import (
+ "path/filepath"
+ "strings"
+
+ "github.com/syncthing/syncthing/lib/config"
+ "github.com/syncthing/syncthing/lib/protocol"
+)
+
+// FIXME remove and use an interface on xdsconfig.FolderConfig
+type FolderChangeArg struct {
+ ID string
+ Label string
+ RelativePath string
+ SyncThingID string
+ ShareRootDir string
+}
+
+// FolderChange is called when configuration has changed
+func (s *SyncThing) FolderChange(f FolderChangeArg) error {
+
+ // Get current config
+ stCfg, err := s.ConfigGet()
+ if err != nil {
+ s.log.Errorln(err)
+ return err
+ }
+
+ // Add new Device if needed
+ var devID protocol.DeviceID
+ if err := devID.UnmarshalText([]byte(f.SyncThingID)); err != nil {
+ s.log.Errorf("not a valid device id (err %v)\n", err)
+ return err
+ }
+
+ newDevice := config.DeviceConfiguration{
+ DeviceID: devID,
+ Name: f.SyncThingID,
+ Addresses: []string{"dynamic"},
+ }
+
+ var found = false
+ for _, device := range stCfg.Devices {
+ if device.DeviceID == devID {
+ found = true
+ break
+ }
+ }
+ if !found {
+ stCfg.Devices = append(stCfg.Devices, newDevice)
+ }
+
+ // Add or update Folder settings
+ var label, id string
+ if label = f.Label; label == "" {
+ label = strings.Split(id, "/")[0]
+ }
+ if id = f.ID; id == "" {
+ id = f.SyncThingID[0:15] + "_" + label
+ }
+
+ folder := config.FolderConfiguration{
+ ID: id,
+ Label: label,
+ RawPath: filepath.Join(f.ShareRootDir, f.RelativePath),
+ }
+
+ folder.Devices = append(folder.Devices, config.FolderDeviceConfiguration{
+ DeviceID: newDevice.DeviceID,
+ })
+
+ found = false
+ var fld config.FolderConfiguration
+ for _, fld = range stCfg.Folders {
+ if folder.ID == fld.ID {
+ fld = folder
+ found = true
+ break
+ }
+ }
+ if !found {
+ stCfg.Folders = append(stCfg.Folders, folder)
+ fld = stCfg.Folders[0]
+ }
+
+ err = s.ConfigSet(stCfg)
+ if err != nil {
+ s.log.Errorln(err)
+ }
+
+ return nil
+}
+
+// FolderDelete is called to delete a folder config
+func (s *SyncThing) FolderDelete(id string) error {
+ // Get current config
+ stCfg, err := s.ConfigGet()
+ if err != nil {
+ s.log.Errorln(err)
+ return err
+ }
+
+ for i, fld := range stCfg.Folders {
+ if id == fld.ID {
+ stCfg.Folders = append(stCfg.Folders[:i], stCfg.Folders[i+1:]...)
+ err = s.ConfigSet(stCfg)
+ if err != nil {
+ s.log.Errorln(err)
+ return err
+ }
+ }
+ }
+
+ return nil
+}