aboutsummaryrefslogtreecommitdiffstats
path: root/lib/model
diff options
context:
space:
mode:
authorSebastien Douheret <sebastien.douheret@iot.bzh>2017-05-16 22:51:32 +0200
committerSebastien Douheret <sebastien.douheret@iot.bzh>2017-05-17 14:04:32 +0200
commitc07adb807c41a1545a9a0f5bbf40080d86946538 (patch)
tree21e00efbcd03360416698663a1ea89536717ae86 /lib/model
parent8983eebdbe74489d62eae4097580fc430d75bd07 (diff)
Auto start Syncthing and Syncthing-inotify.
Signed-off-by: Sebastien Douheret <sebastien.douheret@iot.bzh>
Diffstat (limited to 'lib/model')
-rw-r--r--lib/model/folder.go99
1 files changed, 99 insertions, 0 deletions
diff --git a/lib/model/folder.go b/lib/model/folder.go
new file mode 100644
index 0000000..6687b68
--- /dev/null
+++ b/lib/model/folder.go
@@ -0,0 +1,99 @@
+package model
+
+import (
+ "fmt"
+
+ "github.com/iotbzh/xds-server/lib/syncthing"
+ "github.com/iotbzh/xds-server/lib/xdsconfig"
+)
+
+// Folder Represent a an XDS folder
+type Folder struct {
+ Conf *xdsconfig.Config
+ SThg *st.SyncThing
+}
+
+// NewFolder Create a new instance of Model Folder
+func NewFolder(cfg *xdsconfig.Config, st *st.SyncThing) *Folder {
+ return &Folder{
+ Conf: cfg,
+ SThg: st,
+ }
+}
+
+// GetFolderFromID retrieves the Folder config from id
+func (c *Folder) GetFolderFromID(id string) *xdsconfig.FolderConfig {
+ if idx := c.Conf.Folders.GetIdx(id); idx != -1 {
+ return &c.Conf.Folders[idx]
+ }
+ return nil
+}
+
+// UpdateAll updates all the current configuration
+func (c *Folder) UpdateAll(newCfg xdsconfig.Config) error {
+ return fmt.Errorf("Not Supported")
+ /*
+ if err := VerifyConfig(newCfg); err != nil {
+ return err
+ }
+
+ // TODO: c.Builder = c.Builder.Update(newCfg.Builder)
+ c.Folders = c.Folders.Update(newCfg.Folders)
+
+ // FIXME To be tested & improved error handling
+ for _, f := range c.Folders {
+ if err := c.SThg.FolderChange(st.FolderChangeArg{
+ ID: f.ID,
+ Label: f.Label,
+ RelativePath: f.RelativePath,
+ SyncThingID: f.SyncThingID,
+ ShareRootDir: c.ShareRootDir,
+ }); err != nil {
+ return err
+ }
+ }
+
+ return nil
+ */
+}
+
+// UpdateFolder updates a specific folder into the current configuration
+func (c *Folder) UpdateFolder(newFolder xdsconfig.FolderConfig) (xdsconfig.FolderConfig, error) {
+ // rootPath should not be empty
+ if newFolder.RootPath == "" {
+ newFolder.RootPath = c.Conf.ShareRootDir
+ }
+
+ // Sanity check of folder settings
+ if err := newFolder.Verify(); err != nil {
+ return xdsconfig.FolderConfig{}, err
+ }
+
+ c.Conf.Folders = c.Conf.Folders.Update(xdsconfig.FoldersConfig{newFolder})
+
+ err := c.SThg.FolderChange(st.FolderChangeArg{
+ ID: newFolder.ID,
+ Label: newFolder.Label,
+ RelativePath: newFolder.RelativePath,
+ SyncThingID: newFolder.SyncThingID,
+ ShareRootDir: c.Conf.ShareRootDir,
+ })
+ newFolder.BuilderSThgID = c.Conf.Builder.SyncThingID // FIXME - should be removed after local ST config rework
+ newFolder.Status = xdsconfig.FolderStatusEnable
+
+ return newFolder, err
+}
+
+// DeleteFolder deletes a specific folder
+func (c *Folder) DeleteFolder(id string) (xdsconfig.FolderConfig, error) {
+ var fld xdsconfig.FolderConfig
+ var err error
+
+ if err = c.SThg.FolderDelete(id); err != nil {
+ return fld, err
+ }
+
+ c.Conf.Folders, fld, err = c.Conf.Folders.Delete(id)
+
+ return fld, err
+}