Fix invalid rootPath when creating a new project.
[src/xds/xds-server.git] / lib / xdsconfig / folderconfig.go
1 package xdsconfig
2
3 import (
4         "fmt"
5         "log"
6         "path/filepath"
7 )
8
9 // FolderType constances
10 const (
11         FolderTypeDocker           = 0
12         FolderTypeWindowsSubsystem = 1
13         FolderTypeCloudSync        = 2
14
15         FolderStatusErrorConfig = "ErrorConfig"
16         FolderStatusDisable     = "Disable"
17         FolderStatusEnable      = "Enable"
18 )
19
20 // FolderType is the type of sharing folder
21 type FolderType int
22
23 // FolderConfig is the config for one folder
24 type FolderConfig struct {
25         ID            string     `json:"id" binding:"required"`
26         Label         string     `json:"label"`
27         RelativePath  string     `json:"path"`
28         Type          FolderType `json:"type"`
29         SyncThingID   string     `json:"syncThingID"`
30         BuilderSThgID string     `json:"builderSThgID"`
31         Status        string     `json:"status"`
32
33         // Private fields
34         rootPath string
35 }
36
37 // NewFolderConfig creates a new folder object
38 func NewFolderConfig(id, label, rootDir, path string) FolderConfig {
39         return FolderConfig{
40                 ID:           id,
41                 Label:        label,
42                 RelativePath: path,
43                 Type:         FolderTypeCloudSync,
44                 SyncThingID:  "",
45                 Status:       FolderStatusDisable,
46                 rootPath:     rootDir,
47         }
48 }
49
50 // GetFullPath returns the full path
51 func (c *FolderConfig) GetFullPath(dir string) string {
52         if &dir == nil {
53                 dir = ""
54         }
55         if filepath.IsAbs(dir) {
56                 return filepath.Join(c.rootPath, dir)
57         }
58         return filepath.Join(c.rootPath, c.RelativePath, dir)
59 }
60
61 // FolderVerify is called to verify that a configuration is valid
62 func FolderVerify(fCfg FolderConfig) error {
63         var err error
64
65         if fCfg.Type != FolderTypeCloudSync {
66                 err = fmt.Errorf("Unsupported folder type")
67         }
68
69         if fCfg.SyncThingID == "" {
70                 err = fmt.Errorf("device id not set (SyncThingID field)")
71         }
72
73         if fCfg.rootPath == "" {
74                 err = fmt.Errorf("rootPath must not be empty")
75         }
76
77         if err != nil {
78                 fCfg.Status = FolderStatusErrorConfig
79                 log.Printf("ERROR FolderVerify: %v\n", err)
80         }
81
82         return err
83 }