aboutsummaryrefslogtreecommitdiffstats
path: root/lib/model/folders.go
diff options
context:
space:
mode:
authorSebastien Douheret <sebastien.douheret@iot.bzh>2017-11-24 01:28:00 +0100
committerSebastien Douheret <sebastien.douheret@iot.bzh>2017-11-24 01:28:00 +0100
commit67a7d6e46143410a5fa9cfa2554023ab7687ea34 (patch)
tree6168592c5cc72a39795aa49a5683a81f2b872a19 /lib/model/folders.go
parentd2335afe1058153a3aad27fa483499b0639e096c (diff)
Add folder update support and ClientData field.
- folder config can be updated using PUT /folders/:id route - ClientData field of FolderConfig can be used by client to store any data (used from example by dashboard to save build settings)
Diffstat (limited to 'lib/model/folders.go')
-rw-r--r--lib/model/folders.go49
1 files changed, 49 insertions, 0 deletions
diff --git a/lib/model/folders.go b/lib/model/folders.go
index b8e6cf5..0e28538 100644
--- a/lib/model/folders.go
+++ b/lib/model/folders.go
@@ -10,6 +10,7 @@ import (
"time"
"github.com/Sirupsen/logrus"
+ "github.com/franciscocpg/reflectme"
common "github.com/iotbzh/xds-common/golib"
"github.com/iotbzh/xds-server/lib/folder"
"github.com/iotbzh/xds-server/lib/syncthing"
@@ -318,6 +319,54 @@ func (f *Folders) Delete(id string) (folder.FolderConfig, error) {
return fld, err
}
+// Update Update a specific folder
+func (f *Folders) Update(id string, cfg folder.FolderConfig) (*folder.FolderConfig, error) {
+ fcMutex.Lock()
+ defer fcMutex.Unlock()
+
+ fc, exist := f.folders[id]
+ if !exist {
+ return nil, fmt.Errorf("unknown id")
+ }
+
+ // Copy current in a new object to change nothing in case of an error rises
+ newCfg := folder.FolderConfig{}
+ reflectme.Copy((*fc).GetConfig(), &newCfg)
+
+ // Only update some fields
+ dirty := false
+ for _, fieldName := range folder.FolderConfigUpdatableFields {
+ valNew, err := reflectme.GetField(cfg, fieldName)
+ if err == nil {
+ valCur, err := reflectme.GetField(newCfg, fieldName)
+ if err == nil && valNew != valCur {
+ err = reflectme.SetField(&newCfg, fieldName, valNew)
+ if err != nil {
+ return nil, err
+ }
+ dirty = true
+ }
+ }
+ }
+
+ if !dirty {
+ return &newCfg, nil
+ }
+
+ fld, err := (*fc).Update(newCfg)
+ if err != nil {
+ return fld, err
+ }
+
+ // Save config on disk
+ err = f.SaveConfig()
+
+ // Send event to notified changes
+ // TODO emit folder change event
+
+ return fld, err
+}
+
// RegisterEventChange requests registration for folder event change
func (f *Folders) RegisterEventChange(id string, cb *folder.EventCB, data *folder.EventCBData) error {