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