Added short project ID name support if not ambiguous.
[src/xds/xds-server.git] / lib / model / folders.go
index ed0078e..b8e6cf5 100644 (file)
@@ -78,6 +78,8 @@ func (f *Folders) LoadConfig() error {
                        // Don't exit on such error, just log it
                        f.Log.Errorf(err.Error())
                }
+       } else {
+               f.Log.Infof("Syncthing support is disabled.")
        }
 
        // Merge syncthing folders into XDS folders
@@ -101,20 +103,22 @@ func (f *Folders) LoadConfig() error {
 
        // Detect ghost project
        // (IOW existing in xds file config and not in syncthing database)
-       for i, xf := range flds {
-               // only for syncthing project
-               if xf.Type != folder.TypeCloudSync {
-                       continue
-               }
-               found := false
-               for _, stf := range stFlds {
-                       if stf.ID == xf.ID {
-                               found = true
-                               break
+       if f.SThg != nil {
+               for i, xf := range flds {
+                       // only for syncthing project
+                       if xf.Type != folder.TypeCloudSync {
+                               continue
+                       }
+                       found := false
+                       for _, stf := range stFlds {
+                               if stf.ID == xf.ID {
+                                       found = true
+                                       break
+                               }
+                       }
+                       if !found {
+                               flds[i].Status = folder.StatusErrorConfig
                        }
-               }
-               if !found {
-                       flds[i].Status = folder.StatusErrorConfig
                }
        }
 
@@ -142,6 +146,27 @@ func (f *Folders) SaveConfig() error {
        return foldersConfigWrite(f.fileOnDisk, f.getConfigArrUnsafe())
 }
 
+// ResolveID Complete a Folder ID (helper for user that can use partial ID value)
+func (f *Folders) ResolveID(id string) (string, error) {
+       if id == "" {
+               return "", nil
+       }
+
+       match := []string{}
+       for iid := range f.folders {
+               if strings.HasPrefix(iid, id) {
+                       match = append(match, iid)
+               }
+       }
+
+       if len(match) == 1 {
+               return match[0], nil
+       } else if len(match) == 0 {
+               return id, fmt.Errorf("Unknown id")
+       }
+       return id, fmt.Errorf("Multiple IDs found with provided prefix: " + id)
+}
+
 // Get returns the folder config or nil if not existing
 func (f *Folders) Get(id string) *folder.IFOLDER {
        if id == "" {
@@ -164,8 +189,7 @@ func (f *Folders) GetConfigArr() []folder.FolderConfig {
 
 // getConfigArrUnsafe Same as GetConfigArr without mutex protection
 func (f *Folders) getConfigArrUnsafe() []folder.FolderConfig {
-       var conf []folder.FolderConfig
-
+       conf := []folder.FolderConfig{}
        for _, v := range f.folders {
                conf = append(conf, (*v).GetConfig())
        }
@@ -196,10 +220,13 @@ func (f *Folders) createUpdate(newF folder.FolderConfig, create bool, initial bo
        switch newF.Type {
        // SYNCTHING
        case folder.TypeCloudSync:
-               if f.SThg == nil {
-                       return nil, fmt.Errorf("ClownSync type not supported (syncthing not initialized)")
+               if f.SThg != nil {
+                       fld = f.SThg.NewFolderST(f.Conf)
+               } else {
+                       f.Log.Debugf("Disable project %v (syncthing not initialized)", newF.ID)
+                       fld = folder.NewFolderSTDisable(f.Conf)
                }
-               fld = f.SThg.NewFolderST(f.Conf)
+
        // PATH MAP
        case folder.TypePathMap:
                fld = folder.NewFolderPathMap(f.Conf)
@@ -207,24 +234,23 @@ func (f *Folders) createUpdate(newF folder.FolderConfig, create bool, initial bo
                return nil, fmt.Errorf("Unsupported folder type")
        }
 
+       // Allocate a new UUID
+       if create {
+               newF.ID = fld.NewUID("")
+       }
+       if !create && newF.ID == "" {
+               return nil, fmt.Errorf("Cannot update folder with null ID")
+       }
+
        // Set default value if needed
        if newF.Status == "" {
                newF.Status = folder.StatusDisable
        }
        if newF.Label == "" {
-               newF.Label = filepath.Base(newF.ClientPath) + "_" + newF.ID[0:8]
-       }
-
-       // Allocate a new UUID
-       if create {
-               i := len(newF.Label)
-               if i > 20 {
-                       i = 20
+               newF.Label = filepath.Base(newF.ClientPath)
+               if len(newF.ID) > 8 {
+                       newF.Label += "_" + newF.ID[0:8]
                }
-               newF.ID = fld.NewUID(newF.Label[:i])
-       }
-       if !create && newF.ID == "" {
-               return nil, fmt.Errorf("Cannot update folder with null ID")
        }
 
        // Normalize path (needed for Windows path including bashlashes)