Added short project ID name support if not ambiguous.
[src/xds/xds-server.git] / lib / model / folders.go
index 576c4a2..b8e6cf5 100644 (file)
@@ -146,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 == "" {
@@ -168,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())
        }
@@ -214,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)