0da0d1b859274027a34dbc8af924571ff51d10a5
[src/xds/xds-server.git] / lib / crosssdk / sdks.go
1 package crosssdk
2
3 import (
4         "path"
5         "path/filepath"
6         "sync"
7
8         "github.com/Sirupsen/logrus"
9         common "github.com/iotbzh/xds-common/golib"
10         "github.com/iotbzh/xds-server/lib/xdsconfig"
11 )
12
13 // SDKs List of installed SDK
14 type SDKs struct {
15         Sdks []SDK
16
17         mutex sync.Mutex
18 }
19
20 // Init creates a new instance of Syncthing
21 func Init(cfg *xdsconfig.Config, log *logrus.Logger) (*SDKs, error) {
22         s := SDKs{}
23
24         // Retrieve installed sdks
25         sdkRD := cfg.FileConf.SdkRootDir
26
27         if common.Exists(sdkRD) {
28
29                 // Assume that SDK install tree is <rootdir>/<profile>/<version>/<arch>
30                 dirs, err := filepath.Glob(path.Join(sdkRD, "*", "*", "*"))
31                 if err != nil {
32                         log.Debugf("Error while retrieving SDKs: dir=%s, error=%s", sdkRD, err.Error())
33                         return &s, err
34                 }
35                 s.mutex.Lock()
36                 defer s.mutex.Unlock()
37
38                 for _, d := range dirs {
39                         if !common.IsDir(d) {
40                                 continue
41                         }
42                         sdk, err := NewCrossSDK(d)
43                         if err != nil {
44                                 log.Debugf("Error while processing SDK dir=%s, err=%s", d, err.Error())
45                                 continue
46                         }
47                         s.Sdks = append(s.Sdks, *sdk)
48                 }
49         }
50
51         log.Debugf("SDKs: %d cross sdks found", len(s.Sdks))
52
53         return &s, nil
54 }
55
56 // GetAll returns all existing SDKs
57 func (s *SDKs) GetAll() []SDK {
58         s.mutex.Lock()
59         defer s.mutex.Unlock()
60         res := s.Sdks
61         return res
62 }
63
64 // Get returns an SDK from id
65 func (s *SDKs) Get(id int) SDK {
66         s.mutex.Lock()
67         defer s.mutex.Unlock()
68
69         if id < 0 || id > len(s.Sdks) {
70                 return SDK{}
71         }
72         res := s.Sdks[id]
73         return res
74 }
75
76 // GetEnvCmd returns the command used to initialized the environment for an SDK
77 func (s *SDKs) GetEnvCmd(id string, defaultID string) []string {
78         if id == "" && defaultID == "" {
79                 // no env cmd
80                 return []string{}
81         }
82
83         s.mutex.Lock()
84         defer s.mutex.Unlock()
85         defaultEnv := []string{}
86         for _, sdk := range s.Sdks {
87                 if sdk.ID == id {
88                         return sdk.GetEnvCmd()
89                 }
90                 if sdk.ID == defaultID {
91                         defaultEnv = sdk.GetEnvCmd()
92                 }
93         }
94         // Return default env that may be empty
95         return defaultEnv
96 }