Added -logfile option.
[src/xds/xds-server.git] / lib / xdsconfig / foldersconfig.go
1 package xdsconfig
2
3 import (
4         "fmt"
5 )
6
7 // FoldersConfig contains all the folder configurations
8 type FoldersConfig []FolderConfig
9
10 // GetIdx returns the index of the folder matching id in FoldersConfig array
11 func (c FoldersConfig) GetIdx(id string) int {
12         for i := range c {
13                 if id == c[i].ID {
14                         return i
15                 }
16         }
17         return -1
18 }
19
20 // Update is used to fully update or add a new FolderConfig
21 func (c FoldersConfig) Update(newCfg FoldersConfig) FoldersConfig {
22         for i := range newCfg {
23                 found := false
24                 for j := range c {
25                         if newCfg[i].ID == c[j].ID {
26                                 c[j] = newCfg[i]
27                                 found = true
28                                 break
29                         }
30                 }
31                 if !found {
32                         c = append(c, newCfg[i])
33                 }
34         }
35         return c
36 }
37
38 // Delete is used to delete a folder matching id in FoldersConfig array
39 func (c FoldersConfig) Delete(id string) (FoldersConfig, FolderConfig, error) {
40         if idx := c.GetIdx(id); idx != -1 {
41                 f := c[idx]
42                 c = append(c[:idx], c[idx+1:]...)
43                 return c, f, nil
44         }
45
46         return c, FolderConfig{}, fmt.Errorf("invalid id")
47 }