bb2b56f8ab8c728382d83e52a4c845025df82ca7
[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         DefaultSdk    string     `json:"defaultSdk"`
33
34         // Not exported fields
35         RootPath string `json:"-"`
36 }
37
38 // NewFolderConfig creates a new folder object
39 func NewFolderConfig(id, label, rootDir, path string, defaultSdk string) FolderConfig {
40         return FolderConfig{
41                 ID:           id,
42                 Label:        label,
43                 RelativePath: path,
44                 Type:         FolderTypeCloudSync,
45                 SyncThingID:  "",
46                 Status:       FolderStatusDisable,
47                 RootPath:     rootDir,
48                 DefaultSdk:   defaultSdk,
49         }
50 }
51
52 // GetFullPath returns the full path
53 func (c *FolderConfig) GetFullPath(dir string) string {
54         if &dir == nil {
55                 dir = ""
56         }
57         if filepath.IsAbs(dir) {
58                 return filepath.Join(c.RootPath, dir)
59         }
60         return filepath.Join(c.RootPath, c.RelativePath, dir)
61 }
62
63 // Verify is called to verify that a configuration is valid
64 func (c *FolderConfig) Verify() error {
65         var err error
66
67         if c.Type != FolderTypeCloudSync {
68                 err = fmt.Errorf("Unsupported folder type")
69         }
70
71         if c.SyncThingID == "" {
72                 err = fmt.Errorf("device id not set (SyncThingID field)")
73         }
74
75         if c.RootPath == "" {
76                 err = fmt.Errorf("RootPath must not be empty")
77         }
78
79         if err != nil {
80                 c.Status = FolderStatusErrorConfig
81                 log.Printf("ERROR Verify: %v\n", err)
82         }
83
84         return err
85 }