a3da18405f1ada1ff50003962552a4fe1bed9745
[src/xds/xds-server.git] / lib / crosssdk / sdks.go
1 package crosssdk
2
3 import (
4         "fmt"
5         "path"
6         "path/filepath"
7         "strings"
8         "sync"
9
10         "github.com/Sirupsen/logrus"
11         common "github.com/iotbzh/xds-common/golib"
12         "github.com/iotbzh/xds-server/lib/xdsconfig"
13 )
14
15 // SDKs List of installed SDK
16 type SDKs struct {
17         Sdks map[string]*SDK
18
19         mutex sync.Mutex
20 }
21
22 // Init creates a new instance of Syncthing
23 func Init(cfg *xdsconfig.Config, log *logrus.Logger) (*SDKs, error) {
24         s := SDKs{
25                 Sdks: make(map[string]*SDK),
26         }
27
28         // Retrieve installed sdks
29         sdkRD := cfg.FileConf.SdkRootDir
30
31         if common.Exists(sdkRD) {
32
33                 // Assume that SDK install tree is <rootdir>/<profile>/<version>/<arch>
34                 dirs, err := filepath.Glob(path.Join(sdkRD, "*", "*", "*"))
35                 if err != nil {
36                         log.Debugf("Error while retrieving SDKs: dir=%s, error=%s", sdkRD, err.Error())
37                         return &s, err
38                 }
39                 s.mutex.Lock()
40                 defer s.mutex.Unlock()
41
42                 for _, d := range dirs {
43                         if !common.IsDir(d) {
44                                 continue
45                         }
46                         sdk, err := NewCrossSDK(d)
47                         if err != nil {
48                                 log.Debugf("Error while processing SDK dir=%s, err=%s", d, err.Error())
49                                 continue
50                         }
51                         s.Sdks[sdk.ID] = sdk
52                 }
53         }
54
55         log.Debugf("SDKs: %d cross sdks found", len(s.Sdks))
56
57         return &s, nil
58 }
59
60 // ResolveID Complete an SDK ID (helper for user that can use partial ID value)
61 func (s *SDKs) ResolveID(id string) (string, error) {
62         if id == "" {
63                 return "", nil
64         }
65
66         match := []string{}
67         for iid := range s.Sdks {
68                 if strings.HasPrefix(iid, id) {
69                         match = append(match, iid)
70                 }
71         }
72
73         if len(match) == 1 {
74                 return match[0], nil
75         } else if len(match) == 0 {
76                 return id, fmt.Errorf("Unknown sdk id")
77         }
78         return id, fmt.Errorf("Multiple sdk IDs found with provided prefix: " + id)
79 }
80
81 // Get returns an SDK from id
82 func (s *SDKs) Get(id string) *SDK {
83         s.mutex.Lock()
84         defer s.mutex.Unlock()
85
86         sc, exist := s.Sdks[id]
87         if !exist {
88                 return nil
89         }
90         return sc
91 }
92
93 // GetAll returns all existing SDKs
94 func (s *SDKs) GetAll() []SDK {
95         s.mutex.Lock()
96         defer s.mutex.Unlock()
97         res := []SDK{}
98         for _, v := range s.Sdks {
99                 res = append(res, *v)
100         }
101         return res
102 }
103
104 // GetEnvCmd returns the command used to initialized the environment for an SDK
105 func (s *SDKs) GetEnvCmd(id string, defaultID string) []string {
106         if id == "" && defaultID == "" {
107                 // no env cmd
108                 return []string{}
109         }
110
111         s.mutex.Lock()
112         defer s.mutex.Unlock()
113
114         if iid, err := s.ResolveID(id); err == nil {
115                 if sdk, exist := s.Sdks[iid]; exist {
116                         return sdk.GetEnvCmd()
117                 }
118         }
119
120         if sdk, exist := s.Sdks[defaultID]; defaultID != "" && exist {
121                 return sdk.GetEnvCmd()
122         }
123
124         // Return default env that may be empty
125         return []string{}
126 }