Use go module as dependency tool instead of glide
[src/xds/xds-agent.git] / lib / syncthing / stfolder.go
index a5312eb..94d278c 100644 (file)
@@ -1,3 +1,20 @@
+/*
+ * Copyright (C) 2017-2018 "IoT.bzh"
+ * Author Sebastien Douheret <sebastien@iot.bzh>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
 package st
 
 import (
@@ -5,20 +22,57 @@ import (
        "fmt"
        "strings"
 
-       common "github.com/iotbzh/xds-common/golib"
+       common "gerrit.automotivelinux.org/gerrit/src/xds/xds-common.git"
        stconfig "github.com/syncthing/syncthing/lib/config"
        "github.com/syncthing/syncthing/lib/protocol"
 )
 
-// FIXME remove and use an interface on xdsconfig.FolderConfig
+// FolderChangeArg argument structure used by FolderChange
 type FolderChangeArg struct {
        ID           string
        Label        string
        RelativePath string
        SyncThingID  string
-       ShareRootDir string
 }
 
+// FolderLoadFromStConfig Load/Retrieve folder config from syncthing database
+/*
+func (s *SyncThing) FolderLoadFromStConfig(f *[]xsapiv1.FolderConfig) error {
+
+       defaultSdk := "" // cannot know which was the default sdk
+
+       stCfg, err := s.ConfigGet()
+       if err != nil {
+               return err
+       }
+       if len(stCfg.Devices) < 1 {
+               return fmt.Errorf("Cannot load syncthing config: no device defined")
+       }
+       devID := stCfg.Devices[0].DeviceID.String()
+       if devID == s.MyID {
+               if len(stCfg.Devices) < 2 {
+                       return fmt.Errorf("Cannot load syncthing config: no valid device found")
+               }
+               devID = stCfg.Devices[1].DeviceID.String()
+       }
+
+       for _, stFld := range stCfg.Folders {
+               *f = append(*f, xsapiv1.FolderConfig{
+                       ID:            stFld.ID,
+                       Label:         stFld.Label,
+                       ClientPath:    strings.TrimRight(stFld.Path, "/"),
+                       Type:          xsapiv1.TypeCloudSync,
+                       Status:        StatusDisable,
+                       DefaultSdk:    defaultSdk,
+                       RootPath:      "",
+                       DataCloudSync: xsapiv1.CloudSyncConfig{SyncThingID: devID},
+               })
+       }
+
+       return nil
+}
+*/
+
 // FolderChange is called when configuration has changed
 func (s *SyncThing) FolderChange(f FolderChangeArg) (string, error) {
 
@@ -29,16 +83,17 @@ func (s *SyncThing) FolderChange(f FolderChangeArg) (string, error) {
                return "", err
        }
 
+       stClientID := f.SyncThingID
        // Add new Device if needed
        var devID protocol.DeviceID
-       if err := devID.UnmarshalText([]byte(f.SyncThingID)); err != nil {
+       if err := devID.UnmarshalText([]byte(stClientID)); err != nil {
                s.log.Errorf("not a valid device id (err %v)", err)
                return "", err
        }
 
        newDevice := stconfig.DeviceConfiguration{
                DeviceID:  devID,
-               Name:      f.SyncThingID,
+               Name:      stClientID,
                Addresses: []string{"dynamic"},
        }
 
@@ -59,7 +114,7 @@ func (s *SyncThing) FolderChange(f FolderChangeArg) (string, error) {
                label = strings.Split(id, "/")[0]
        }
        if id = f.ID; id == "" {
-               id = f.SyncThingID[0:15] + "_" + label
+               id = stClientID[0:15] + "_" + label
        }
 
        // Resolve local path
@@ -67,13 +122,11 @@ func (s *SyncThing) FolderChange(f FolderChangeArg) (string, error) {
        if err != nil {
                pathCli = f.RelativePath
        }
-       // SEB still need ShareRootDir ? a sup
-       // pathCli := filepath.Join(f.ShareRootDir, f.RelativePath)
 
        folder := stconfig.FolderConfiguration{
                ID:            id,
                Label:         label,
-               RawPath:       pathCli,
+               Path:          pathCli,
                AutoNormalize: true,
        }
 
@@ -102,11 +155,8 @@ func (s *SyncThing) FolderChange(f FolderChangeArg) (string, error) {
        }
 
        err = s.ConfigSet(stCfg)
-       if err != nil {
-               s.log.Errorln(err)
-       }
 
-       return id, nil
+       return id, err
 }
 
 // FolderDelete is called to delete a folder config